spring 基础

本文详细介绍了Spring框架中Bean的配置方式,包括ApplicationContext与BeanFactory的区别、Bean标识符的定义、singleton模式的应用、以及如何配置复杂类型的Bean属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.ApplicationContext是BeanFactory的子类,它扩展了BeanFactory的功能,一般情况下,应该使用ApplicationContext,除非对内存有要求的情况下,而且不需要使用ApplicationContext所提供的功能。

------------------


Users are sometimes unsure whether BeanFactory or ApplicationContext are best suited for use in a particular
situation. Normally when building most applications in a J2EE-environment, the best option is to use the
ApplicationContext, since it offers all the features of the BeanFactory and adds on to it in terms of features,
while also allowing a more declarative approach to use of some functionality, which is generally desireable.
The main usage scenario when you might prefer to use the BeanFactory is when memory usage is the greatest
concern (such as in an applet where every last kilobyte counts), and you don't need all the features of the
ApplicationContext.

 

- --------  
2.id和name属性是一回事,一个Bean可以有多个id和name,推荐使用id做为Bean的唯一标示符,name可以作为Bean的别名,在XML文件中对属性id的值会有一些限制,对name则没有,如果想使用一些XML限制的字符作为Bean的标示符,可以使用name属性来表示。

------------

Every bean has one or more ids (also called identifiers, or names; these terms refer to the same thing). These ids
must be unique within the BeanFactory or ApplicationContext the bean is hosted in. A bean will almost always
have only one id, but if a bean has more than one id, the extra ones can essentially be considered aliases.
In an XmlBeanFactory (including ApplicationContext variants), you use the id or name attributes to specify the
bean id(s), and at least one id must be specified in one or both of these attributes. The id attribute allows you to
specify one id, and as it is marked in the XML DTD (definition document) as a real XML element ID attribute,
the parser is able to do some extra validation when other elements point back to this one. As such, it is the
preferred way to specify a bean id. However, the XML spec does limit the characters which are legal in XML
IDs. This is usually not really a constraint, but if you have a need to use one of these characters, or want to
introduce other aliases to the bean, you may also or instead specify one or more bean ids (separated by a
comma (,) or semicolon (;) via the name attribute.

------------

3.class属性可以指定任何类,或指定一个工厂类,并使用factory-method属性指定要使用的静态工厂方法。

----------

<bean id="exampleBean"
class="examples.ExampleBean"/>
<bean name="anotherExample"
class="examples.ExampleBeanTwo"/>


静态工厂方法

<bean id="exampleBean"
class="examples.ExampleBean2"
factory-method="createInstance"/>

---------

When defining a bean which is to be created using a static factory method, along with the class attribute which
specifies the class containing the static factory method, another attribute named factory-method is needed to
specify the name of the factory method itself. Spring expects to be able to call this method (with an optional list
of arguments as described later) and get back a live object, which from that point on is treated as if it had been
created normally via a constructor. One use for such a bean definition is to call static factories in legacy code.
Following is an example of a bean definition which specifies that the bean is to be created by calling a
factory-method. Note that the definition does not specify the type (class) of the returned object, only the class
containing the factory method. In this example, createInstance must be a static method.
<bean id="exampleBean"
class="examples.ExampleBean2"
factory-method="createInstance"/>
The mechanism for supplying (optional) arguments to the factory method, or setting properties of the object
instance after it has been returned from the factory, will be described shortly.

---------

4.singleton属性指定该Bean是否为单例模式,默认为true。若把该属性设为false则在应用程序每次请求该Bean时,都会返回一个新的实例。当为false时,该Bean的生命周期将不再被Spring管理,此时可以把Spring的作用想象成new运算符。

------------

When a bean is a singleton, only one
shared instance of the bean will be managed and all requests for beans with an id or ids matching that bean
definition will result in that one specific bean instance being returned.
Beans, BeanFactory and the ApplicationContext
The non-singleton, prototype mode of a bean deployment results in the creation of a new bean instance every
time a request for that specific bean is done. This is ideal for situations where for example each user needs an
independent user object or something similar.

Note: when deploying a bean in the prototype mode, the lifecycle of the bean changes slightly. By definition,
Spring can not manage the complete lifecycle of a non-singleton/prototype bean, since after it is created, it is
given to the client and the container does not keep track of it at all any longer. You can think of Spring's role
when talking about a non-singleton/prototype bean as a replacement for the 'new' operator. Any lifecycle
aspects past that point have to be handled by the client. The lifecycle of a bean in the BeanFactory is further
described in Section 3.4.1, “Lifecycle interfaces”.

------------

5.spring会把空的属性和参数配置信息当做空串,


<bean class="ExampleBean">
<property name="email"><value></value></property>
</bean>

等价于:exampleBean.setEmail("")

 

使用<null>节点可以为属性和参数赋值为null


<bean class="ExampleBean">
<property name="email"><null/></property>
</bean>

等价于exampleBean.setEmail(null)

6.list,set,map,properties的配置情况参照下面:

The list, set, map, and props elements allow properties and arguments of Java type List, Set, Map, and
Properties, respectively, to be defined and set.
<beans>
...
<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setPeople(java.util.Properties) call -->
<property name="people">
<props>
<prop key="HaaryPotter">The magic property</prop>
<prop key="JerrySeinfeld">The funny property</prop>
</props>
</property>
<!-- results in a setSomeList(java.util.List) call -->
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource"/>
</list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
<map>
<entry key="yup an entry">
<value>just some string</value>
</entry>
<entry key="yup a ref">
<ref bean="myDataSource"</ref>
</entry>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource"</ref>
</map>
</property>
</bean>
</beans>
Note that the value of a Map entry, or a set value, can also again be any of the elements:
(bean | ref | idref | list | set | map | props | value | null)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值