一:Bean的作用范围
<bean id="accountService" class="com.foo.DefaultAccountService"/>
<!-- the following is equivalent, though redundant (singleton scope is the default); using spring-beans-2.0.dtd -->
<bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/>
<!-- the following is equivalent and preserved for backward compatibility in spring-beans.dtd -->
<bean id="accountService" class="com.foo.DefaultAccountService" singleton="true"/>
Scope | Description |
singleton | Scopes a single bean definition to a single object instance per Spring IoC container. |
request | Scopes a single bean definition to any number of object instances. |
request | Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext . |
session | Scopes a single bean definition to the lifecycle of a HTTP Session . Only valid in the context of a web-aware Spring ApplicationContext . |
globel session | Scopes a single bean definition to the lifecycle of a global HTTP Session . Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext . |
二:集合注入
<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setAdminEmails(java.util.Properties) call -->
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.org</prop>
<prop key="support">support@example.org</prop>
<prop key="development">development@example.org</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>
<value>an entry</value>
</key>
<value>just some string</value>
</entry>
<entry>
<key>
<value>a ref</value>
</key>
<ref bean="myDataSource" />
</entry>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource" />
</set>
</property>
</bean>
这样配置,在取得moreComplexObject 之后,就可以取得封装在其中的list、map等
三:自动装配 autowire
No
autowiring at all. Bean references must be defined via a ref
element. This is the default, and changing this is discouraged for larger deployments, since explicitly specifying collaborators gives greater control and clarity. To some extent, it is a form of documentation about the structure of a system.
Autowiring by property name. This option will inspect the container and look for a bean named exactly the same as the property which needs to be autowired. For example, if you have a bean definition which is set to autowire by name, and it contains a master property (that is, it has a setMaster(..)method), Spring will look for a bean definition named master
, and use it to set the property.
Allows a property to be autowired if there is exactly one bean of the property type in the container. If there is more than one, a fatal exception is thrown, and this indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens; the property is not set. If this is not desirable, setting the dependency-check="objects"
attribute value specifies that an error should be thrown in this case.
This is analogous to byType, but applies to constructor arguments. If there isn't exactly one bean of the constructor argument type in the container, a fatal error is raised.
autodetectChooses constructor or byType through introspection of the bean class. If a default constructor is found, the byType mode will be applied.
例如:<beans>
<bean id="userDao" class="com.haizhu.dao.impl.UserDaoImpl"></bean>
<bean id="service" class="com.haizhu.service.impl.SpringUserServiceImpl" autowire="byName">
</bean>
</beans>
四:生命周期
1、延迟加载bean
<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>
<bean name="not.lazy" class="com.foo.AnotherBean"/>
在需要使用这个bean的时候才加载,一般不适用,除非启动程序很慢的时候。
2、自动调用方法
在初始化执行之前,要运行一个方法,比如做日志,
<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
public class ExampleBean {
public void init() {
// do some initialization work
}
}
或者容器关闭的时候,
<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
public class AnotherExampleBean implements InitializingBean {
public void afterPropertiesSet() {
// do some initialization work
}
}
五:注解
1、@Repository、@Service、@Controller 这三个和 @Component 属于同一个层级,只是分开来写,用于区分MVC的层次
2、@Autowired、@Resource 前者默认按照类型加载,后者可以指定加载的bean名称
3、@Scope
4、@PostConstruct、@PreDestory,这个是4.2自动调用方法的注解形式