初衷:我们希望通过配置文件动态的生成多个Bean对象,后面可以通过aop直接获取配置文件中值,具体做法如下:
application.yml
test:
mapping:
key1: value1
key2: value2
BeanProperties.java
package;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.Map;
@Configuration
@ConfigurationProperties(prefix = "test")
public class BeanProperties {
private Map<String, String> mapping;
public Map<String, String> getMapping() {
return mapping;
}
public void setMapping(Map<String, String> mapping) {
this.mapping = mapping;
}
}
Config.java
package;
public class Config {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
WebServiceDynamicConfig.java
package;
import com.example.config.BeanProperties;
import com.example.config.Config;
import lombok.SneakyThrows;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;
import java.util.Map;
import java.util.Objects;
@Configuration
@EnableConfigurationProperties(BeanProperties.class)
@Import(WebServiceDynamicConfig.ImportConfig.class)
public class WebServiceDynamicConfig {
public static class ImportConfig implements ImportBeanDefinitionRegistrar, EnvironmentAware {
private BeanProperties beanProperties;
@SneakyThrows
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
Map<String, String> mapping = beanProperties.getMapping();
for (Map.Entry<String, String> entry : beanProperties.getMapping().entrySet()) {
// 注册bean
RootBeanDefinition beanDefinition = new RootBeanDefinition();
beanDefinition.setBeanClass(Config.class);
MutablePropertyValues values = new MutablePropertyValues();
values.addPropertyValue("value", entry.getValue());
beanDefinition.setPropertyValues(values);
beanDefinitionRegistry.registerBeanDefinition(entry.getKey(), beanDefinition);
}
}
@Override
public void setEnvironment(Environment environment) {
// 通过Binder将environment中的值转成对象
beanProperties = Binder.get(environment).bind(getPropertiesPrefix(BeanProperties.class), BeanProperties.class).get();
}
private String getPropertiesPrefix(Class<?> tClass) {
return Objects.requireNonNull(AnnotationUtils.getAnnotation(tClass, ConfigurationProperties.class)).prefix();
}
}
}
TestController.java
package;
import com.example.config.BeanProperties;
import com.example.config.Config;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequiredArgsConstructor
public class TestController {
private final BeanProperties beanProperties;
@Resource(name = "key1")
private Config key1Config;
@Resource(name = "key2")
private Config key2Config;
@GetMapping("/config")
public String config() {
return beanProperties.getMapping().toString();
}
@GetMapping("key1")
public String key1() {
return key1Config.getValue();
}
@GetMapping("key2")
public String key2() {
return key2Config.getValue();
}
}
在浏览器访问:http://自己的ip:端口/xxx/config
输出结果:
{key1=value1, key2=value2}
在浏览器访问:http://自己的ip:端口/xxx/key1
输出结果:
value1
在浏览器访问:http://自己的ip:端口/xxx/key2
输出结果:
value2