2012-08-07 85 views
2

我有4个单身类与私人构造函数 和我想创建所有4类的bean属性。春天(错误创建bean,没有可见的构造函数)与私有构造函数的单身类

的主要问题是,我可以为3类 和这3类创建bean有一个getInstance方法和 私有构造函数()(Singleton类),但第四和最后一个类似的结构 抛出异常(异常消息粘贴在下面)

请在下面找到getInstance方法,私有构造函数 和bean id声明。在所有四个bean声明中都是相同的

但是,如果我将构造函数从“Private”更改为“Public”,则 我没有收到错误。任何人都可以对发生的事情有所了解吗?由于其他三个类有私有构造函数和他们的工作完全正常

的getInstance()方法

public static ApplicationConfiguration getInstance() throws IOException, 
      IllegalArgumentException, InconsistentDataException { 
     ApplicationConfiguration result = instance.get(); 
     if (result == null) { 
      try { 
       // Check again if already created 
       result = instance.get(); 
       if (result == null) { 
        result = new ApplicationConfiguration(); 

       } 
      } finally { 
       // something here 
      } 
     } 
     return result; 
    } 

的私有构造

private ApplicationConfiguration() throws Exception { 
     // call a method here 
    } 

bean属性声明

<bean id="configManager" class="com.manager.ApplicationConfiguration" factory-method="getInstance" /> 

<bean id="configEnricher" class="com.enricher.ApplicationConfiguration" factory-method="getInstance" /> 

<bean id="configBussiness" class="com.validationservice.ApplicationConfiguration" factory-method="getInstance" /> 

以上三件作品

这个bean属性引发错误

<bean id="configEviction" class="com.evictionservice.ApplicationConfiguration" factory-method="getInstance" /> 

异常消息

[#|2012-08-07 11:53:21,130|ERROR|RMI TCP Connection(226)-172.18.36.14|org.springframework. 
web.context.ContextLoader||slodev-rhngp5.mblox.com|core-1|Context initialization failed|#] 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'co 
nfigEviction' defined in ServletContext resource [/WEB-INF/camel-context.xml]: Initializat 
ion of bean failed; nested exception is org.springframework.aop.framework.AopConfigExcepti 
on: Could not generate CGLIB subclass of class [class com.evictionservice.ApplicationConfiguration]: 
Common causes of this problem include using 
a final class or a non-visible class; nested exception is java.lang.IllegalArgumentExcepti 
on: No visible constructors in class com.evictionservice.ApplicationConfiguration 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.do 
CreateBean(AbstractAutowireCapableBeanFactory.java:526) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.cr 
eateBean(AbstractAutowireCapableBeanFactory.java:455) 
     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(Abstr 
actBeanFactory.java:293) 
     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingl 
eton(DefaultSingletonBeanRegistry.java:222) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstrac 
tBeanFactory.java:290) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractB 
eanFactory.java:192) 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstant 
iateSingletons(DefaultListableBeanFactory.java:585) 
     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactor 
yInitialization(AbstractApplicationContext.java:895) 
     at org.springframework.context.support.AbstractApplicationContext.refresh(Abstract 
ApplicationContext.java:425) 
: 
+0

你对com.evictionservice.ApplicationConfiguration类或其任何方法有任何注释 - 比如@Transaction等。 – 2012-08-07 14:11:25

+0

看起来这里有4个不同的类(所有不同的包)。每个ApplicationConfiguration是否扩展一个通用接口?这是什么样子?在你的spring上下文中有什么东西在com.evictionservice类上代理? – 2012-08-07 15:55:51

回答

4

问题不在于创造豆本身(因为你已经注意到了,这不是不同于其他豆类)。这个问题似乎与您尝试使用的某些AOP配置有关。如果你想为这个类创建一个代理,它不能用CGLIB来完成,因为这个类不能被子类化(因为它有一个私有的构造函数)。

解决此问题的唯一方法是(根据当前设计)创建一个接口,该接口将由ApplicationConfiguration类实现,然后为该接口创建代理,而不是代理该类。

+0

谢谢你的回复,但我不确定你正在谈论的是哪些是AOP配置,创建一个代理和接口。已经全部四个ApplicationConfiguration类都有一个接口,并且如何为接口创建代理?我应该在哪里做一些改变和对改变的提示?由于其他三个班级工作正常,我真的不明白发生了什么事情。 – likeToCode 2012-08-07 14:13:52

+2

在应用程序上下文中是否有类似于 2012-08-07 14:26:38

+0

是的,你是对的,在经过我的骆驼环境之后,我偶然发现了这个标签(),它指的是(最后一个singleton类的)项目。我删除了依赖关系,并且创建bean时没有任何问题。但是我想知道您正在使用的代理服务器以及有关AOP配置和这些代理服务器的任何链接的明确解释?我可以在网上搜索,但总是听到有经验的人的好消息。 – likeToCode 2012-08-07 15:43:32

相关问题