欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

【Spring学习】spring依赖注入用法总结

发布时间:2024/4/14 javascript 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 【Spring学习】spring依赖注入用法总结 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

顾名思义就是通过注解来实现注入,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; @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; @Servicepublic class CarService {@Autowiredprivate 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/beanshttp://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 "><!-- 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的效果是一样的。
另外,同时使用Autowired注解和Qualifier注解时也要注意一点:
如果采用@Autowired来注解,则无需指定name属性,若是实现该接口有多个类,则需要通过@Qualifier来做区分:
CarService1、CarService2是实现ICarService的两个实现类,
类中@Service的注解分别是

@Service("carService1") public class CarService implements ICarService {}@Service("carService2") public class CarService2 implements ICarService {}

那么在TestMethod中测试方法,使用接口ICarService时,使用的@Autowired来标注时,需要使用注解@Qualifier来做区分:

@Autowired @Qualifier("carService2") private ICarService carService;

总结

以上是生活随笔为你收集整理的【Spring学习】spring依赖注入用法总结的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。