Spring Bean的公共访问修饰符和模块导出 [英] Public Access Modifier and Module Exporting for Spring Beans

查看:168
本文介绍了Spring Bean的公共访问修饰符和模块导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必须Spring bean类,Spring应用程序上下文设置的对象的类, public classes, for Spring创建这些对象?显然,如果你的 bean在不同的包中访问你的其他bean,你的bean必须是 public 才能相互交互。我的问题是所有你的bean类必须是 public

Must Spring bean classes, the classes of the objects that the Spring application context sets up, be public classes, for Spring to create those objects? Obviously, if your beans access other beans of yours in different packages, your beans will have to be public to interact with each other. My question is whether all your bean classes must be public.

作为Spring代码本身与代码所在的软件包位于不同的软件包中,从概念上讲,Spring正在做一些应该需要 public 访问你的类的东西。另一方面,Spring使用反射来创建bean,因此它可能 你的类 public

As the Spring code itself is in a different package from the package you code is in, conceptually Spring is doing something that ought to require public access to your classes. On the other hand, Spring is using reflection to create the beans, so it probably does not need your classes to be public.

除了包访问类型之外,从Java 9我们还有Java模块。这些类是否需要公开并从模块中导出,如果你把它们放在一个模块中?

It addition to the package access types, from Java 9 we have Java Modules. Do the classes need to be public and exported from your module, if you put them in a module?

推荐答案

不,不是全部类必须是 public 。 Spring可以像你提到的那样使用反射来实例化包私有类而没有任何问题。

No, not all classes have to be public. Spring can instantiate package-private classes using reflection like you mentioned without any problems..

如果包 - 私有bean由IoC容器管理并由类中的类使用同样的包装没问题。只有在尝试将bean连接到包时才会出现问题。当然这很明显。

If a package-private bean is managed by the IoC container and used by classes in the same package it's no problem. Problems only arise when you try to wire that bean across packages. Which is quite obvious of course.

这篇关于Spring Bean的公共访问修饰符和模块导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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