sqlite 测试 www oca oid pat 不知道 text
注解注入顾名思义就是通过注解来实现注入,Spring 和注入相关的常见注解有 Autowired、Resource、Qualifier、Service、Controller、Repository、Component。
上面的 Autowired 和 Resource 是用来修饰字段,构造函数,或者设置方法,并做注入的。而 Service,Controller,Repository,Component 则是用来修饰类,标记这些类要生成 bean。
下面我们通过实例项目来看下 spring 注解注入的使用。
首先新建一个 maven 项目,并在 pom 中添加 spring 相关的依赖,如果不知道添加那些依赖,请参照上一篇文章。
然后新建 CarDao 类,[email protected],如下代码:
- package cn.outofmemory.helloannotation;
- import org.springframework.stereotype.Repository;
- @Repository
- public class CarDao {
- public void insertCar(String car) {
- String insertMsg = String.format("inserting car %s", car);
- System.out.println(insertMsg);
- }
- }
新建 CarService 类,[email protected],在这个类中定义 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 =newAnnotationConfigApplicationContext("cn.outofmemory.helloannotation");
- CarService service = appContext.getBean(CarService.class);
- service.addCar("宝马");
- }
- }
在上面的 main 方法中首先我们初始化了 appContext,他是 AnnotationConfigApplicationContext,它的构造函数接受一个 package 的名称,来限定要扫描的 package。然后就可以通过 appContext 的 getBean 方法获得 CarService 的实例了。
上面的例子非常简单,单纯的使用 AnnotationConfigApplicationContext 就可以了,但是在实际项目中情况往往没有这么简单,还是需要 spring 配置文件的。在 spring 配置文件中也可以通过下面的配置让 spring 自动扫描注解配置。
- />
- base-package="cn.outofmemory.helloannotation" >
- </context:component-scan>
下面我们看下混合使用 spring 配置和注解的例子,首先在项目中添加 source folder,src/main/resources, 并添加 spring.xml, 其内容如下:
- 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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd ">
- package="cn.outofmemory.helloannotation" >class="cn.outofmemory.helloannotation.CarDao" >
在上面的配置文件中,我们通过 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 =newClassPathXmlApplicationContext("/spring.xml");
- CarService service = appContext.getBean(CarService.class);
- service.addCar("宝马");
- }
- }
运行程序发现输出为:
,显然 CarService 自动注入的 CarDao 使用了默认构造函数构造的实例。是否可以通过注解指定使用 spring.xml 中配置的 sqliteCarDao 呢?
- inserting car 宝马 into mysql
我们可以修改下 CarService 类,通过 Qualifier 注解来指定要使用的 bean 的名字。
如下, 在指定 Autowired 注解时,同时指定 Qualifier 注解指定 bean 的名字
- @Autowired
- @Qualifier("sqliteCarDao")
- private CarDaocarDao;
重新运行下 App.java 这次输出的是
, 这次使用了 spring.xml 中配置的 bean 了。
- inserting car 宝马 into sqlite
在文中开头我们还提到了 Resouce 注解,这个注解可以指定名字注入,我们再次修改下 CarService 类:
- @Resource(name="sqliteCarDao")
- private CarDaocarDao;
[email protected][email protected]
spring 注解使用介绍
来源: http://www.bubuko.com/infodetail-2091843.html