带有私有构造函数的 Java Spring bean [英] Java Spring bean with private constructor

查看:53
本文介绍了带有私有构造函数的 Java Spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is possible in Spring that class for bean doesn't have public constructor but only private ? Will this private constructor invoked when bean is created? Thanks.

推荐答案

是的,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.

这篇关于带有私有构造函数的 Java Spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆