In -depth understanding of the @Configuration annotation in the Spring framework
annotations in the Spring framework are used to mark a class defined by a class. In Spring's Java -based configuration, this annotation is very important. It allows developers to configure the application context without XML. @Configuration
annotations, Spring will treat it as a configuration class and processes it to generate and manage Spring Bean. This type usually contains one or more @Configuration
annotations. These methods define the Bean managed by the Spring container. @Bean
This class becomes the source of Bean definition, and Spring will use these definitions to set the application context.
Spring will generate the CGLIB -based sub -class (proxy) to ensure that the method returns the same single -example instance by default. This behavior is called complete mode
. If you do not perform an agent, call the method multiple times may create multiple instances. @Bean
@Bean
Integrated with components
annotations), the class of annotations can be explicitly defined by, and Spring is allowed to automatically scan and register other beans.
@ComponentScan
Allow the injecting @SpringBootApplication
@Configuration
Basic example
@Configuration
<code class="language-java">import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public MyService myService() { return new MyServiceImpl(); } @Bean public MyController myController() { return new MyController(myService()); } }</code>
myController()
myService()
According to functions (such as DataConfig, ServiceConfig, and Webconfig), the configuration is split into multiple classes. This improves readability and maintenance. MyService
3. Use @componentscan to scan
<code class="language-java">@Configuration public class DataConfig { @Bean public DataSource dataSource() { // 配置并返回数据源 } } @Configuration public class ServiceConfig { @Bean public UserService userService() { return new UserServiceImpl(); } }</code>
components. @Value
@ConfigurationProperties
<code class="language-java">@Configuration public class AppConfig { @Value("${app.name}") private String appName; @Bean public AppService appService() { return new AppService(appName); } }</code>
<code class="language-java">import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public MyService myService() { return new MyServiceImpl(); } @Bean public MyController myController() { return new MyController(myService()); } }</code>
annotations. @ConfigurationProperties
@Value
<code class="language-java">@Configuration public class DataConfig { @Bean public DataSource dataSource() { // 配置并返回数据源 } } @Configuration public class ServiceConfig { @Bean public UserService userService() { return new UserServiceImpl(); } }</code>
@Configuration
new
Wrong writing: Cyclone dependence
Be careful when defining mutually dependent Bean, because it will cause circulation dependence.<code class="language-java">@Configuration public class AppConfig { @Value("${app.name}") private String appName; @Bean public AppService appService() { return new AppService(appName); } }</code>
The @Bean method Avoid the @Lazy
annotation method of re -loading, because it can cause unexpected results.
Agent restrictions The agency mechanism of @Bean
is not effective when the class is not final. Avoid marking the configuration class to final.
Use @Component carefully Avoid mixing and @Configuration
in the same class. This may lead to unexpected behaviors, because the
Using advanced examples of injects @Component
@Configuration
@Configuration
<code class="language-java">@Configuration @ComponentScan(basePackages = "com.example.myapp") public class AppConfig { // 必要时使用显式Bean }</code>
DataSource
JdbcTemplate
Best practice @Configuration
The above is the detailed content of spring-: @Configuration-in-depth. For more information, please follow other related articles on the PHP Chinese website!