java使用AOP初始化对象
假设我有几个配置类,如下所示。它们都会自动连接一个基于不同文件路径配置的customObject,然后将其用于创建另一个bean
我对这种方法的担忧与customObject将来可能需要另一个属性(取决于config类)才能工作这一事实有关
@Configuration
public ClassA{
@Value("${A.filePath}")
private String filePath;
@Autowired
private CustomObject customObject;
@Bean
public CustomObjectWrapper something(){
customObject.set(filePath);
return CustomObjectWrapperFactory.set(customObject).build();
}
}
@Configuration
public ClassB{
@Value("${B.filePath}")
private String filePath;
@Autowired
private CustomObject customObject;
@Bean
public CustomObjectWrapper something(){
customObject.set(filePath);
return CustomObjectWrapperFactory.set(customObject).build();
}
}
@Configuration
public ClassC{
@Value("${C.filePath}")
private String filePath;
@Autowired
private CustomObject customObject;
@Bean
public CustomObjectWrapper something(){
customObject.set(filePath);
return CustomObjectWrapperFactory.set(customObject).build();
}
}
读了一些关于注释和AOP的内容后,我想知道是否有可能创建一个注释,在config类被实例化后,该注释获取config类的实例,并使用反射调用其中定义的每个自动连接属性的每个setter,并根据它们的名称将所需的属性传递给它们
最终的结果是这样的:
@Configuration
@InstantiateAutowiredObjectsAfterCreation
public ClassA{
@Value("${A.filePath}")
private String filePath;
@Autowired
private CustomObject customObject;
@Bean
public CustomObjectWrapper something(){
return CustomObjectWrapperFactory.set(customObject).build();
}
}
共 (0) 个答案