什么是Spring beans?beans的用途是什么?
作者:互联网
我是Spring MVC的初学者,我有一个问题是为什么要使用bean.
据我所知,bean只有带有getter和setter的私有变量.
我有几个问题,
is that the same beans appear in Spring MVC,
why beans are used in Spring what is the syntax for defining beans
(my project is sampleSpr) sampleSpr-servlet.xml (which is in WEB-INF)
有人可以帮我解决这些问题吗?
解决方法:
The objects that form the backbone of your application and that are
managed by the Spring IoC container are called beans. A bean is an
object that is instantiated, assembled, and otherwise managed by a
Spring IoC container. These beans are created with the configuration
metadata that you supply to the container, for example, in the form of
XML definitions.
从SpringSource了解更多有关bean和作用域的信息:
When you create a bean definition what you are actually creating is 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, just like a class, you can potentially have
many object instances created 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 the scope of the objects created
from a particular bean definition. This approach is very powerful and
gives you the flexibility to 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
beans是Spring bean XML schema的名称空间前缀.在架构定义中,名称空间前缀到名称空间的映射是在其他地方完成的-很有可能在根元素中.
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
不需要前缀,因为默认名称空间再次映射到相同的架构(很可能是在根元素中).从文档中:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
xmlns:表示默认名称空间是http://www.springframework.org/schema/beans.在xsi:schemaLocation属性中,您看到名称空间被映射到定义该名称空间的Spring bean模式:
<xsd:schema xmlns="http://www.springframework.org/schema/beans"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.springframework.org/schema/beans">
标签:spring-bean,spring,spring-mvc 来源: https://codeday.me/bug/20191122/2057984.html