目录
5.按名称注入域属性@Autowired与@Qualifier
7.Bean的生命始末@PostConstruct与@PreDestroy
注解的作用就是替换Spring配置文件中bean的注册~
对于DI使用注解,将不再需要在Spring配置文件中声明Bean实例。Spring中使用注解,需要在原有Spring运行环境基础上再做一些改变,完成以下三个步骤。
- 导入AOP的Jar包。因为注解的后台实现用到了AOP编程。
- 需要更换配置文件头,即添加相应的约束。
- 需要在Spring配置文件中配置组件扫描器,用于在指定的基本包中扫描注解。
1.定义Bean@Component
需要在类上使用注解@Component,该注解的value属性用于指定该bean的id值。
package com.lmm.di01;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
// @Scope("prototype") // 默认也是singleton,指定他是单例的还是原型的
//注解参数中省略了 value属性,该属性用于指定Bean的id
@Component("myStudent")
public class Student {
@Value("张三")
private String name;
@Value("23")
private int age;
//@Autowired
//private School school; // 对象属性,域属性的注入,,byType方式的注解式注入
@Autowired
@Qualifier("mySchool") // byName方式的注解式注入,要求@Autowired与@Qualifier联合使用
private School school; // 对象属性,域属性
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", school=" + school
+ "]";
}
}
另外,Spring还提供了3个功能基本和@Component等效的注解:
- @Repository 用于对DAO实现类进行注解
- @Service 用于对Service实现类进行注解
- @Controller 用于对Controller实现类进行注解(SpringMVC)
之所以创建这三个功能与@Component等效的注解,是为了以后对其进行功能上的扩展,使它们不再等效。
我们在applicationContext里限定扫描注解包,否则程序还会到配置文件找Bean的注册,我们让他去某个包里面找!
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描com.lmm这个包及其子包 -->
<!-- <context:component-scan base-package="com.lmm"/> -->
<!-- 扫描com.lmm这个包的子包 -->
<!-- <context:component-scan base-package="com.lmm.*"/> -->
<!-- 注册组件扫描器 -->
<!-- 扫描com.lmm.di01这个包 -->
<context:component-scan base-package="com.lmm.di01"/>
</beans>
2.Bean的作用域@Scope
需要在类上使用注解@Scope,其value属性用于指定作用域。默认为singleton。
3.基本类型属性注入@Value
需要在属性上使用注解@Value,该注解的value属性用于指定要注入的值。
使用该注解完成属性注入时,类中无需setter。当然,若属性有setter,则也可将其加到setter上。
4.按类型注入域属性@Autowired
需要在域属性上使用注解@Autowired,该注解默认使用按类型自动装配Bean的方式。使用该注解完成属性注入时,类中无需setter。当然,若属性有setter,则也可将其加到setter上。
5.按名称注入域属性@Autowired与@Qualifier
@Autowired还有一个属性required,默认值为true,表示当匹配失败后,会终止程序运行。若将其值设置为false,则匹配失败,将被忽略,未匹配的属性值为null。
@Autowired还有一个属性required,默认值为true,表示当匹配失败后,会终止程序运行。若将其值设置为false,则匹配失败,将被忽略,未匹配的属性值为null。
6.域属性注解@Resource
pring提供了对JSR-250规范中定义@Resource标准注解的支持。@Resource注解既可以按名称匹配Bean,也可以按类型匹配Bean。使用该注解,要求JDK必须是6及以上版本。
(1)按类型注入域属性
(2)按名称注入域属性
7.Bean的生命始末@PostConstruct与@PreDestroy
在方法上使用@PostConstruct,与原来的init-method等效。在方法上使用@PreDestroy,与destroy-method等效。
8.使用JavaConfig进行配置(了解)
JavaConfig,是在Spring 3.0开始从一个独立的项目并入到Spring中的。JavaConfig可以看成一个用于完成Bean装配的Spring配置文件,即Spring容器,只不过该容器不是XML文件,而是由程序员使用Java自己编写的Java类。
package com.lmm.di05;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration // 表示当前类充当Spring容器,即所有的Bean将由这个类来创建
public class MyJavaConfig {
@Bean(name="school")
public School mySchoolCreator() {
return new School("北京大学");
}
// autowire=Autowire.BY_TYPE 指从当前类这个容器中查找与域属性的类型具有is-a关系的Bean
/*
@Bean(name="myStudent", autowire=Autowire.BY_TYPE)
public Student myStudentCreator() {
return new Student("张三", 23);
}
*/
// autowire=Autowire.BY_NAME 指从当前类这个容器中查找与域属性同名的Bean
@Bean(name="myStudent", autowire=Autowire.BY_NAME)
public Student myStudentCreator() {
return new Student("张三", 23);
}
}
9.使用Spring JUnit4测试Spring(了解)
使用Spring的JUnit4对Spring代码进行测试,将不再需要在程序的代码中直接写出创建Spring容器,及从Spring容器中通过getBean()获取对象了。这些工作将由JUnit4注解,配合着域属性的自动注入注解共同完成。
package com.lmm.di06;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:com/lmm/di06/applicationContext.xml")
public class MyTest {
@Autowired
private Student student;
@Autowired
private School school;
@Test
public void test01() {
System.out.println(student);
}
@Test
public void test02() {
System.out.println(school);
}
}
10.注解与XML共同使用
注解的好处是,配置方便,直观。但其弊端也显而易见:以硬编码的方式写入到了Java代码中,其修改是需要重新编译代码的。
XML配置方式的最大好处是,对其所做修改,无需编译代码,只需重启服务器即可将新的配置加载。
若注解与XML同用,XML的优先级要高于注解。这样做的好处是,需要对某个Bean做修改,只需修改配置文件即可。当然,此时,Bean类要有setter或构造器。