springBoot加载
@Configuration
表明该类是一个配置类常常配合@Bean使用,让容器管理对象
@Configuration(proxyBeanMethods = true)
proxyBeanMethods = true表示@Configuration注解的类走不走代理,如果走代理会从单例池拿对象,拿到的对象是同一个,不走代理不会从单例池拿对象
@Configuration
public class Appconfig {
@Bean
public OrderService oderService(){
return new OrderService();
}
}
@Bean
将指定类交给spring容器管理
@SpringBootApplication
public class TestSpringBootAnnoApplication {
@Bean
public UserService userService(){
return new UserService();
}
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(TestSpringBootAnnoApplication.class, args);
UserService userService = context.getBean(UserService.class);
System.out.println(userService);
}
}
@Scope
指定交给spring容器管理的bean对象是单例或是多例的
@Scope("singleton")单例(默认) @Scope("prototype")多例
@SpringBootApplication
public class TestSpringBootAnnoApplication {
@Bean
@Scope("prototype")
public UserService userService(){
return new UserService();
}
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(TestSpringBootAnnoApplication.class, args);
UserService userService = context.getBean(UserService.class);
UserService userService2 = context.getBean(UserService.class);
System.out.println(userService==userService2);
}
}
spring.factories
springboot在启动时会先寻找resource/META-INFO文件夹下有没有spring.factories文件,如果有就会解析,注入相应的类
META-INFO
#Auto
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.testspringbootanno.service.UserService
@ComponentScan
定义spring容器扫描的路径,扫描的spring注解才会被解析
@Profile
指定注入到spring容器的Bean生效的范围
spring.profiles.active=dev
@SpringBootApplication
public class TestSpringBootAnnoApplication {
@Bean
@Scope("prototype")
@Profile("dev")
public UserService userService(){
return new UserService();
}
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(TestSpringBootAnnoApplication.class, args);
UserService userService = context.getBean(UserService.class);
UserService userService2 = context.getBean(UserService.class);
System.out.println(userService==userService2);
}
}
@ConditionalOnBean
满足spring容器有某个springBean对象条件时把该对象交给spring管理。
@Configuration
public class Appconfig {
@Bean
public OrderService oderService(){
return new OrderService();
}
@Bean
@ConditionalOnBean(name = "oderService")
public UserService userService(){
return new UserService();
}
}
@Configuration
public class Appconfig {
@Bean
public OrderService oderService(){
return new OrderService();
}
@Bean
@ConditionalOnBean(OrderService.class)
public UserService userService(){
return new UserService();
}
}
@ConditionalOnMissingBean
满足spring容器没有某个springBean对象条件时把该对象交给spring管理。
@Configuration
public class Appconfig {
//@Bean
public OrderService oderService(){
return new OrderService();
}
@Bean
@ConditionalOnMissingBean(OrderService.class)
public UserService userService(){
return new UserService();
}
}
@ConditionalOnSingleCandidate
是否符合指定的类型只有一个
@Configuration
public class Appconfig {
@Bean
public OrderService oderService(){
return new OrderService();
}
@Bean
public OrderService oderService1(){
return new OrderService();
}
@Bean
@ConditionalOnSingleCandidate(OrderService.class)
public UserService userService(){
return new UserService();
}
}
@ConditionalOnClass
存在某个class生效
@Configuration
public class Appconfig {
@Bean
public OrderService oderService(){
return new OrderService();
}
@Bean
@ConditionalOnClass(name = "com.example.testspringbootanno.service.OrderService")
public UserService userService(){
return new UserService();
}
}
@ConditionalOnMissingClass
不存在某个class生效
@Configuration
public class Appconfig {
@Bean
public OrderService oderService(){
return new OrderService();
}
@Bean
@ConditionalOnMissingClass("com.example.testspringbootanno.service.OrderService")
public UserService userService(){
return new UserService();
}
}
@ConditionalOnExpression
根据表达式结果判断是否生效
@Configuration
public class Appconfig {
@Bean
public OrderService oderService(){
return new OrderService();
}
@Bean
@ConditionalOnExpression("${test.condition}==true")
public UserService userService(){
return new UserService();
}
}
@ConditionalOnJava
指定的java版本生效
@Configuration
public class Appconfig {
@Bean
public OrderService oderService(){
return new OrderService();
}
@Bean
@ConditionalOnJava(JavaVersion.ELEVEN)
public UserService userService(){
return new UserService();
}
}
@ConditionalOnWebApplication
Web环境生效
@ConditionalOnNotWebApplication
不是Web环境生效
@ConditionalOnProperty
判断配置文件中某个key是否存在
test.condition=true
@Configuration
public class Appconfig {
@Bean
public OrderService oderService(){
return new OrderService();
}
@Bean
@ConditionalOnProperty(name = "test.condition")
//@ConditionalOnProperty(name = "test1.condition",matchIfMissing = true) 确实了照样匹配
public UserService userService(){
return new UserService();
}
}
@ConditionalOnResource
某个资源存在时生效、
@Configuration
public class Appconfig {
@Bean
public OrderService oderService(){
return new OrderService();
}
@Bean
@ConditionalOnResource(resources = "http://www.baidu.com")
public UserService userService(){
return new UserService();
}
}
@ConditionalOnWarDeployment
应用是war包时生效
@ConditionalOnCloudPlatform
应用在某个云平台时生效
读取配置文件
方法一:
@Component
@ConfigurationProperties(prefix = "user")
public class Aconfig {
private String username;
private String password;
}
方法二:
@ConfigurationProperties(prefix = "user")
public class Aconfig {
private String username;
private String password;
}
@Configuration
@EnableConfigurationProperties(Aconfig.class)
public class Appconfig {
}
还可以扫描,用于properties文件过多统一管理
@Configuration
@ConfigurationPropertiesScan("com.zhuoyue.properties")
public class Appconfig {
}
配置文件加载顺序
JVM环境变量(Envionment variables)>电脑环境变量>application.properties>application.yml
标签:return,SpringBoot,class,Bean,OrderService,UserService,注解,public,加载 From: https://www.cnblogs.com/WangJingjun/p/17004565.html