2012-08-07 83 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

相關問題