参考:慕课网http://www.imooc.com/video/3668
一.spring 注入方式
public class InjectionServiceImpl implements InjectionService {//需要实例化的类
private InjectionDAO injectionDAO;//需要初始化的成员变量
//构造器注入--参数名称必须和配置文件中的name一致
public InjectionServiceImpl(InjectionDAO injectionDAO) {
this.injectionDAO = injectionDAO;
}
//设值注入----set方法的名称必须和配置文件中的name一致,参数名称可以不一致
public void setInjectionDAO(InjectionDAO injectionDAO1) {
this.injectionDAO = injectionDAO1;
}
……
}
spring配置文件:设值注入的name要与 set方法的名称一致,set方法的 参数名称名称可以不一样
<?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.xsd" >
<!-- <bean id="injectionService" class="com.imooc.ioc.injection.service.InjectionServiceImpl"> -->
<!-- <property name="injectionDAO" ref="injectionDAO"></property> -->
<!-- </bean> -->
<bean id="injectionService" class="com.imooc.ioc.injection.service.InjectionServiceImpl">
<constructor-arg name="injectionDAO" ref="injectionDAO"></constructor-arg>
</bean>
<bean id="injectionDAO" class="com.imooc.ioc.injection.dao.InjectionDAOImpl"></bean>
</beans>
3.自动注入-自动装配 全局default-autowire="xxx",单个 autowire=“xxx”
<bean>的autowire属性有如下六个取值,他们的说明如下:
1、 No:即不启用自动装配。Autowire默认的值。
2、 byName:在这种方式中要求bean的id与java类set方法名称一致。通过属性的名字的方式查找JavaBean依赖的对象并为其注入。比如说类Computer有个属性printer,指定其autowire属性为byName后,Spring IoC容器会在配置文件中查找id/name属性为printer的bean,然后使用set方法为其注入。ioc容器启动是就会检查byName方式的正确性
3、 byType:通过属性的类型查找JavaBean依赖的对象并为其注入。比如类Computer有个属性printer,类型为Printer,那么,指定其autowire属性为byType后,Spring IoC容器会查找Class属性为Printer的bean,使用set方法为其注入。可能抛出异常
4、 constructor:通byType一样,也是通过类型查找依赖对象。调用构造函数注入。可能抛出异常
5、 autodetect:在byType和constructor之间自动的选择注入方式。
6、 default:由上级标签<beans>的default-autowire属性确定。
注意:在配置bean时,<bean>标签中Autowire属性的优先级比其上级标签高,即是说,如果在上级标签中定义default-autowire属性为byName,而在<bean>中定义为byType时,Spring IoC容器会优先使用<bean>标签的配置。
二.Bean的配置项
scope:实例的作用域
3.request:每个http请求将会有各自的bean实例,类似于prototype。
4.session:在一个http session中,一个bean定义对应一个bean实例。
5.global session:在一个全局的http session中,一般是在portlet context的时候有效。
三.Bean生命周期
bean的初始化与销毁
- 通过实现InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法;
- 通过<bean> 元素的 init-method/destroy-method属性指定初始化之后 /销毁之前调用的操作方法;
- bean全局配置:default-init-method/default-destroy-method属性指定所有的bean初始化/销毁调用的操作方法。
- 在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用
四.Bean Aware接口
五.Spring 注解
1.注解扫描
Spring配置文件 applicationContext.xml的<context:component-scan >标签用途比我想像的还要实用。而且后来才知道,有了<context:component-scan >,另一个<context:annotation-config/>标签根本可以移除掉,因为被包含进去了。原本我survery Spring3通常只配置成<context:component-scan base-package="com.foo.bar"/>,意即在base-package下寻找有@Component和@Configuration的target Class。而现在如下的:
<context:component-scan base-package="com.foo" use-default-filters ="false"> |
<context:component-scan > 提供两个子标签:<context:include-filter>和<context:exclude-filter>各代表 引入和排除的过滤。而上例把use-default-filters属性设为false,意即在base-package所有被宣告为 @Component和@Configuration等target Class不予注册为bean,由filter子标签代劳。
filter标签在Spring3有五个type,如下:
Filter Type | Examples Expression | Description |
annotation | org.example.SomeAnnotation | 符合SomeAnnoation的target class |
assignable | org.example.SomeClass | 指定class或interface的全名 |
aspectj | org.example..*Service+ | AspectJ语法 |
regex | org\.example\.Default.* | Regelar Expression |
custom | org.example.MyTypeFilter | Spring3新增自订Type,实作org.springframework.core.type.TypeFilter |
所以上例用的regex就有个语病,com.foo.config.* 可以找到com.foo.config.WebLogger,但也可以找到com1fool2config3abcde,因为小数点在Regex是任意字 元,是故要用\.把小数点跳脱为佳。(2010/3/15补充:但要使用\.方式,其use-default-filters不能为false,否则抓不 到,感觉是Bug)
2.@component
把类实例化到spring容器中,相当于配置文件中的<bean id="" class=""/> id为类名-第一个字母小写
3. @Autowired
使用 @Autowired
的地方都是需要注入 Bean 的,它可以用在成员变量、构造函数、setter方法。
@Autowired(required = false)
表示不是一定要注入,如果依赖对象为null就不用注入了
可以注解众所周知的spring的解析依赖性接口:如ApplicationContex、BeanFactory
还可以通过注解成员变量、方法注入特定类型的集合(set,list,map)---符合该类型的所有bean(如果想要集合有序可以加@Order)
不能注解BeanPostProcessor、BeanFactorPostProcessor类型,因为Autowired本身就是有spring BeanPostProcessor处理的,这些类型只能通过xml或者@Bean来注入
如:
@Autowired
public func(set<xxx> para){ }
@Resource和@Autowired都可以来完成注入依赖,但它们之间是有区 别的。首先来看一下:
a。@Resource默认是按照名称来装配注入的,只有当找不到与名称匹配的bean才会按照类型来装配注入;适用于成员变量、只有一个参数的setter方法;所以目标是构造函数或者,多参数方法时最好使用@Autowired+@Qualifier
b。@Autowired默认是按照类型装配注入的,如果想按照名称id来转配注入,则需要结合@Qualifier一起使用(1.可以写在方法参数类型前来限定参数类型;2.可以用在成员变量上);
c。@Resource注解是又J2EE提供,而@Autowired是由Spring提供,故减少系统对spring的依赖建议使用
@Resource的方式;
d。 @Resource和@Autowired都可以书写标注在字段或者该字段的setter方法之上
4.mvc 注解:@ Repository @Serivce @ Control
5.@Bean
6.配置文件读取
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
<context:property-placeholder location="classpath:/config.properties"/>
</beans>
法一:java文件中采用注解的方式获取值
@Configuration
@ImportResource("classpath:config.xml")
public class StoreConfig {
@Value("${url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${password}")
private String password;
@Bean
public MyDriverManager myDriverManager() {
return new MyDriverManager(url, username, password);
}
}
法二:在config.xml文件中添加
</bean>
7.spring 3.0 jsr33
@Inject等效于@Autowired 可以用于属性、方法、成员变量
@name 等效于@component 可以指定name即指定bean的id