-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.*; | ||
import org.springframework.stereotype.Repository; | ||
import javax.annotation.PostConstruct; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.*; | ||
import org.springframework.jdbc.core.BeanPropertyRowMapper; | ||
|
||
class Movie { | ||
public String name; | ||
public int year; | ||
public int rating; | ||
|
||
public Movie(String name, int year, int rating) { | ||
this.name = name; | ||
this.year = year; | ||
this.rating = rating; | ||
} | ||
} | ||
|
||
@Repository | ||
public class MovieRepository { | ||
|
||
@Autowired | ||
private JdbcTemplate template; | ||
|
||
@PostConstruct | ||
public void createTable() { | ||
template.execute("CREATE TABLE movies (id bigint auto_increment primary key, name VARCHAR(50), year int, rating int)"); | ||
} | ||
|
||
public void createMovie(String name, int year, int rating) { | ||
template.update("INSERT INTO movies (id, name, year, rating) VALUES (?,?,?,?)", | ||
null, name, year, rating); | ||
} | ||
|
||
public List<Movie> findMoviesByName(String nameStartsWith) { | ||
return template.query("SELECT * FROM movies WHERE name LIKE '" + nameStartsWith + "%'", | ||
(rs, rowNum) -> new Movie(rs.getString("name"), rs.getInt("year"), rs.getInt("rating"))); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.config.ConfigurableBeanFactory; | ||
import org.springframework.context.annotation.*; | ||
import org.springframework.scheduling.*; | ||
import org.springframework.scheduling.annotation.*; | ||
import org.springframework.scheduling.concurrent.*; | ||
import org.springframework.stereotype.*; | ||
|
||
@FunctionalInterface | ||
interface TemperatureMeasurementCallback { | ||
public void temperatureMeasured(int temperature); | ||
} | ||
interface Thermometer { | ||
public int measure(); | ||
} | ||
|
||
@Configuration | ||
@EnableScheduling // Test Case | ||
@Import({FakeThermometer.class, WeatherForecastService.class}) | ||
class Config { | ||
|
||
@Bean | ||
public TemperatureMeasurementCallback callback() { | ||
System.out.println("Registering TemperatureMeasurementCallback bean"); | ||
return (temperature) -> System.out.println(temperature); | ||
} | ||
} | ||
|
||
@Component | ||
@Scope("prototype") // Test Case | ||
class FakeThermometer implements Thermometer { | ||
|
||
private int currentTemperature = 21; | ||
|
||
@Override | ||
public int measure() { return currentTemperature++; } | ||
} | ||
|
||
@Service | ||
public class WeatherForecastService { | ||
|
||
@Autowired | ||
private Thermometer thermometer; | ||
@Autowired | ||
private TemperatureMeasurementCallback callback; | ||
|
||
@Scheduled(fixedRate=50) // Test Case | ||
public void takeTemperatureMeasurement() { | ||
int temperature = thermometer.measure(); | ||
callback.temperatureMeasured(temperature); | ||
} | ||
} |