[英]How are beans correctly used and what do they represent?
Good day, 美好的一天,
I am trying to use Spring framework in my java project and currently I am configuring the beans.xml file. 我正在尝试在Java项目中使用Spring框架,当前正在配置beans.xml文件。 I am new to Spring and I have difficulties on understanding how beans are used and what do they represent;
我是Spring的新手,我很难理解bean的使用方式以及它们代表什么。 I have read several documentations but still I need some clarification on this.
我已经阅读了一些文档,但是仍然需要对此进行一些说明。 Can anyone briefly explain what beans are and how are they used?
谁能简要解释什么是豆子以及如何使用它们? Let's say, I have the following classes;
假设我有以下课程; what are beans in this case?
在这种情况下,什么是豆子?
public class Repository {
private final Set<Integer> integers = new HashSet<Integer>();
private final Set<String> strings = new HashSet<String>();
public void addInt(int i) {
integers.add(i);
}
public void addString(String s) {
strings.add(s);
}
public Set<Integer> getInts() {
return integers;
}
public Set<String> getStrings() {
return strings;
}
}
and 和
public class Service {
private Repository repository;
public void createInt(int i) {
repository.addInt(i);
}
public void createString(String s) {
repository.addString(s);
}
public Set<Integer> getInts() {
return repository.getInts();
}
public Set<String> getStrings() {
return repository.getStrings();
}
}
Thank you 谢谢
If you're not forced to use XML configuration, I'd recommend to use at least Spring 4.0.0
and make use of annotation based configuration with @Configuration @Bean
. 如果您不被迫使用XML配置,建议您至少使用Spring
4.0.0
并通过@Configuration @Bean
使用基于注释的配置。
In this case, a "Bean" can be any class you want to be spring managed. 在这种情况下,“ Bean”可以是您要进行Spring管理的任何类。 A Service, a DAO, whatever.
服务,DAO,等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.