我还没有找到我能够理解的Spring bean的高级定义。我经常在Grails文档和书籍中看到它们,但我认为了解它们是什么将是有益的。那么春豆是什么呢?如何使用它们?它们与依赖注入有关吗?
当前回答
For Spring, all objects are beans! The fundamental step in the Spring Framework is to define your objects as beans. Beans are nothing but object instances that would be created by the spring framework by looking at their class definitions. These definitions basically form the configuration metadata. The framework then creates a plan for which objects need to be instantiated, which dependencies need to be set and injected, the scope of the newly created instance, etc., based on this configuration metadata. The metadata can be supplied in a simple XML file, just like in the first chapter. Alternatively, one could provide the metadata as Annotation or Java Configuration.
书:Just Spring
其他回答
春天有IoC容器,里面装着一袋豆子;创建、维护和删除是Spring Container的职责。 我们可以通过布线和自动布线将bean放到Spring中。 “连线”意味着我们手动将其配置到XML文件中,“自动连线”意味着我们将注释放入Java文件中,然后Spring自动扫描Java配置文件所在的根上下文,将其制作并放入Spring的包中。
这里是详细的URI,您可以从中获得有关bean的更多信息
你只理解了一部分。您必须根据需要定制bean,并通知Spring容器在需要时管理它,方法是使用Martin Fowler创造的通常称为IoC(控制反转)的方法,也称为依赖注入(DI)。
您以某种方式连接bean,这样您就不必关心实例化或计算bean上的任何依赖项。这就是众所周知的好莱坞原则。
谷歌是最好的工具,除了在这个问题中你会被淹没的链接之外,还可以探索更多关于这个问题的内容。:)
Spring的XML配置由bean组成,而bean基本上是类。它们只是我们在ApplicationContext中使用的pojo。定义bean可以看作是替换关键字new。所以无论你在应用程序中使用关键字new,比如:
MyRepository myRepository =new MyRepository ();
当你使用关键字new时你可以删除配置并将其放入XML文件中。 所以我们将像这样编码:
<bean name="myRepository "
class="com.demo.repository.MyRepository " />
现在我们可以简单地使用Setter注入/构造函数注入。我正在使用Setter注入。
public class MyServiceImpl implements MyService {
private MyRepository myRepository;
public void setMyRepository(MyRepository myRepository)
{
this.myRepository = myRepository ;
}
public List<Customer> findAll() {
return myRepository.findAll();
}
}
Spring beans are just object instances that are managed by the Spring IOC container. Spring IOC container carry the Bag of Bean.Bean creation,maintain and deletion are the responsibilities of Spring Container. We can put the bean in to Spring by Wiring and Auto Wiring. Wiring mean we manually configure it into the XML file. Auto Wiring mean we put the annotations in the Java file then Spring automatically scan the root-context where java configuration file, make it and put into the bag of Spring.
Spring bean只是由Spring容器管理的实例对象,也就是说,它们是由框架创建和连接的,并放入一个“对象袋”(容器)中,您以后可以从那里获取它们。
“连接”部分是依赖注入的全部内容,它的意思是你可以说“我将需要这个东西”,框架将遵循一些规则来为你获得适当的实例。
对于那些不习惯Spring的人来说,我认为Wikipedia Spring的文章有一个很好的描述:
Central to the Spring Framework is its inversion of control container, which provides a consistent means of configuring and managing Java objects using reflection. The container is responsible for managing object lifecycles of specific objects: creating these objects, calling their initialization methods, and configuring these objects by wiring them together. Objects created by the container are also called managed objects or beans. The container can be configured by loading XML files or detecting specific Java annotations on configuration classes. These data sources contain the bean definitions which provide the information required to create the beans. Objects can be obtained by means of either dependency lookup or dependency injection. Dependency lookup is a pattern where a caller asks the container object for an object with a specific name or of a specific type. Dependency injection is a pattern where the container passes objects by name to other objects, via either constructors, properties, or factory methods.
推荐文章
- 如何分割逗号分隔的字符串?
- Java字符串—查看字符串是否只包含数字而不包含字母
- Mockito.any()传递带有泛型的接口
- 在IntelliJ 10.5中运行测试时,出现“NoSuchMethodError: org.hamcrest. matcher . descripbemismatch”
- 使用String.split()和多个分隔符
- Java数组有最大大小吗?
- 在Android中将字符串转换为Uri
- 从JSON生成Java类?
- 为什么java.util.Set没有get(int index)?
- Swing和AWT的区别是什么?
- 为什么Java流是一次性的?
- 四舍五入BigDecimal *总是*有两位小数点后
- 设计模式:工厂vs工厂方法vs抽象工厂
- Java:检查enum是否包含给定的字符串?
- 它的意思是:序列化类没有声明一个静态的最终serialVersionUID字段?