025. Configuration - dkkahm/study-springfamework5 GitHub Wiki
Java Configuration
@COnfiguration
public class ChuckConfiguration {
Bean
- Return type matter, not function name
@Bean
public ChuckNorrisQuotes chuckNorrisQuotes() {
return new ChuckNorrisQuotes();
}
@Autowired
private ChuckNorrisQuotes chuckNorrisQuotes;
XML Configuration
- XML config file position
- Loading configuration
@SpringBootApplication
@ImportResource("classpath:chuck-config.xml")
public class JokeApplication {
Spring Factory Bean
public class GreetingServiceFactory {
private GreetingRepositsory repository;
public GreetingServiceFactory (GreetingRepositsory repository) {
this.repository = repository;
}
public GreetingService createGreetingService(String lang) {
switch(lang) {
case "en":
return new PrimaryGreetingService(...);
case "de":
return new PrimaryGermanGreetingService(...);
....
}
@Configuration
public class GreetingSeericeConfig {
@Bean
GreetingSericeFactory greetingSericeFactory(GreetingRepositsory repository) {
return new GreetingServiceFactory(repository);
}
@Bean
@Primary
@Profile({"default", "en"})
GreetingService primaryGreetingService(GreetingServiceFactory greetingServiceFactory) {
return greetingServiceFactory.createGreetingService("en");
}
@Bean
@Primary
@Profile("es")
GreetingService primarySpanishGreetingService(GreetingServiceFactory greetingServiceFactory) {
return greetingServiceFactory.createGreetingService("es");
}
....
Spring Boot Auto Configuration Report
- application.properties
- debug=true
- logging.level.root=debug
- command line arguments
1. add to build.gradle
bootRun {
systemProperties = System.properties
}
2. run with -Ddebug=true
gradlew bootRun -Ddebug=true
Spring Bean Scope
- Singleton
- Prototype
- A new instance is created each time the bean is requested
- Request
- A single instance per http request
- Session
- A single instance per http session
- Applicaton
- bean is scoped to the lifecyle of a ServletContext
- Websocket
- Scopes a single bean definition to the lifecyle of a WebSocket
- Global-session
- A single instance per global session
- Typically Only used in a Portlet context
- In Java Configuration
- In XML Configuration