
@Configuration(proxyBeanMethods = true)
@Configuration(proxyBeanMethods = false)
组件类
//用户类
public class User {
private Pet pet;
public User(Pet pet) {
this.pet = pet;
}
public Pet getPet() {
return pet;
}
@PostConstruct
public void init() {
System.out.println("user 初始化。。。");
}
}
//宠物类
public class Pet {
@PostConstruct
public void init() {
System.out.println("pet 初始化。。。");
}
}配置类
@Configuration(proxyBeanMethods = true)
public class MyConfig {
@Bean
public User user(){
Pet pet = pet();
Pet pet1 = pet();
return new User(pet);
}
@Bean
public Pet pet(){
return new Pet();
}
}启动类
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
//获取ioc容器
ConfigurableApplicationContext context =
SpringApplication.run(SpringbootApplication.class, args);
//获取组件
User user1 = context.getBean("user", User.class);
User user2 = context.getBean("user", User.class);
Pet pet1 = context.getBean("pet", Pet.class);
Pet pet2 = context.getBean("pet", Pet.class);
System.out.println(user1==user2);
System.out.println(pet1==pet2);
System.out.println(user1.getPet()==pet1);
}
}当proxyBeanMethods =true


当proxyBeanMethods =false


所以,如果配置类中的@Bean标识的方法之间不存在依赖调用的话,可以设置为false,可以避免拦截方法进行代理操作,也是提升性能的一种优化。但是需要注意,@Bean标识的返回值对象还是会放入到容器中的,从容器中获取bean还是可以是单例的,会走生命周期。
@Configuration
@ComponentScan("spring.module.annotation")
public class MainConfig {
@Bean
@Scope(value = "prototype")
public Person person(){
return new Person(1,"张三");
}
}@Configuration
@ComponentScan("spring.module.annotation")
public class MainConfig {
@Bean
@Lazy
public Person person(){
return new Person(1,"张三");
}
}@Configuration
@ComponentScan("spring.module.annotation")
public class MainConfig {
@Conditional({WindowsCondition.class})
@Bean("lilei")
public Person person01(){
return new Person(10,"李雷");
}
@Conditional({LinuxCondition.class})
@Bean("hanmeimei")
public Person person02(){
return new Person(20,"韩梅梅");
}
}加载条件
//判断是否linux系统
public class LinuxCondition implements Condition {
/**
*ConditionContext:判断条件能使用的上下文
*AnnotatedTypeMetadata:注释信息
*/
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//1、获取ioc使用的beanFactory
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
//2.获取类加载器
ClassLoader classLoader = context.getClassLoader();
//3.获取当前环境信息
Environment environment = context.getEnvironment();
//4.获取bean定义的注册类(注册、移除、获取查看等)
BeanDefinitionRegistry registry = context.getRegistry();
//linux返回true,否则返回false
String property = environment.getProperty("os.name");
return property.contains("linux") ? true : false;
}
}//判断是否windows系统
public class WindowsCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//linux返回true,否则返回false
String property = context.getEnvironment().getProperty("os.name");
return property.contains("Windows") ? true : false;
}
}容器中自动注册组件,id默认全类名
@Configuration
@ComponentScan("spring.module.annotation")
@Import({Red.class, Blue.class})
public class MainConfig {
}打印ioc容器中所有组件
public class Client {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
for (String beanName : context.getBeanDefinitionNames()) {
System.out.println(beanName);
}
}
}结果:
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
spring.module.annotation.entity.Red
spring.module.annotation.entity.Blue返回多个组件的全类名数组
@Configuration
@ComponentScan("spring.module.annotation")
@Import({MyImportSelector.class})
public class MainConfig {
}自定义逻辑返回需要导入的组件
public class MyImportSelector implements ImportSelector {
/**
* 返回值:导入容器的组件数组,id为全类名
*AnnotationMetadata:标注此注解的类上面的所有注解信息
*/
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"spring.module.annotation.entity.Black", "spring.module.annotation.entity.Yellow"};
}
}三个注解信息分别为:@Configuration、@ComponentScan、@Import

结果:
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
spring.module.annotation.entity.Black
spring.module.annotation.entity.Yellow手动注册bean到容器中
@Configuration
@ComponentScan("spring.module.annotation")
@Import({MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
public class MainConfig {
}自定义手工注册
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
/**
* AnnotationMetadata:此注解类的所有注解信息
* BeanDefinitionRegistry:BeanDefinition注册类
* 通过BeanDefinitionRegistry.registerBeanDefinition手工注册bean
*/
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
boolean exist = registry.containsBeanDefinition("spring.module.annotation.entity.Black");
//如果存在Black的bean,则会注册White的bean
if(exist){
//指定bean名
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(White.class);
registry.registerBeanDefinition("white",rootBeanDefinition);
}
}
}结果:
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
spring.module.annotation.entity.Black
spring.module.annotation.entity.Yellow
white@PropertySource(value={"classpath:/jdbc.properties"})
@Configuration
public class MyConfig(){
}