我还没有找到我能够理解的Spring bean的高级定义。我经常在Grails文档和书籍中看到它们,但我认为了解它们是什么将是有益的。那么春豆是什么呢?如何使用它们?它们与依赖注入有关吗?
当前回答
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的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是什么。
根据Spring IoC容器和bean的介绍部分(其中“IoC”表示“控制反转”):
在Spring中,构成应用程序主干并由Spring IoC容器管理的对象称为bean。bean是由Spring IoC容器实例化、组装和管理的对象。否则,bean只是应用程序中的众多对象之一。bean以及它们之间的依赖关系反映在容器使用的配置元数据中。
Bean和作用域在Bean作用域部分中描述:
When you create a bean definition, you create a recipe for creating actual instances of the class defined by that bean definition. The idea that a bean definition is a recipe is important, because it means that, as with a class, you can create many object instances from a single recipe. You can control not only the various dependencies and configuration values that are to be plugged into an object that is created from a particular bean definition but also control the scope of the objects created from a particular bean definition. This approach is powerful and flexible, because you can choose the scope of the objects you create through configuration instead of having to bake in the scope of an object at the Java class level. Beans can be defined to be deployed in one of a number of scopes.
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.
bean是Spring Container管理的类的实例。
春天有IoC容器,里面装着一袋豆子;创建、维护和删除是Spring Container的职责。 我们可以通过布线和自动布线将bean放到Spring中。 “连线”意味着我们手动将其配置到XML文件中,“自动连线”意味着我们将注释放入Java文件中,然后Spring自动扫描Java配置文件所在的根上下文,将其制作并放入Spring的包中。
这里是详细的URI,您可以从中获得有关bean的更多信息
推荐文章
- 是否有(Java)包组织的最佳实践?
- 如何从Eclipse中的开放资源对话框中隐藏.class文件?
- 将日期字符串解析为java.util.Date时,不合法的模式字符'T'
- 使用Mockito的泛型“any()”方法
- 如何使用Java属性文件?
- 我如何修复一个NoSuchMethodError?
- Maven surefire找不到ForkedBooter类
- Java 8:我如何在流中使用异常抛出方法?
- 去下一次迭代在java For循环
- 在Java中使用什么数据类型来表示钱?
- Class.getResource()和ClassLoader.getResource()之间的区别是什么?
- 如何通过传递特定日期来确定星期几?
- 如何将DecimalFormat的小数分隔符从逗号更改为点/点?
- 控制jar工件的Maven最终名称
- Spring-MVC控制器中的404触发器?