通过接口创建代理bean的正确方法是什么?
public class JdbiRepositoryAnnotationBeanPostProcessorTest {
private DBI dbi = mock(DBI.class);
@org.junit.Test
public void testIncompleteBeanDefinition() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(JdbiRepositoryAnnotationBeanPostProcessor.class);
ctx.register(MyConfig.class);
ctx.refresh();
ITest bean = ctx.getBean(ITest.class);
assertNotNull(bean);
}
@JdbiRepository
public static interface ITest {
}
@Configuration
@ComponentScan(basePackageClasses = {ITest.class},
includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = JdbiRepository.class)})
public static class MyConfig {
}
}
我尝试过 bean 后处理器,但它对我没有帮助。
编辑:
我想通过包含注释过滤器来使用组件扫描,但它也没有帮助我。
编辑:
我想通过另一个创建代理 bean 的库来创建实例,如下所示:
TestInterface proxy = factory.onDemand(TestInterface.class);
编辑:
我已经延长InstantiationAwareBeanPostProcessorAdapter
了JdbiRepositoryAnnotationBeanPostProcessor
. 我目前只是在登录。但是我看不到我的接口作为一个 bean。
请注意,我还更改了上面的测试代码。
public class JdbiRepositoryAnnotationBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware {
private ConfigurableListableBeanFactory beanFactory;
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
if(!(beanFactory instanceof ConfigurableListableBeanFactory)) {
throw new IllegalArgumentException("AutowiredAnnotationBeanPostProcessor requires a ConfigurableListableBeanFactory");
}
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
// this.dbiMap = this.beanFactory.getBeansOfType(DBI.class);
}
}