您的位置:首页 > 编程语言 > Java开发

spring注解学习

2017-01-22 21:36 393 查看
spring配置使用传统 XML声明为bean

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

<beansxmlns="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 id="boss"class="com.baobaotao.Boss">

        <property name="car"ref="car"/>

        <property name="office" ref="office"/>

    </bean>

    <bean id="office"class="com.baobaotao.Office">

        <property name="officeNo"value="002"/>

    </bean>

    <bean id="car"class="com.baobaotao.Car" scope="singleton">

        <property name="brand"value=" 红旗 CA72"/>

        <property name="price"value="2000"/>

    </bean>

</beans>

这是可以通过ApplicationContext的ClassPathXmlApplicationContext来获取bean对象.

Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作.单独使用@Autowired,要配置xml

<beanclass="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

@Autowired方法标注在方法的时候,可以自动注入参数.

@Autowired方法标注在构造方法的时候,可以自动注入参数,装配类的bean,bean的实例应该和标注注释对应,只有一方都会报错.

@Qualifier可以指定xml中的bean

@Resource 的作用相当于 @Autowired,只不过 @Autowired 按 byType 自动注入,面@Resource 默认按 byName 自动注入.

使@Resource注解生效:

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>

CommonAnnotationBeanPostProcessor实现了 BeanPostProcessor 接口.

@PostConstruct:将在类实例化后调用.

@PreDestroy:将在类销毁之前调用.

Spring 2.1 添加了一个新的 context 的 Schema 命名空间,该命名空间对注释驱动、属性文件引入、加载期织入等功能提供了便捷的配置。我们知道注释本身是不会做任何事情的,它仅提供元数据信息。要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工作起来。

而我们前面所介绍的 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor 就是处理这些注释元数据的处理器。但是直接在 Spring 配置文件中定义这些 Bean 显得比较笨拙。Spring 为我们提供了一种方便的注册这些BeanPostProcessor 的方式,这就是 <context:annotation-config/>,它将隐式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor
以及equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。

在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间。

Spring 2.5 在 @Repository 的基础上增加了功能类似的额外三个注解:@Component、@Service、@Constroller,它们分别用于软件系统的不同层次.

在使用 @Component 注释后,Spring 容器必须启用类扫描机制以启用注释驱动 Bean 定义和注释驱动 Bean 自动注入的策略

<context:component-scan/>的 base-package 属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。

扫描过滤方式:

过滤器类型  说明

注释     假如 com.xxx.SomeAnnotation 是一个注释类,我们可以将使用该注释的类过滤出来。

类名指定     通过全限定类名进行过滤,如您可以指定将 com.baobaotao.Boss 纳入扫描,而将 com.baobaotao.Car 排除在外。

正则表达式  通过正则表达式定义过滤的类,如下所示: com\.baobaotao\.Default.*

AspectJ 表达式  通过 AspectJ 表达式定义过滤的类,如下所示: com. baobaotao..*Service+

<c
b981
ontext:component-scanbase-package="com.xxx">

    <context:include-filtertype="regex"

       expression="com\.baobaotao\.service\..*"/>

    <context:exclude-filtertype="aspectj"

       expression="com.baobaotao.util..*"/>

</context:component-scan>

<context:component-scan/>配置项不但启用了对类包进行扫描以实施注释驱动Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。

@Scope("prototype"):默认情况下通过 @Component 定义的 Bean 都是 singleton 的,如果需要使用其它作用范围的 Bean,可以通过@Scope 注释来达到目标.

如果 Bean 不是自己编写的类(如 JdbcTemplate、SessionFactoryBean 等),注释配置将无法实施,此时 XML 配置是唯一可用的方式。

注释配置往往是类级别的,而 XML 配置则可以表现得更加灵活。比如相比于@Transaction 事务注释,使用 aop/tx 命名空间的事务配置更加灵活和简单
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring