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

标签 java spring apache-camel

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

主要问题是,我可以为 3 个类创建 bean 这 3 个类具有与 getInstance 方法相似的结构,并且 一个 private constructor() (单例类)但是第四个也是最后一个 正在抛出异常(异常消息粘贴在下面)

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

但是如果我将构造函数从“Private”更改为“Public”,那么 我没有得到错误。任何人都可以了解正在发生的事情吗?由于其他三个类具有私有(private)构造函数并且它们工作得很好

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)构造函数

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)
:

最佳答案

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

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

关于java - 具有私有(private)构造函数的 Singleton 类的 Spring(创建 bean 时出错,没有可见的构造函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11847395/

相关文章:

spring - MQTT Apache Camel 路由错误导致应用程序停止

java - 使用反射,获取对象的父类/字段

java - Spring 中的嵌套传播

java - 如何获取要保存到 MDC 的请求 header

java - apache Camel 混合组件

java - 如何在 Apache Camel 中记录 csv 的内容?

java - 无法通过 URLConnection 或 HttpsURLConnection 访问 Maven 存储库 (403)

java - 均匀分割整数

java - openweathermap api 获取今天和其余的工作日?

java - Apache Camel : Ambiguous method invocations possible : JmsMessage