我还没有找到我能够理解的Spring bean的高级定义。我经常在Grails文档和书籍中看到它们,但我认为了解它们是什么将是有益的。那么春豆是什么呢?如何使用它们?它们与依赖注入有关吗?
当前回答
bean是Spring Container管理的类的实例。
其他回答
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.
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引导应用程序而言,bean只是一个Java对象,它是在应用程序启动时由Spring框架创建的。
对象的用途可以是任何东西——配置、服务、数据库连接工厂等等——Spring并不关心这些。
大多数bean依赖于其他bean才能工作,例如实体管理器可能需要一个数据库连接。Spring框架能够找出如何将bean自动连接在一起。从应用程序开发人员的角度来看,您只需声明所需的bean,它们就会“神奇地”出现在您的应用程序中,随时可以使用。
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
For a Java class to be usable as a Java bean, its setter- and getter-method names need to be as per the JavaBean guidelines (also called design patterns) for properties. If such a Java class is instantiable & manageable by the Spring IoC container, it is a Spring bean. To achieve this, the programmer wires the class as a bean definition of a suitable scope by using XML config files or annotations or a mix of both. The programmer can create new Spring beans out of existing Spring beans by wiring further by passing the latter to constructor-arguments of the former either as string-names as <idref> elements or by dependency injection (it can be recursive).
这个答案可以与我的这个SO答案一起阅读,以获得更多的背景信息。
推荐文章
- 如何分割逗号分隔的字符串?
- 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字段?