Spring学习笔记(2)(一)

2014-11-24 09:24:03 · 作者: · 浏览: 3

1. 装配各种类型的属性
1.1简单属性
使用的value属性或子标签装配。

68

1.2引用其它bean
使用ref属性或标签。

1.3内部bean
直接使用定义一个新的bean,一般不用指定该bean的名称。

1.4装配集合
1)数组

足球

蓝球

音乐

2)List

北大

清华

3)Set

广州

北京

西安

广州

4)Map

5)Properties

4858458

caishiyou@sina.com

qq=58568565868

msn=kdkdkf@ddd.com

1.5装配空值null

1.6装配复杂类型属性
publicclass Person {

private Stringname;

private Integerage;

private Personparent; //父亲,复杂类型

private Datebirthday; //生日,复杂类型

}

1) 使用value来注入复杂类型的对象

如果使用value来注入复杂类型的对象,要依赖的对象提供带一个参数(并且类型为String)的构造函数,Spring就可以将其转换成需要的类型。

2)自定义类型编辑器

使用value来注入复杂类型对象时,是不完整的。这时就要自定义类型编辑器,自定义类型编辑器时继承PropertyEditorSupport类,并重写其setAsText方法。

publicclass MyDateEditorextendsPropertyEditorSupport {

// PropertyEditorSupport的子类中有日期编辑器

private SimpleDateFormatsdf = new SimpleDateFormat("yyyy-MM-dd");

@Override

publicvoid setAsText(Stringtext)throws IllegalArgumentException {

System.out.println("执行编辑器的setAsText方法....");

if(text !=null &&text.trim().length() > 0) {

try {

Datedate = sdf.parse(text);//转换

this.setValue(date);//将转换后的值设置进去

}catch (ParseException e) {

e.printStackTrace();

super.setAsText(text);

}

}

}

}

3)注册自定义编辑器

2.注解装配解决方案1(XML+注解配合)
用法:xml用来定义Bean的信息;注解用来配置依赖信息。

使用到的标签:

@Resource(Java标签的资源注解)(推荐使用)

@Autowired(Spring自定的注解),注解可以加在setter方法上(setter注入)/可以加在Field上(Field注入)。

@Autowired--按类型注入。required=false用来指定该依赖的对象是否是必须的。(false非必须,true必须的,默认true)。

@Qualifier("personDao2")--用来指定要注入的Bean叫什么名字,与@Autowired配合使用。

@Autowired

@Qualifier("personDao2")//要注入的Bean的名称为personDao2,会到IoC容器中找

publicvoid setDao(IPersonDaodao) {

this.dao = dao;

}

@Resource--按名字注入,找到名字相同,则直接注入。找不到名字相同,则找类型。可在使用name属性来指定要注入的Bean的名称如:@Resource(name=”personDao”)。

//要注入的Bean的名称为personDao,会到IoC容器中找

@Resource(name="personDao")

publicvoid setDao(IPersonDaodao) {

this.dao = dao;

}

beans.xml:

< xmlversion="1.0"encoding="UTF-8" >

xmlns:context="http://www.springframework.org/schema/context"

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-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

3.注解装配解决方案2(完全使用注解)
1)指定到IoC容器中的业务组件

在实现类上添加注解:

@Repository(持久层组件)

@Service(业务层的组件)

@Controller(控件层组件)

@Component(不属于以上三层的组件)

放到容器中的Bean的名字默认是类名首字母变小写,使用value属性就可以指定具体的Bean名称。如:@Service("personService")bean的名字为personService。

Bean的作用域:默认是单例singleton。其它作用域,则配合使用@Scope标签来指定。

如:@Scope("prototype")

@Service("personService")//存在在容器中bean的名字为personService

@Scope("prototype")//Bean的作用域为propotype,默认是singleton

publicclass PersonServiceImpl2implements IPersonService {

....//省略

}

2)给组件指定依赖

使用@Autowire或@Resource标签来指定。

//要注入的Bean的名称为personDao,会到IoC容器中找

@Resource(name="personDao")

publicvoid setDao(IPersonDaodao) {

this.dao = dao;

}

3)启动Spring的注解扫描功能

在配置文件中使用指定让Spring去扫描哪些包,从而找到要放到容器中的组件。

4.容器扩展点及应用
Spring提供了一些接口,让开发者通过这些接口可以扩充容器的功能。如常用的BeanFactoryPostProcessor和BeanPostProcessor口。

4.1自定义类型编辑器
参见1.6 2)和3)。

4.2 BeanFactoryPostProcessor接口(创建Bean前)
实现了该接口的子类要重写其postProcessBeanFactory方法,该方法在配置信息加载完以后,Bean对象初始化之前调用,可以在创建Bean之前过滤信息。

1)写一个类实现该接口,并重写postProcessBeanFactory方法。

publicclassMyBeanFactoryPostProcessorimplementsBeanFactoryPostProcessor {

@Override//该方法在配置信息加载完以后,Bean对象初始化之前调用

publicvoidpostProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)throws BeansException {

//可以在创建Bean之前过滤信息

System.out.println("该方法在配置信息加载完以后,Bean对象初始化之前调用");

}

}

2)在配置文件注册该类。

4.3 BeanPostProcessor接口(创建Bean之后)
1)写一个类实现该接口,并重写postP