[英]Are Java classes in Spring automatically a @Bean?
If I create some classes, can I automatically @Autowire
those classes somewhere else without using the @Bean
, @Component
, @Service
?如果我创建了一些类,我可以在不使用@Bean
、 @Component
、 @Service
的情况下自动@Autowire
这些类吗? Or if I want to @Autowire
a class somewhere else, do I need to use the annotations described before?或者如果我想在其他地方@Autowire
一个 class,我需要使用前面描述的注释吗?
I want to @Autowire
a class to another that uses the @Configuration
and @ComponentScan
annotation我想@Autowire
一个 class 到另一个使用@Configuration
和@ComponentScan
注释的
For example:例如:
public class SomeClass{
//do Something
}
or或者
@Bean
public class SomeClass{
//do Something
}
to achieve this..为达到这个..
@Configuration
@ComponentScan("some.package")
public class SomeOtherClass{
@Autowire
private SomeClass someClass;
}
If i create some class, can i automatically @Autowire that class somewhere else without using the @Bean, @Component, @Service如果我创建了某个类,是否可以在不使用 @Bean、@Component、@Service 的情况下在其他地方自动 @Autowire 该类
No不
You can read more about creating Spring beans .您可以阅读有关创建 Spring bean 的更多信息。
The short answer is No , as you need to inform Spring as to which classes are meant to be "components" and which ones aren't.简短的回答是No ,因为您需要通知 Spring 哪些类是“组件”,哪些不是。 If this wasn't the case, it would become all to easy to initialise as components classes that are not meant for that purpose, causing all sorts of problems.如果不是这种情况,将变得很容易初始化为不用于该目的的组件类,从而导致各种问题。
If you don't want to put a @Component
annotation on your classes, however (say because you're building a library that is supposed to be framework-agnostic and you don't want to carry around dependencies to Spring), the users of your class will indeed have other options to initialise your classes and turn them into @Component
instances.但是,如果您不想在@Component
上添加@Component
注释(比如因为您正在构建一个应该与框架无关的库,并且您不想将依赖项带到 Spring),则用户您的类确实会有其他选项来初始化您的类并将它们转换为@Component
实例。
One option is to use XML configuration:一种选择是使用 XML 配置:
https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/xsd-configuration.html https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/xsd-configuration.html
Another option is to create instances of your beans within classes annotated with @Configuration
:另一种选择是在用@Configuration
注释的类中创建 bean 的实例:
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html
In the latter case, what you do is create a method (in which you will create a new instance of your class) and annotate it with `@Bean'.在后一种情况下,您要做的是创建一个方法(您将在其中创建类的新实例)并使用“@Bean”对其进行注释。
Both approaches are valid and are used across all sorts of applications, my preference leaning towards the @Configuration
annotation variant.这两种方法都是有效的,可用于各种应用程序,我更倾向于使用@Configuration
注释变体。
If you are 100% certain that the package in question will contain ONLY classes meant to be initialised as beans, you can write a method to scan the classes in the package, create instances for each class and register them as beans.如果您 100% 确定有问题的包将仅包含旨在初始化为 bean 的类,您可以编写一个方法来扫描包中的类,为每个类创建实例并将它们注册为 bean。 I must say however that I don't see a valid use-case for such approach that, in fact, has probably more risks than benefits.然而,我必须说,我没有看到这种方法的有效用例,实际上,这种方法的风险可能大于收益。
Bean is Java object living inside the Spring Container (Application Context). Bean 是存在于 Spring Container(应用程序上下文)中的 Java 对象。
Class is not a bean.类不是bean。 Class will become the candidate of becoming bean, if you declare that either in XML or an annotation way ( @Component
, @Service
, @Controller
or etc.).类将成为成为 bean 的候选者,如果您以 XML 或注释方式( @Component
、 @Service
@Controller
、 @Service
@Controller
等)声明。 When Spring Container starts, it will scan for all the candidate-bean classes, and if everything is OK, candidates will become beans, meaning that they will be Java objects living inside a Spring Container .当 Spring Container 启动时,它会扫描所有候选 bean 类,如果一切正常,候选 bean 将成为 bean,这意味着它们将是居住在 Spring Container 中的 Java 对象。 By default, they will be singletons.默认情况下,它们将是单身人士。
Think that way: You have a Math exam and are not ready for the exam.这样想:你有一个数学考试,还没有准备好考试。 So you decided to prepare some cheatSheet to remember some stuff during the exam.所以你决定准备一些备忘单来在考试期间记住一些东西。
@Component
public class CheetSheet{
//Some code to prepare Cheatsheet
}
Let's say we have 2 Spring Classes假设我们有 2 个 Spring 类
@Component
public class Exam{
// some code here
}
You have Cheetsheet and Exam Spring Beans but what about preparing the piece of the sheet?你有 Cheetsheet 和 Exam Spring Beans 但是准备这张表呢? So you may need helper class that it will read things from file(resources) and will give you the exact things you have problem(or you need)因此,您可能需要帮助器 class 它将从文件(资源)中读取内容并为您提供您遇到问题(或您需要)的确切内容
//Helper class
public class HelpToPrepare{
public String ReadFromFile(someList){
// I am reading from a file method
return listOfData;
}
//I am sorting data and collecting the important ones that I have problem
public String sortAndSave(){
readFromFile();
//some code for sort
//some code for save
return saveList;
}
So, the last class will not be part of Spring but it will help to sort out things or keep things that you will use them when you need.因此,最后一个 class 不会成为 Spring 的一部分,但它有助于整理或保留您需要时使用的东西。
Hope it will help希望它会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.