博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring依赖注入:注解注入总结
阅读量:7187 次
发布时间:2019-06-29

本文共 3560 字,大约阅读时间需要 11 分钟。


更多
 
 
 
 

注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解有Autowired、Resource、Qualifier、Service、Controller、Repository、Component。

  • Autowired是自动注入,自动从spring的上下文找到合适的bean来注入
  • Resource用来指定名称注入
  • Qualifier和Autowired配合使用,指定bean的名称
  • Service,Controller,Repository分别标记类是Service层类,Controller层类,数据存储层的类,spring扫描注解配置时,会标记这些类要生成bean。
  • Component是一种泛指,标记类是组件,spring扫描注解配置时,会标记这些类要生成bean。

上面的Autowired和Resource是用来修饰字段,构造函数,或者设置方法,并做注入的。而Service,Controller,Repository,Component则是用来修饰类,标记这些类要生成bean。

下面我们通过实例项目来看下spring注解注入的使用。

首先新建一个maven项目,并在pom中添加spring相关的依赖,如果不知道添加那些依赖,请参照上一篇文章。

然后新建CarDao类,给它添加@Repository注解,如下代码:

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类,并给该类标注@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自动扫描注解配置。

下面我们看下混合使用spring配置和注解的例子,首先在项目中添加source folder,src/main/resources,并添加spring.xml, 其内容如下:

在上面的配置文件中,我们通过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的效果是一样的。

转载地址:http://lzykm.baihongyu.com/

你可能感兴趣的文章
透明的ico
查看>>
XSS攻击简单了解
查看>>
高性能的I/O设计中 著名的模式Reactor和Proactor模式
查看>>
XDOC合同填报解决方案
查看>>
git迁移
查看>>
Ubuntu 安装JDK6
查看>>
C++前置声明
查看>>
dbcp基本配置和重连配置
查看>>
用fgetc()函数读取磁盘文件并打印到屏幕
查看>>
Golang、python中统计字母,数字、汉字其他的个数。
查看>>
greenRobot的EventBus
查看>>
select2的基本应用
查看>>
PHP面试时经常出现的小算法题
查看>>
使用strace+pstack利器分析程序性能
查看>>
关于Cadence中电源及地的处理
查看>>
cordova 环境搭建
查看>>
json所需包
查看>>
Android中ContentProvider和Uri用法
查看>>
.Net Core 简单定时任务框架封装
查看>>
php curl 的几个常用实例
查看>>