注解注入顾名思义就是经过注解来实现注入,Spring和注入相关的常见注解有Autowired、Resource、Qualifier、Service、Controller、Repository、Component。java
Autowired是自动注入,自动从spring的上下文找到合适的bean来注入mysql
Resource用来指定名称注入spring
Qualifier和Autowired配合使用,指定bean的名称sql
Service,Controller,Repository分别标记类是Service层类,Controller层类,数据存储层的类,spring扫描注解配置时,会标记这些类要生成bean。app
Component是一种泛指,标记类是组件,spring扫描注解配置时,会标记这些类要生成bean。maven
上面的Autowired和Resource是用来修饰字段,构造函数,或者设置方法,并作注入的。而Service,Controller,Repository,Component则是用来修饰类,标记这些类要生成bean。函数
下面咱们经过实例项目来看下spring注解注入的使用。测试
首先新建一个maven项目,并在pom中添加spring相关的依赖,若是不知道添加那些依赖,请参照上一篇文章。ui
而后新建CarDao类,给它添加@Repository注解,以下代码:this
package cn.outofmemory.helloannotation;import org.springframework.stereotype.Repository;@Repositorypublic class CarDao { public void insertCar(String car) { String insertMsg = String.format("inserting car %s", car); System.out.println(insertMsg); }}
新建CarService类,并给该类标注@Service注解,在这个类中定义CarDao的字段,并经过Autowired来修饰此字段,这样上面定义的CarDao类的实例就会自动注入到CarService的实例中了。
package cn.outofmemory.helloannotation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class CarService { @Autowired private CarDao carDao; public void addCar(String car) { this.carDao.insertCar(car); }}
注意:Autowired注解有一个能够为空的属性required,能够用来指定字段是不是必须的,若是是必需的,则在找不到合适的实例注入时会抛出异常。
下面咱们在App.java中使用上面测试下注解注入:
package cn.outofmemory.helloannotation; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * Hello world! * */public class App { public static void main( String[] args ) { ApplicationContext appContext = new AnnotationConfigApplicationContext("cn.outofmemory.helloannotation"); CarService service = appContext.getBean(CarService.class); service.addCar("宝马"); }}
在上面的main方法中首先咱们初始化了appContext,他是AnnotationConfigApplicationContext,它的构造函数接受一个package的名称,来限定要扫描的package。而后就能够经过appContext的getBean方法得到CarService的实例了。
上面的例子很是简单,单纯的使用AnnotationConfigApplicationContext就能够了,可是在实际项目中状况每每没有这么简单,仍是须要spring配置文件的。在spring配置文件中也能够经过下面的配置让spring自动扫描注解配置。
<!-- bean annotation driven --> <context:annotation-config /> <context:component-scan base-package="cn.outofmemory.helloannotation" > </context:component-scan>
下面咱们看下混合使用spring配置和注解的例子,首先在项目中添加source folder,src/main/resources,并添加spring.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <!-- bean annotation driven --> <context:annotation-config /> <context:component-scan base-package="cn.outofmemory.helloannotation" > </context:component-scan> <bean id="sqliteCarDao" class="cn.outofmemory.helloannotation.CarDao" > <constructor-arg name="driver" value="sqlite"/> </bean> </beans>
在上面的配置文件中,咱们经过context:annotation-config和context:component-sacn节点来指定要扫描注解注入,而后又定义了一个id为sqliteCarDao的bean,它的构造函数的driver值为sqlite。
咱们修改下App.java使用xml配置文件,再运行下App看下会怎样。
package cn.outofmemory.helloannotation; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Hello world! * */public class App { public static void main( String[] args ) { //ApplicationContext appContext = new AnnotationConfigApplicationContext("cn.outofmemory.helloannotation"); ApplicationContext appContext = new ClassPathXmlApplicationContext("/spring.xml"); CarService service = appContext.getBean(CarService.class); service.addCar("宝马"); }}
运行程序发现输出为:inserting car 宝马 into mysql
,显然CarService自动注入的CarDao使用了默认构造函数构造的实例。是否能够经过注解指定使用spring.xml中配置的sqliteCarDao呢?
咱们能够修改下CarService类,经过Qualifier注解来指定要使用的bean的名字。
以下,在指定Autowired注解时,同时指定Qualifier注解指定bean的名字
@Autowired @Qualifier("sqliteCarDao") private CarDao carDao;
从新运行下App.java 此次输出的是inserting car 宝马 into sqlite
,此次使用了spring.xml中配置的bean了。
在文中开头咱们还提到了Resouce注解,这个注解能够指定名字注入,咱们再次修改下CarService类:
@Resource(name="sqliteCarDao") private CarDao carDao;
javax.annotation.Resource注解实现的效果和@Autowired+@Qualifier的效果是同样的。