提交 8a1f6d94 编写于 作者: 武汉红喜's avatar 武汉红喜

beans test

上级 f7a1c9f8
package org.hongxi.whatsmars.boot.sample.bean;
package org.hongxi.whatsmars.boot.sample.beans;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
......
package org.hongxi.whatsmars.boot.sample.beans;
/**
* Created by shenhongxi on 2020/6/23.
*/
public class Planet {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package org.hongxi.whatsmars.boot.sample.bean;
package org.hongxi.whatsmars.boot.sample.beans.autoconfigure;
import org.hongxi.whatsmars.boot.sample.beans.*;
import org.hongxi.whatsmars.boot.sample.beans.condition.OrCondition;
import org.hongxi.whatsmars.boot.sample.beans.postprocessor.DemoBeanPostProcessor;
import org.hongxi.whatsmars.boot.sample.beans.register.PlanetBeanDefinitionRegistryPostProcessor;
import org.hongxi.whatsmars.boot.sample.beans.register.PlanetBeanDefinitionRegistryPostProcessor2;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import java.util.ArrayList;
import java.util.List;
/**
* Created by shenhongxi on 2020/6/22.
......@@ -39,4 +49,27 @@ public class ApplicationConfiguration {
public ConditionalBean conditionalBean() {
return new ConditionalBean();
}
@Bean
@ConditionalOnProperty(name = "planet.names[0]")
public PlanetBeanDefinitionRegistryPostProcessor planetBeanDefinitionRegistryPostProcessor(Environment environment) {
return new PlanetBeanDefinitionRegistryPostProcessor(parseNames(environment));
}
@Bean
@ConditionalOnProperty(name = "planet.names[0]")
public PlanetBeanDefinitionRegistryPostProcessor2 planetBeanDefinitionRegistryPostProcessor2(Environment environment) {
return new PlanetBeanDefinitionRegistryPostProcessor2(parseNames(environment));
}
public List<String> parseNames(Environment environment) {
List<String> names = new ArrayList<>();
String configsKey = "planet.names[%d]";
int configIndex = 0;
String name;
while ((name = environment.getProperty(String.format(configsKey, configIndex++))) != null) {
names.add(name);
}
return names;
}
}
package org.hongxi.whatsmars.boot.sample.beans.autoconfigure;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
/**
* Created by shenhongxi on 2020/6/23.
*/
@ConfigurationProperties(prefix = "planet")
public class PlanetProperties {
private List<String> names;
public List<String> getNames() {
return names;
}
public void setNames(List<String> names) {
this.names = names;
}
}
package org.hongxi.whatsmars.boot.sample.beans.autoconfigure;
import org.hongxi.whatsmars.boot.sample.beans.Planet;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
/**
* Created by shenhongxi on 2020/6/23.
*/
@EnableConfigurationProperties(PlanetProperties.class)
@Configuration
public class PlanetRegisterConfiguration implements InitializingBean, ApplicationContextAware {
private ConfigurableApplicationContext applicationContext;
@Autowired
private PlanetProperties planetProperties;
@Override
public void afterPropertiesSet() throws Exception {
planetProperties.getNames().forEach(name -> {
BeanDefinitionBuilder beanBuilder = BeanDefinitionBuilder.rootBeanDefinition(Planet.class);
beanBuilder.addPropertyValue("name", name);
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getBeanFactory();
beanFactory.registerBeanDefinition("planet3_" + name, beanBuilder.getBeanDefinition());
Planet planet = beanFactory.getBean("planet3_" + name, Planet.class);
System.out.println(planet.getName());
});
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = (ConfigurableApplicationContext) applicationContext;
}
}
package org.hongxi.whatsmars.boot.sample.bean;
package org.hongxi.whatsmars.boot.sample.beans.condition;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
......
package org.hongxi.whatsmars.boot.sample.bean;
package org.hongxi.whatsmars.boot.sample.beans.environment;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
......
package org.hongxi.whatsmars.boot.sample.beans.factory;
import org.hongxi.whatsmars.boot.sample.beans.Planet;
/**
* Created by shenhongxi on 2020/6/23.
*/
public class PlanetFactory {
public static Planet createPlanet(String name) {
Planet planet = new Planet();
planet.setName(name);
return planet;
}
}
package org.hongxi.whatsmars.boot.sample.beans.factory;
import org.hongxi.whatsmars.boot.sample.beans.Planet;
import org.springframework.beans.factory.FactoryBean;
/**
* Created by shenhongxi on 2020/6/23.
*/
public class PlanetFactoryBean implements FactoryBean<Planet> {
private String name;
public PlanetFactoryBean(String name) {
this.name = name;
}
@Override
public Planet getObject() throws Exception {
Planet planet = new Planet();
planet.setName(name);
return planet;
}
@Override
public Class<?> getObjectType() {
return Planet.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
package org.hongxi.whatsmars.boot.sample.bean;
package org.hongxi.whatsmars.boot.sample.beans.postprocessor;
import org.hongxi.whatsmars.boot.sample.beans.DemoBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
......
package org.hongxi.whatsmars.boot.sample.beans.register;
import org.hongxi.whatsmars.boot.sample.beans.Planet;
import org.hongxi.whatsmars.boot.sample.beans.factory.PlanetFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import java.util.List;
/**
* Created by shenhongxi on 2020/6/23.
*/
public class PlanetBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
private List<String> names;
public PlanetBeanDefinitionRegistryPostProcessor(List<String> names) {
this.names = names;
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
names.forEach(name -> {
BeanDefinitionBuilder beanBuilder = BeanDefinitionBuilder.rootBeanDefinition(PlanetFactory.class);
beanBuilder.setFactoryMethod("createPlanet");
beanBuilder.addConstructorArgValue(name);
registry.registerBeanDefinition("planet_" + name, beanBuilder.getRawBeanDefinition());
});
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
names.forEach(name -> {
Planet planet = beanFactory.getBean("planet_" + name, Planet.class);
System.out.println(planet.getName());
});
}
}
package org.hongxi.whatsmars.boot.sample.beans.register;
import org.hongxi.whatsmars.boot.sample.beans.Planet;
import org.hongxi.whatsmars.boot.sample.beans.factory.PlanetFactoryBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import java.util.List;
/**
* Created by shenhongxi on 2020/6/23.
*/
public class PlanetBeanDefinitionRegistryPostProcessor2 implements BeanDefinitionRegistryPostProcessor {
private List<String> names;
public PlanetBeanDefinitionRegistryPostProcessor2(List<String> names) {
this.names = names;
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
names.forEach(name -> {
GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
genericBeanDefinition.setBeanClass(PlanetFactoryBean.class);
ConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues();
constructorArgumentValues.addIndexedArgumentValue(0, name);
genericBeanDefinition.setConstructorArgumentValues(constructorArgumentValues);
registry.registerBeanDefinition("planet2_" + name, genericBeanDefinition);
});
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
names.forEach(name -> {
Planet planet = beanFactory.getBean("planet2_" + name, Planet.class);
System.out.println(planet.getName());
});
}
}
org.springframework.boot.env.EnvironmentPostProcessor=\
org.hongxi.whatsmars.boot.sample.bean.EnvironmentProcessor
\ No newline at end of file
org.hongxi.whatsmars.boot.sample.beans.environment.EnvironmentProcessor
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册