转自:http://www.360sdn.com/springmvc/2013/0627/407.html
——————————————————————————————————————————
1、使用Spring非注解(annotation)方式注入属性
1
2
3
4
5
6
|
public class RoleServicesLogic implements RoleServices { private RoleDao roleDao; public void setRoleDao(RoleDao roleDao) { this .roleDao = roleDao; } } |
applicationContext.xml配置文件:
1
2
3
4
5
6
7
8
|
< bean id = "roleServicesLogic" class = "com.example.logic.RoleServicesLogic" > < property name = "roleDao" ref = "roleDao" /> </ bean > < bean id = "roleDao" class = "com.example.dao.RoleDaoImpl" > < property name = "sessionFactory" ref = "sessionFactory" /> </ bean > |
2、使用Spring注解(annotation)注入属性的
这里引入@Autowired注解(不推荐使用,建议使用@Resource),对成员变量进行注解.
1
2
3
4
5
|
public class RoleServicesLogic implements RoleServices { @Autowired private RoleDao roleDao; } |
也可以对方法进行注解
1
2
3
4
5
6
7
8
|
public class RoleServicesLogic implements RoleServices { private RoleDao roleDao; @Autowired public void setRoleDao(RoleDao roleDao) { this .roleDao = roleDao; } } |
applicationContext.xml使用注解后的完整配置文件:
这里使用<context:annotation-config />简化配置,<context:annotationconfig />将隐式地向Spring容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor这4个BeanPostProcessor。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" default-lazy-init = "true" > <!-- 使Spring关注Annotation --> < context:annotation-config /> <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 --> < context:component-scan base-package = "com.example.*" /> < bean id = "roleServicesLogic" class = "com.example.logic.RoleServicesLogic" /> < bean id = "roleDao" class = "com.example.dao.RoleDaoImpl" > < property name = "sessionFactory" ref = "sessionFactory" /> </ bean > </ beans > |
@Autowired注解标签可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。 以上两种不同实现方式中,@Autowired的标注位置不同,它们都会在Spring在初始化RoleManagerImpl这个bean时, 自动装配roleDao这个属性,区别是:第一种实现中,Spring会直接将RoleDao类型的唯一一个bean赋值给roleDao这个成员变量; 第二种实现中,Spring会调用setRoleDao方法来将RoleDao类型的唯一一个bean装配到RoleDao这个属性。
3、使用Spring的@Qualifier注解标签
@Autowired是根据类型进行自动装配的。在上面的例子中,如果当Spring上下文中存在不止一个RoleDao类型的bean时,就会抛出BeanCreationException异常; 如果Spring上下文中不存在RoleDao类型的bean,也会抛出BeanCreationException异常。我们可以使用@Qualifier配合@Autowired来解决这些问题。
a:如果存在多个RoleDao实例
1
2
3
4
5
|
@Autowired public void setRoleDao( @Qualifier ( "RoleDao" ) RoleDao roleDao) { this .RoleDao = roleDao; } |
这样,Spring会找到id为RoleDao的bean进行装配。
b:如果不存在RoleDao实例
1
2
3
4
5
|
@Autowired (required = false ) public void setRoleDao(RoleDao RoleDao) { this .RoleDao = RoleDao; } |
4、 @Resource(JSR-250标准注解,推荐使用它来代替Spring专有的@Autowired注解)
Spring 不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource、@PostConstruct以及@PreDestroy。 @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按byName自动注入罢了。@Resource有两个属性是比较重要的,分别是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。
@Resource装配顺序
1:如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
2:如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
3:如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
4:如果既没有指定name,又没有指定type,则自动按照byName方式进行装配(见2);如果没有匹配,则回退为一个原始类型(RoleDao)进行匹配,如果匹配则自动装配
5、 @Component(不推荐使用)、@Repository、@Service、@Controller
a:@Service用于标注业务层组件
b:@Repository用于标注数据访问组件,即DAO组件
c:@Controller用于标注控制层组件,如Struts中的Action或者springmvc的controller
d:@Component泛指组件,当组件不好归类时,可以使用这个注解进行标注
e:@Entity 实体类注解标签
f:@Table 实体类对应的数据库实际的表名,不设置表名默认数据库表名与类名一致。
Spring还提供了更加细化的注解形式:@Repository、@Service、@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean。目前版本(2.5)中,这些注解与@Component的语义是一样的,完全通用,在Spring以后的版本中可能会给它们追加更多的语义。所以,我们推荐使用@Repository、@Service、@Controller来替代@Component。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Service @Transactional public class CommonLogic { } @Repository public class RoleDao extends HibernateDao<ROLEENTITY,STRING> { } @Controller @RequestMapping (value = "/role" ) public class RoleController { @Autowired private RoleServicesLogic roleServicesLogic; } </ROLEENTITY,STRING> |
对于无法划分是那一层的类只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了.
1
2
3
4
|
@Component public class DateUtils { } |
使用@Component注解定义的Bean,默认的名称(id)是小写开头的非限定类名。如这里定义的Bean名称就是dateUtils。你也可以指定Bean的名称:@Component("DateUtils"),@Component是所有受Spring管理组件的通用形式.
6、使用<context:component-scan />让Bean定义注解工作起来
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" default-lazy-init = "true" > <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 --> < context:component-scan base-package = "com.example.*" /> </ beans > |
这里,所有通过元素定义Bean的配置内容已经被移除,仅需要添加一行<context:component-scan />配置就解决所有问题了——Spring XML配置文件得到了极致的简化(当然配置元数据还是需要的,只不过以注释形式存在罢了)。<context:component-scan />的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。 <context:component-scan />还允许定义过滤器将基包下的某些类纳入或排除。
过滤器类型 | 表达式范例 | 说明 |
注解(annotation) | org.example.SomeAnnotation | 将所有使用SomeAnnotation注解的类过滤出来 |
正则表达式 | com.example.action..* | 通过正则表达式过滤一些类 |
AspectJ表达式 | org.example..*Logic+ | 通过AspectJ表达式过滤一些类 |
1
2
3
4
5
6
7
8
9
10
|
<!--不注入spring的controller类--> < context:component-scan base-package = "com.example" > < context:exclude-filter type = "annotation" expression = "org.springframework.stereotype.Controller" /> </ context:component-scan > <!-- 开启controller注解支持,只注入 spring的controller类 --> < context:component-scan base-package = "com.example" use-default-filters = "false" > < context:include-filter type = "annotation" expression = "org.springframework.stereotype.Controller" /> </ context:component-scan > |
值得注意的是<context:component-scan />配置项不但启用了对类包进行扫描以实施注释驱动Bean定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此当使用<context:component-scan />后,就可以将<context:annotation-config />移除了。
7、使用@Scope来定义Bean的作用范围
在使用XML定义Bean时,我们可能还需要通过bean的scope属性来定义一个Bean的作用范围,我们同样可以通过@Scope注解来完成这项工作:
1
2
3
4
5
6
|
@Scope ( "session" ) @Component public class RoleBean implements Serializable { } |