springMVC学习(注解实现依赖注入)

上一篇博客,学习了spring的依赖注入,即利用spring容器来为类中的属性赋值,分为两种赋值方法,利用set和利用构造方法,我们都知道,如果我需要为某一个属性赋值的话,必须为该属性写上set方法,那么大家有没有想过一个问题,如果我们一个类中有很多个属性,我们会生成大量的set方法,如果用构造方法来赋值,即<constructor-arg index="" type="" ref="" value=""></constructor-arg>,这样也会存在很多个这样的标签,因为一个<constructor-arg index="" type="" ref="" value=""></constructor-arg>只能为一个属性赋值,为了解决这些难题,我们可以利用注解来为属性进行赋值。

同样,我们新建两个类ClassInfo.java和Teacher.java

package com.test.spring.di;public class ClassInfo {public void printClassInfo() {System.out.println("this is classInfo….");}}package com.test.spring.di;import javax.annotation.Resource;public class Teacher {private String teacherName;@Resourceprivate ClassInfo classInfo;public void printClassInfo() {this.classInfo.printClassInfo();}} 注意,这里我们给需要被赋值的属性上添加了@Resource的注解。其中包含了基本的String类型以及ClassInfo的引用类型。接下来我们来写一个applicationContext.xml文件:

注意:因为我们使用注解来为属性赋值的,所以我们需要导入命名空间,如下:

xmlns:context=""

"

其次需要导入依赖注入的注解解析器:

<context:annotation-config></context:annotation-config>

3.导入需要被spring容器来管理的类。

经过上面散步操作之后呢,applicationContext.xml是这个样子:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns=""xmlns:xsi="" xmlns:context=""xsi:schemaLocation=""><context:annotation-config></context:annotation-config><bean id="classInfo" class="com.test.spring.di.ClassInfo"></bean><bean id="teacher" class="com.test.spring.di.Teacher"></bean></beans>此时,编写我们的测试类TestAnotation.java

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/test/spring/di/applicationContext.xml");Teacher teacher = (Teacher) applicationContext.getBean("teacher");teacher.printClassInfo();此时打印如下:

this is classInfo….

这充分说明了利用注解为Teacher类中的classInfo对象赋值成功了。在这里需要注意:如果我么你的@Resource的注解的值为"",那么spring将会将容器中id为该属性的类注入给该属性。什么意思呢??我举个栗子:

我将上面的@Resource的值修改为这样:@Resource(name="test")此时由于name的值不是"",所以spring容器将不会试图匹配与该属性相同的id对应的类,而是将id为test的类注入给该属性,然而,此时并没有id为test的类,所以会抛出如下异常:

No bean named ‘test’ is defined

此时我将classInfo对应的id改为test,再次运行发现程序是ok的。

其实spring有自己定义的注解的,细心的程序员可能发现了@Resource是java官方提供的一个注解,并不是spring的,我们可以将@Resource替换成@Autowired,这个@Autowired是根据类型进行匹配的。那我如果非要按照id来进行匹配怎么办呢?别急,spring为我们提供了一个@Qualifier,我们如果要匹配spring容器中id为classInfo的类,可以这样写:@Qualifier("classInfo"),注意:这里还需要加上@Autowired,如下:

@Autowired@Qualifier("classInfo")private ClassInfo classInfo; 大家有没有想过,既然spring是用来管理bean的,难道就没有生命周期的管理???是有的。我们为ClassInfo类添加如下初始化和销毁方法:

@PostConstructpublic void init() {System.out.println("classInfo init….");}@PreDestroypublic void destroy() {System.out.println("classInfo destroy…..");}顾名思义,@PostConstruct表示紧接着在构造方法之后执行的,@PreDestroy表示在销毁前执行的,然后编写测试代码如下:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/test/spring/di/applicationContext.xml");ClassPathXmlApplicationContext classContext = (ClassPathXmlApplicationContext) applicationContext;Teacher teacher = (Teacher) classContext.getBean("teacher");teacher.printClassInfo();classContext.close();此时运行工程,会打印如下信息:

classInfo init….this is classInfo….classInfo destroy…..我们发现确实执行了初始化和销毁的方法,说明利用注解来实现声明周期的管理也是可以的。

但是,注意spring的注解只能用于引用类型。

那我能不能在applicationContext.xml中对于bean的声明也不写呢??是可以的,怎么做的,同样分为以下三步:

1.导入命名空间:

xmlns:context=""

"

2.启动扫描类的注解解析器

<context:annotation-config></context:annotation-config>3.启动依赖注入的注解解析器

<context:component-scan base-package="com.test.spring.di"></context:component-scan>

这里是扫描"com.test.spring.di"包和该包下的所有的类。

4.在需要被spring来管理的类上加上@Component的注解

注意:和@Resource比较相似,@Component是默认匹配类名的第一个字母小写的,比如我在Teacher类上加了@Component这个注解,那么可以这样得到该类对象:

即使爬到最高的山上,一次也只能脚踏实地地迈一步。

springMVC学习(注解实现依赖注入)

相关文章:

你感兴趣的文章:

标签云: