具有私有构造函数的 Java Spring bean

发布于 2024-12-02 06:32:32 字数 71 浏览 3 评论 0原文

在 Spring 中,bean 的类是否有可能没有公共构造函数而只有私有构造函数? 创建 bean 时会调用这个私有构造函数吗?

Is it possible in Spring that a class for bean doesn't have a public constructor but only a private one?
Will this private constructor be invoked when the bean is created?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

静待花开 2024-12-09 06:32:32

是的,Spring 可以调用私有构造函数。如果它找到具有正确参数的构造函数,无论可见性如何,它将使用反射将其构造函数设置为可访问。

Yes, Spring can invoke private constructors. If it finds a constructor with the right arguments, regardless of visibility, it will use reflection to set its constructor to be accessible.

菊凝晚露 2024-12-09 06:32:32

您始终可以使用工厂方法来创建 bean,而不是依赖于默认构造函数,来自 IoC 容器:使用实例工厂方法进行实例化

<!-- the factory bean, which contains a method called createInstance() -->
<bean id="serviceLocator" class="com.foo.DefaultServiceLocator">
  <!-- inject any dependencies required by this locator bean -->
</bean>

<!-- the bean to be created via the factory bean -->
<bean id="exampleBean"
      factory-bean="serviceLocator"
      factory-method="createInstance"/>

这样做的优点是您可以为您的 bean 和依赖项使用非默认构造函数为工厂方法bean也可以被注入。

You can always use a factory method to create beans rather than relying on a default constructor, from The IoC container: Instantiation using an instance factory method:

<!-- the factory bean, which contains a method called createInstance() -->
<bean id="serviceLocator" class="com.foo.DefaultServiceLocator">
  <!-- inject any dependencies required by this locator bean -->
</bean>

<!-- the bean to be created via the factory bean -->
<bean id="exampleBean"
      factory-bean="serviceLocator"
      factory-method="createInstance"/>

This has the advantage that you can use non-default constructors for your bean, and the dependencies for the factory method bean can be injected as well.

那伤。 2024-12-09 06:32:32

是的,私有构造函数是由 spring 调用的。
考虑我的代码:

Bean 定义文件:

<bean id="message" class="com.aa.testp.Message">
        <constructor-arg index="0" value="Hi Nice"/>
    </bean>

Bean 类:

package com.aa.testp;

public class Message {

    private String message;

    private Message(String msg) {
       // You may add your log or print statements to check execution or invocation
        message = msg;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void display() {
        System.out.println(" Hi " + message);
    }

}

上面的代码工作正常。因此,spring调用了私有构造函数。

Yes, Private constructors are invoked by spring.
Consider my code:

Bean definition file:

<bean id="message" class="com.aa.testp.Message">
        <constructor-arg index="0" value="Hi Nice"/>
    </bean>

Bean class:

package com.aa.testp;

public class Message {

    private String message;

    private Message(String msg) {
       // You may add your log or print statements to check execution or invocation
        message = msg;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void display() {
        System.out.println(" Hi " + message);
    }

}

The above code works fine. Hence, spring invoked the private constructor.

假情假意假温柔 2024-12-09 06:32:32

是的 ! Spring可以访问私有构造函数。
它将像下面的代码一样在内部工作。

 try {
    Class clazz = Class.forName("A"); // A - Fully qualified class name
    Constructor constructor[] = clazz.getDeclaredConstructors();
    constructor[0].setAccessible(true);
    A a = (A) constructor[0].newInstance();
 }
 catch (Exception e) {
        e.printStackTrace();
 }

Yes ! Spring can access private constructor.
It will works internally like below code.

 try {
    Class clazz = Class.forName("A"); // A - Fully qualified class name
    Constructor constructor[] = clazz.getDeclaredConstructors();
    constructor[0].setAccessible(true);
    A a = (A) constructor[0].newInstance();
 }
 catch (Exception e) {
        e.printStackTrace();
 }
-小熊_ 2024-12-09 06:32:32

通常在此类 bean 中会有一个静态工厂方法,您可以为 spring 指定该方法来获取该 bean 的实例。请参阅 3.3.1.3 此处。 就是这样Spring推荐的,而不是违反可见性限制。

You would normally have a static factory method in such beans, you can specify that method for spring to get an instance of that bean. See 3.3.1.3 here. This is the way it's recommended by Spring, rather than going against visibility restrictions.

灼痛 2024-12-09 06:32:32

Spring 永远不会将私有构造函数作为 Bean 范围调用。如果这样做将会出现以下错误。

此问题的常见原因包括使用最终类或不可见类。嵌套异常是

java.lang.IllegalArgumentException:类中没有可见的构造函数。

Spring will never call private constructor as Bean scope. If do it will through the below error.

Common causes of this problem include using a final class or a non-visible class. Nested exception is

java.lang.IllegalArgumentException: No visible constructors in class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文