我还没有找到我能够理解的Spring bean的高级定义。我经常在Grails文档和书籍中看到它们,但我认为了解它们是什么将是有益的。那么春豆是什么呢?如何使用它们?它们与依赖注入有关吗?
当前回答
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 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 beans are classes. Instead of instantiating a class (using new), you get an instance as a bean cast to your class type from the application context, where the bean is what you configured in the application context configuration. This way, the whole application maintains singleton-scope instance throughout the application. All beans are initialized following their configuration order right after the application context is instantiated. Even if you don't get any beans in your application, all beans instances are already created the moment after you created the application context.
就Spring引导应用程序而言,bean只是一个Java对象,它是在应用程序启动时由Spring框架创建的。
对象的用途可以是任何东西——配置、服务、数据库连接工厂等等——Spring并不关心这些。
大多数bean依赖于其他bean才能工作,例如实体管理器可能需要一个数据库连接。Spring框架能够找出如何将bean自动连接在一起。从应用程序开发人员的角度来看,您只需声明所需的bean,它们就会“神奇地”出现在您的应用程序中,随时可以使用。
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答案一起阅读,以获得更多的背景信息。
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();
}
}
推荐文章
- 禁用IntelliJ星(包)导入?
- 面试问题:检查一个字符串是否是另一个字符串的旋转
- 将文件加载为InputStream的不同方法
- 到底是什么导致了堆栈溢出错误?
- 为什么Android工作室说“等待调试器”如果我不调试?
- Java:路径vs文件
- ExecutorService,如何等待所有任务完成
- Maven依赖Servlet 3.0 API?
- 如何在IntelliJ IDEA中添加目录到应用程序运行概要文件中的类路径?
- getter和setter是糟糕的设计吗?相互矛盾的建议
- Android room persistent: AppDatabase_Impl不存在
- Java的String[]在Kotlin中等价于什么?
- Intellij IDEA上的System.out.println()快捷方式
- 使用Spring RestTemplate获取JSON对象列表
- Spring JPA选择特定的列