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
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()); } }
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
@Configuration public class DataConfig { @Bean public DataSource dataSource() { // 配置并返回数据源 } } @Configuration public class ServiceConfig { @Bean public UserService userService() { return new UserServiceImpl(); } }
components. @Value
@ConfigurationProperties
@Configuration public class AppConfig { @Value("${app.name}") private String appName; @Bean public AppService appService() { return new AppService(appName); } }
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()); } }
annotations. @ConfigurationProperties
@Value
@Configuration public class DataConfig { @Bean public DataSource dataSource() { // 配置并返回数据源 } } @Configuration public class ServiceConfig { @Bean public UserService userService() { return new UserServiceImpl(); } }
@Configuration
new
<误> Wrong writing: <循> Cyclone dependence
Be careful when defining mutually dependent Bean, because it will cause circulation dependence.@Configuration public class AppConfig { @Value("${app.name}") private String appName; @Bean public AppService appService() { return new AppService(appName); } }
<载> 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
@Configuration @ComponentScan(basePackages = "com.example.myapp") public class AppConfig { // 必要时使用显式Bean }
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!