1 引入context命名空间(在Spring的配置文件中),配置文件如下:
- xmlns:context="http://www.springframework.org/schema/context"
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
打开配置 <context:component-scan base-package="包名(扫描本包及子包)"/>
spring 会自动扫描cn.pic包下面有注解的类,完成Bean的装配。
- <?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-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <context:component-scan base-package="cn.pic"/>
- </beans>
2 在classPath中加入注解用的jar包
lib\j2ee\common-annotations.jar
Spring 的context:component-scan扫描支持扫描jar包的方法:
eclipse自带的jar打包程序,默认打包的时候有个选项<Add directory entries>没有勾选,只要勾选了,就可以了.
-----------常用注解--------
--定义Bean的注解
@Controller
@Controller("Bean的名称")
定义控制层Bean,如Action
@Service
@Service("Bean的名称")
定义业务层Bean
@Repository
@Repository("Bean的名称")
定义DAO层Bean
@Component
定义Bean, 不好归类时使用.
--自动装配Bean (选用一种注解就可以)
@Autowired (Srping提供的)
默认按类型匹配,自动装配(Srping提供的),可以写在成员属性上,或写在setter方法上
@Autowired(required=true)
一定要找到匹配的Bean,否则抛异常。 默认值就是true
@Autowired
@Qualifier("bean的名字")
按名称装配Bean,与@Autowired组合使用,解决按类型匹配找到多个Bean问题。
@Resource JSR-250提供的
默认按名称装配,当找不到名称匹配的bean再按类型装配.
可以写在成员属性上,或写在setter方法上
可以通过@Resource(name="beanName") 指定被注入的bean的名称, 要是未指定name属性, 默认使用成员属性的变量名,一般不用写name属性.
@Resource(name="beanName")指定了name属性,按名称注入但没找到bean, 就不会再按类型装配了.
@Inject 是JSR-330提供的
按类型装配,功能比@Autowired少,没有使用的必要。
--定义Bean的作用域和生命过程
@Scope("prototype")
值有:singleton,prototype,session,request,session,globalSession
@PostConstruct
相当于init-method,使用在方法上,当Bean初始化时执行。
@PreDestroy
相当于destory-method,使用在方法上,当Bean销毁时执行。
--声明式事务
@Transactional
-----------------------------------------------------------------------------------------------------------------------------------
首先了解从spring2.5增加的新特性:
这些新特性包括:注解驱动的依赖性注入(annotation-driven dependency injection),使用注解而非XML元数据来自动侦测classpath上的Spring组件,注解对生命周期方法的支持,一个新的web控制器模型将请求映射到加注解的方法上,在测试框架中支持Junit4,Spring XML命名空间的新增内容,等等。
1.前提条件 引入context 的 Schema命名空间 在配置文件中添加context:annotation-config标签
为了获得新的的特性 首先要引入新的context的Schema命名空间,该命名空间对注释驱动、属性文件引入、加载期织入等功能提供了便捷的配置。我们知道注释本身是不会做任何事情的,它仅提供元数据信息。要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工作起来。
<?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:aop=”http://www.springframework.org/schema/aop”
xmlns:tx=”http://www.springframework.org/schema/tx”
xmlns:jdbc=”http://www.springframework.org/schema/jdbc”
xmlns:context=”http://www.springframework.org/schema/context”
xsi:schemaLocation=”
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd”>
<!–
这个配置隐式注册了多个对注释进行解析处理的处理器,代码中就可以直接使用@Autowired, @Required等annotaion了。
- AutowiredAnnotationBeanPostProcessor 对应@Autowire,CommonAnnotationBeanPostProcessor 对应@Resource等,
- PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor 对应@required
–>
<context:annotation-config />
</beans>
2.自动装配属性实例。
使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是:
@Autowired 默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。
强烈建议 放弃@Autowire 使用@Resource 原因 spring支持标准
即 Java EE5中引入了“Java平台的公共注解(Common Annotations for the Java Platform)”,而且该公共注解从Java SE 6一开始就被包含其中。
在2.5版本中,Spring框架的核心(core)现在支持以下JSR-250注解:
结合Spring,这些注解在任何开发环境下都可以使用——无论是否有应用程序服务器——甚至是集成测试环境都可以。
使用方式:可以用来标注在字段或属性的setter方法上.如果标注在字段上,则可以省略掉该属性的getter 和setter方法。
同时
所要注入实例bean的名称可以通过@Resource的name属性指定,如果没有指定name属性,
1.当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象
2.当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象。
//用于字段上
@Resource(name=“personDao”)
private PersonDaopersonDao;
//用于属性的set方法上
@Resource(name=“personDao”)
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
注意:如果没有指定name属性,并且按照默认的名称找不到依赖对象时, @Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。
3.spring自动扫描机制
spring2.5为我们引入了组件自动扫描机制,它可以在classPath路径底下寻找标注了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入进spring容器中管理。它的作用和在xml文件中使用bean节点配置组件是一样的。
也就是要spring自动扫描机制只会查找指定类路径下包含@Component、@Service、@Controller、@Repository这四种注解的类。
要使用自动扫描机制,我们需要打开以下配置信息:
1、引入context命名空间 需要在xml配置文件中配置以下信息: 同上先引入context 命名空间,同时
2、在配置文件中添加context:component-scan标签
<context:component-scan base-package=”*”/>
其中base-package为需要扫描的包(含子包)。
注:
1、在使用组件扫描元素时,AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor会隐式地被包括进来。 也就是说,连个组件都会被自动检测并织入 – 所有这一切都不需要在XML中提供任何bean配置元数据。也就是说如果使用了context:component-scan标签,就可以不需要再引入context:annotation-config标签
<context:component-scan />还允许定义过滤器将基包下的某些类纳入或排除。Spring支持以下4种类型的过滤方式:
过滤器类型 表达式范例 说明
注解 org.example.SomeAnnotation 将所有使用SomeAnnotation注解的类过滤出来
类名指定 org.example.SomeClass 过滤指定的类
正则表达式 com\.kedacom\.spring\.annotation\.web\..* 通过正则表达式过滤一些类
AspectJ表达式 org.example..*Service+ 通过AspectJ表达式过滤一些类
以正则表达式为例,我列举一个应用实例:
Java代码
<context:component-scan base-package=”com.casheen.spring.annotation”>
<context:exclude-filter type=”regex” expression=”com\.casheen\.spring\.annotation\.web\..*” />
</context:component-scan>
<context:component-scan base-package=”com.casheen.spring.annotation”>
<context:exclude-filter type=”regex” expression=”com\.casheen\.spring\.annotation\.web\..*” />
</context:component-scan>
值得注意的是<context:component-scan />配置项不但启用了对类包进行扫描以实施注释驱动Bean定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此当使用<context:component-scan />后,就可以将<context:annotation-config />移除了。
2.为需要被扫描的类添加相应的注解,注解的类型有以下几种:
@Service用于标注业务层组件、
@Controller用于标注控制层组件(如struts中的action)、
@Repository用于标注数据访问组件,即DAO组件。
而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。 这四种注解仅仅是角色不同,但实质都一样。
细节问题总结:
1.当我们进行测试时,用ApplicationContext对象的getBean()方法寻找组件。在之前的配置文件中我们会用<bean>标签的id属性去定义,在使用自动扫描注解后怎样获得组建的id呢?
在这种情况下,Spring会将被标注注解的类名,然后再将该类名的第一个字母变为小写,放到getBean()方法中。如:UserBizImpl类的组件Id就会是userBizImpl,获取时为context.getBean(“userBizImpl”);
那么,我们在使用注解时可以自定义组件的Id吗?
当然可以。我们需要在为相应的类添加注解时,在注解之后添加自定义的类名,例如:
@Service(“userBiz”)
public class UserBizImpl implements UserBiz {
……
}
当我们在获取该组件时,为context.getBean(“userBiz);
2.在配置文件中我们可以对组件(bean)的作用域范围进行设置,它的默认值是单例模式,那么在添加注解的情况下,我们怎样设置组件的作用域范围呢?
我们可以直接在为类添加注解的同时,运用另一个注解@Scope(“prototype”)来设置,如下
@Service(“userBiz”)@Scope(“prototype”)
public class UserBizImpl implements UserBiz {
……
}
3.在使用注解时,为组件设置初始化和销毁方法:
在添加注解的相应的类中,如果想初始化或销毁某个方法,我们可以直接在方法上添加注解,如下:
@PostConstruct
public void addItem() {
System.out.println(“初始化方法”);
}
@PreDestroy
public void testItem() {
System.out.println(“释放资源”);
}
4.在使用Spring自动扫描组件后,怎样进行依赖注入?
运用注解@Resource和@Autowired,并为依赖对象设置名称,例如:
@Resource(name=”userDao”)
private UserDAO userDao = null;
首先它会根据名称去找Spring自动扫描的并加入到Spring容器的组件(bean),如果有相同的名称,则进行依赖注入,如果没有相同的名称。则会根据类型区寻找组件。
理解以上的内容后,你就可以很轻松的实现spirng零配置。
————————————————————————————————
项目后期开发工作 定义了大量的bean,现在需要为每个数据库操作添加 日志记录,所以就定义了一个logBiz,
如果按照通常的做法,需要修改所有的配置文件 添加property属性,现在使用自动注入机制。
在baseAction中添加通用日志方法,留出一个IogBiz接口,在继承的子类action中,定义一个logBiz属性 并用@Resouce 注解。即可。