FactoryBeanNotInitializedException:无法确定代理的目标类

时间:2014-06-30 23:20:00

标签: java spring pool

我想在我的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

1 个答案:

答案 0 :(得分:0)

你有点过于设计这个。 Spring已经知道如何注入代理MyTask,不需要FactoryBean<MyTask>或在池上调用getObject()。在&#34; pooledTask&#34;下面Spring知道通过注入ProxyFactoryBean(&#34; pfb&#34;)它实际上会注入工厂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();
        }
    }
}