spring程序员java

Spring注解

2017-06-19  本文已影响204人  是夏莞也是CiCi

Spring注解

Spring可以通过XML文件和注解来配置bean,本文就Spring中的注解进行简要学习

概述

Spring注释可以利用Java的反射机制获取类结构信息,这些信息可以简化XML的配置工作。同时,注释和Java代码位于一个文件中,有助于增强程序的内聚性,采用独立的XML配置文件,程序员在编写一个功能时,要在程序文件和配置文件中不断切换,这种思维上的不连贯会降低开发效率。

通过XML配置

在使用注解配置之前,我们是使用XML文件来对Bean配置的。
下面有三个类,分别是Office,Car,Boss。

package annotation;

/**
 * Created by CiCi on 2017/6/19.
 */
public class Car {
    private String brand;
    private double price;

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "brand:" + brand + "," + "price:" + price;
    }
}

package annotation;

/**
 * Created by CiCi on 2017/6/19.
 */
public class Office {
    private String officeNo;

    public String getOfficeNo() {
        return officeNo;
    }

    public void setOfficeNo(String officeNo) {
        this.officeNo = officeNo;
    }

    @Override
    public String toString() {
        return "officeNo:" + officeNo;
    }
}

package annotation;

/**
 * Created by CiCi on 2017/6/19.
 */
public class Boss {
    private Car car;
    private Office office;

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public Office getOffice() {
        return office;
    }

    public void setOffice(Office office) {
        this.office = office;
    }

    @Override
    public String toString() {
        return "car:" + car + "\n" + "office:" + office;
    }
}

XML配置文件内容

<?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 name="car" class="annotation.Car">
        <property name="brand" value="bmw"/>
        <property name="price" value="100000"/>
    </bean>

    <bean name="office" class="annotation.Office">
        <property name="officeNo" value="001"/>
    </bean>

    <bean name="boss" class="annotation.Boss">
        <property name="car" ref="car"/>
        <property name="office" ref="office"/>
    </bean>

</beans>

测试类

public class AnnotationTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("annotation-learn.xml");
        Boss myBoss = (Boss)applicationContext.getBean("boss");
        System.out.println(myBoss);
    }
}

控制台可以正确准确打印出Boss的信息,说明Bean装载成功

使用注解配置

用于依赖注入
@Autowired

Spring2.5引入了@Autowired注释,他可以对类成员变量方法构造函数进行标注,Spring容器按类型ByType(但是lz测的是ByName,你可以自己试一下是ByType还是ByName)完成依赖注入的自动装配

<?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/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--<context:annotation-config/>-->
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    <bean name="car" class="annotation.Car">
        <property name="brand" value="bmw"/>
        <property name="price" value="100000"/>
    </bean>

    <bean name="office" class="annotation.Office">
        <property name="officeNo" value="001"/>
    </bean>

    <bean name="boss" class="annotation.Boss">
        <!--<property name="car" ref="car"/>-->
        <!--<property name="office" ref="office"/>-->
    </bean>


</beans>

package annotation;

import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by CiCi on 2017/6/19.
 */
public class Boss {
    private Car car;
    private Office office;

    public Car getCar() {
        return car;
    }

    @Autowired
    public void setCar(Car car) {
        this.car = car;
    }

    public Office getOffice() {
        return office;
    }

    @Autowired
    public void setOffice(Office office) {
        this.office = office;
    }

    @Override
    public String toString() {
        return "car:" + car + "\n" + "office:" + office;
    }
}

即在Boss类的内部需要被注入依赖的位置添加注解,并在XML配置文件中删除依赖注入,并在XML文件中添加
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
这是因为Spring通过一个BeanPostProcessor@Autowired进行解析,所以要让@Autowired起作用必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor Bean

@Autowired可以注解在构造方法,设置函数以及成员变量声明处,方法都是一样的。

可能会出现这样的情况,Boss类中需要注入一个Car,但是Spring容器中并没有Car类型的Bean,那么当Spring容器加载Boss Bean的时候会产生异常,这种情况下,我们可以在不确定Spring容器一定拥有某个类的Bean时,在需要注入该Bean的位置使用@Autowired(required=false)来防止报错,这等于告诉Spring,在找不到Bean时也不报错

@Resource

也是用于依赖注入,不同的是它可以设置是byName or byType

@Resource(name = "car")
    public void setCar(Car car) {
        this.car = car;
    }
@PostConstruct和@PreDestroy

Spring 容器中的 Bean 是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,您既可以通过实现 InitializingBean/DisposableBean接口来定制初始化之后 / 销毁之前的操作方法,也可以通过 bean元素的init-method/destroy-method属性指定初始化之后 / 销毁之前调用的操作方法。
JSR-250 为初始化之后/销毁之前方法的指定定义了两个注释类,分别是@PostConstruct@PreDestroy,这两个注释只能应用于方法上。标注了 @PostConstruct注释的方法将在类实例化后调用,而标注了 @PreDestroy的方法将在类销毁之前调用。

Spring添加了一个新的context的Schema命名空间,该命名空间对注释驱动、属性文件引入、加载期织入等功能提供了便捷的配置。我们知道注释本身是不会做任何事情的,它仅提供元数据信息。要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工作起来。
AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor就是处理这些注释元数据的处理器。但是直接在Spring配置文件中定义这些Bean显得比较笨拙。Spring为我们提供了一种方便的注册这些BeanPostProcessor的方式,这就是<context:annotation-config />

<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:annotation-config />    
</beans>    

<context:annotationconfig />将隐式地向Spring容器注册AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessorPersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor这4个BeanPostProcessor。

用于生成Bean
@Component

使用@Component可以通用来生成
只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了:

package annotation;

import org.springframework.stereotype.Component;

/**
 * Created by CiCi on 2017/6/19.
 */
@Component
public class Office {
    private String officeNo;

    public String getOfficeNo() {
        return officeNo;
    }

    public void setOfficeNo(String officeNo) {
        this.officeNo = officeNo;
    }

    @Override
    public String toString() {
        return "officeNo:" + officeNo;
    }
}

使用@Component注解定义的Bean,默认的名称(id)是小写开头的非限定类名。你也可以指定Bean的名称:
@Component("office1")
@Component是所有受Spring管理组件的通用形式,Spring还提供了更加细化的注解形式:@Repository@Service@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean。版本(2.5)中,这些注解与@Component的语义是一样的,完全通用,在Spring以后的版本中可能会给它们追加更多的语义。所以,我们推荐使用@Repository@Service@Controller来替代@Component

参考文章

Spring注解详解
Spring注解总结

上一篇下一篇

猜你喜欢

热点阅读