什么鬼。。。。。。spring学习笔记整理
1.配置spring的头头文件
<?xml version="1.0" encoding="UTF-8"?>
<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-2.5.xsd">
<!--添加bean-->
</beans>
2.bean 的相关属性
<bean id="exampleBean" class="examples.ExampleBean"/>
<!--<bean />中的属性:-->
<!--class:指定配置类的全类名
<bean class="com.demo.entity.User">
2.1. id: 指定类的唯一标识id
<bean id="user" >
2.2. name:指定类的唯一标识name
<bean name="user" >
2.3. scope:用于限制Bean的作用域
<bean scope="singleton">或<bean singleton="true">
singleton Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例
<bean scope="prototype">或<bean singleton="false">
Prototype作用域的bean会导致在每次对该bean请求时都会创建一个新的bean实例。根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。通常情况下,DAO不会被配置成prototype,因为DAO通常不会持有任何会话状态,因此应该使用singleton作用域。
<bean id="" class="" scope="request"/>
针对每次HTTP请求,Spring容器会根据bean定义创建一个全新的bean实例, 且该bean实例仅在当前HTTP request内有效,因此可以根据需要放心的更改所建实例的内部状态, 而其他请求中根据bean定义创建的实例,将不会看到这些特定于某个请求的状态变化。 当处理请求结束,request作用域的bean实例将被销毁。
<bean id="" class="" scope="session"/>
针对某个HTTP Session,Spring容器会根据 bean定义创建一个全新的bean实例, 且该bean仅在当前HTTP Session内有效。 与request作用域一样,你可以根据需要放心的更改所创建实例的内部状态,而别的HTTP Session中根据创建的实例, 将不会看到这些特定于某个HTTP Session的状态变化。 当HTTP Session最终被废弃的时候,在该HTTP Session作用域内的bean也会被废弃掉。
<bean id="" class="" scope="globalSession"/>
global session作用域类似于标准的HTTP Session作用域,不过它仅仅在基于portlet的web应用中才有意义。Portlet规范定义了全局Session的概念,它被所有构成某个portlet web应用的各种不同的portlet所共享。在global session作用域中定义的bean被限定于全局portlet Session的生命周期范围内。
2.4. constructor arguments:构造器注入
构造器注入 eg:
java类:
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
}
配置文件:
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="java.lang.String" value="7500000"/>
<constructor-arg type="java.lang.String" value="42"/>
</bean>
或
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0" value="7500000"/>
<constructor-arg index="1" value="42"/>
</bean>
2.5. properties:
setter注入 eg:
java类:
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public void setBeanOne(AnotherBean beanOne) {
this.beanOne = beanOne;
}
public void setBeanTwo(YetAnotherBean beanTwo) {
this.beanTwo = beanTwo;
}
public void setIntegerProperty(int i) {
this.i = i;
}
}
配置文件:
<bean id="exampleBean" class="examples.ExampleBean">
<property name="beanOne"><ref bean="anotherExampleBean"/></property>
<property name="beanTwo" ref="yetAnotherBean"/>
<property name="integerProperty" value="1"/>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
2.6. autowiring mode: 自动装配(autowire)协作者
模式 说明
no:
byName: 根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配。例如,在bean定义中将autowire设置为by name,而该bean包含master属性(同时提供setMaster(..)方法),Spring就会查找名为master的bean定义,并用它来装配给master属性。
byType: 如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。若没有找到相匹配的bean,则什么事都不发生,属性也不会被设置。如果你不希望这样,那么可以通过设置dependency-check="objects"让Spring抛出异常。
constructor: 与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。
autodetect: 通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式。
2.7. dependency checking mode:依赖检查
模式 说明
none: 没有依赖检查,如果bean的属性没有值的话可以不用设置。
simple: 对于原始类型及集合(除协作者外的一切东西)执行依赖检查
object: 仅对协作者执行依赖检查
all: 对协作者,原始类型及集合执行依赖检查
2.8. lazy-initialization mode:延迟初始化bean(传说中的懒加载???)
<!-- 在类中添加懒加载-->
<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>
<!-- 在容器层次上添加懒加载-->
<beans default-lazy-init="true">
<!-- no beans will be pre-instantiated... -->
</beans>
2.9. initialization method:初始化回调
实现org.springframework.beans.factory.InitializingBean接口允许容器在设置好bean的所有必要属性后,执行初始化事宜。InitializingBean接口仅指定了一个方法:
<!--配置文件-->
<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
//相关的java代码
public class ExampleBean {
public void init() {
// do some initialization work
}
}
2.10. destruction method:析构回调
<!--配置文件-->
<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>
//相关的java代码
public class ExampleBean {
public void cleanup() {
// do some destruction work (like releasing pooled connections)
}
}
缺省的初始化和析构方法
public class DefaultBlogService implements BlogService {
private BlogDao blogDao;
public void setBlogDao(BlogDao blogDao) {
this.blogDao = blogDao;
}
// this is (unsurprisingly) the initialization callback method
public void init() {
if (this.blogDao == null) {
throw new IllegalStateException("The [blogDao] property must be set.");
}
}
}
<beans default-init-method="init">
<bean id="blogService" class="com.foo.DefaultBlogService">
<property name="blogDao" ref="blogDao" />
</bean>
</beans>