Java FactoryBeanNotInitializedException:无法确定代理的目标类
我想在我的spring应用程序中设置一个只带有注释的对象池。 我从Spring文档中的这个示例开始: 以下是我如何转换XML配置:Java FactoryBeanNotInitializedException:无法确定代理的目标类,java,spring,pool,Java,Spring,Pool,我想在我的spring应用程序中设置一个只带有注释的对象池。 我从Spring文档中的这个示例开始: 以下是我如何转换XML配置: @Configuration @EnableAutoConfiguration @ComponentScan public class SpringObjectPoolTest { public static void main(String[] args) throws Exception { context = new SpringApp
@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();
}
}
}
你设计得有点过头了。Spring已经知道如何注入代理的
MyTask
,不需要有FactoryBean
或调用池上的getObject()
。在下面的“pooledTask”中,Spring知道,通过注入ProxyFactoryBean
(“pfb”),它实际上将注入工厂bean创建的实例,而不是工厂bean本身。我是这样做的:
@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();
}
private static ConfigurableApplicationContext context;
@Resource(name = "pfb")
private MyTask pooledTask;
@Resource(name="pool")
private CommonsPoolTargetSource targetSource;
@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(setupObjectPool());
return pfb;
}
private void go() {
try {
pooledTask.speak();
// getting another object from pool
MyTask someOtherTask = (MyTask) targetSource.getTarget();
// returning the object to the pool
targetSource.releaseTarget(someOtherTask);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如何从池中获取多个对象并释放它们?您需要为此使用上下文:
MyTask task=(MyTask)context.getBean(“pfb”)代码>。每次调用getBean
都会从池中借用一个对象。要释放它们,您需要持有pool对象并对其调用releaseTarget
。是否可以使用持有来借用对象而不是上下文?请参阅我的更新答案。我定义了一个要注入的新@Resource(name=“pool”)
,并在“go()”方法中使用了它。
@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();
}
private static ConfigurableApplicationContext context;
@Resource(name = "pfb")
private MyTask pooledTask;
@Resource(name="pool")
private CommonsPoolTargetSource targetSource;
@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(setupObjectPool());
return pfb;
}
private void go() {
try {
pooledTask.speak();
// getting another object from pool
MyTask someOtherTask = (MyTask) targetSource.getTarget();
// returning the object to the pool
targetSource.releaseTarget(someOtherTask);
} catch (Exception e) {
e.printStackTrace();
}
}
}