【发布时间】:2014-06-30 23:20:00
【问题描述】:
我想在我的 spring 应用程序中设置一个仅带有注释的对象池。 我从 Spring 文档中的这个示例开始:http://docs.spring.io/spring/docs/2.5.x/reference/aop-api.html#aop-ts-pool。
这是我翻译 XML 配置的方法:
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class SpringObjectPoolTest {
public static void main(String[] args) throws Exception {
context = new SpringApplicationBuilder(SpringObjectPoolTest.class) //
.addCommandLineProperties(false) //
.web(false) //
.headless(false) //
.registerShutdownHook(true) //
.application() //
.run();
context.getBean(SpringObjectPoolTest.class).go();
}
@Resource(name = "pfb")
private FactoryBean<MyTask> pool;
@Resource(name="pool")
private TargetSource targetSource;
private static ConfigurableApplicationContext context;
@Bean(name = "task")
@Scope("prototype")
public MyTask createNewTask() {
return new MyTask();
}
@Bean(name = "pool")
public CommonsPoolTargetSource setupObjectPool() {
CommonsPoolTargetSource pc = new CommonsPoolTargetSource();
pc.setMaxSize(25);
pc.setTargetBeanName("task");
return pc;
}
@Bean(name = "pfb")
public ProxyFactoryBean createProxyFactoryBean() {
ProxyFactoryBean pfb = new ProxyFactoryBean();
pfb.setTargetSource(targetSource);
return pfb;
}
private void go() {
try {
pool.getObject().speak();
} catch (Exception e) {
e.printStackTrace();
}
}
}
但是我得到了这个异常:
org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'pfb':
org.springframework.beans.factory.FactoryBeanNotInitializedException:
Cannot determine target class for proxy
【问题讨论】: