具有私有构造函数的Singleton类的Spring(创建bean,没有可见构造函数时出错)

lik*_*ode 3 java spring apache-camel

我有4个带有私有构造函数的单例类,我正在尝试为所有4个类创建bean属性.

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

请在下面找到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;
    }
Run Code Online (Sandbox Code Playgroud)

私有构造函数

private ApplicationConfiguration() throws Exception {
        // call a method here
    }
Run Code Online (Sandbox Code Playgroud)

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" />
Run Code Online (Sandbox Code Playgroud)

以上三件作品

这个bean属性抛出错误

<bean id="configEviction" class="com.evictionservice.ApplicationConfiguration" factory-method="getInstance" />
Run Code Online (Sandbox Code Playgroud)

异常消息

[#|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)
:
Run Code Online (Sandbox Code Playgroud)

Cos*_*atu 5

问题不在于bean创建本身(正如您已经注意到的那样,它与其他bean没有区别).该问题似乎与您尝试使用的某些AOP配置有关.如果要为该类创建代理,则无法使用CGLIB执行此操作,因为该类无法进行子类化(因为它具有私有构造函数).

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

  • 在应用程序上下文中是否有类似`<aop:...`或`<tx:...`的内容或者应用了任何`@Transactional`注释?如果这些方面中的任何一个(以及其他一些方面)涉及该类,则应用程序上下文将尝试为其创建代理. (3认同)