继续学习Spring框架,前面我们都是使用xml配置文件,这篇开始,我们学习如何使用注解方式去代替xml配置文件。注解我们多多少少接触过,例如在junit写单元测试用例这个@Test就是一个注解,在软件工程工程中一般@开头都是注解,JDK1.5开始引入注解这个功能。
1.环境准备
新建一个动态web项目,然后把导包,把前面bean对象和配置文件拿过来。这两个Bean对象代码可以看这篇文章。
2.为主配置文件引入新的命名空间(约束)
这里再次需要导入一个xsd约束,叫context约束,请参考前面介绍如何导入bean约束,导入效果之后,xml文件内容如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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 ">
</beans>
如果你不会导入约束文件到eclipse上,你可以直接copy上面内容也可以。
3.开启注解代替配置文件
有一个开关用来代替xml配置文件,这个开关的写法如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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.anthony.bean包下所有类中的注解 -->
<context:component-scan base-package="com.anthony.bean"></context:component-scan>
</beans>
4.在类中使用注解完成配置
例如这里我们用User.java来举例如何添加注解
package com.anthony.bean;
import org.springframework.stereotype.Component;
@Component("user")
public class User {
private int age;
private String name;
private Car car;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "User [age=" + age + ", name=" + name + ", car=" + car + "]";
}
}
上面类名称上面一行的注解@Component("user") 相当于之前我们在xml中写的
<bean name="user" class="com.anthony.bean.User"> </bean>
下面把前面test包下的Demo1.java拷贝过来,运行一下
package com.anthony.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.anthony.bean.User;
public class Demo1 {
@Test
public void fun1() {
//1 创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2 向容器 要 user对象
User u1 = (User) ac.getBean("user");
//3 打印user对象
System.out.println(u1);
}
}
结果发现,运行之后什么都没有输出,原因就是我们没有把spring-aop-xxx.jar包导入。
导入包之后再次运行 Demo1.java类,输出结果如下
User [age=0, name=null, car=null]
这个输出说明注解产生了作用,没有值是因为我们没有写属性注入。
5.其他三个注解
早期Spring用@Component一个注解就可以,后来,开发人员发现,全部对象都使用这一个注解,没法快速区分这个对象是属于哪一层的,是service层还是control层,还是dao层,后来Spring为了解决这个问题,新增三个注解。
@Service("user")
用这个注解表示这个对象属于Service层,也就是业务层
@Controller("user")
用这个注解表示这个对象属于Control层,例如我们的Servlet类
@Repository("user")
这个单词是仓库的意思,联想到数据库仓库,这个就表示DAO层对象
上面三个注解的功能和@Component是一样效果,推荐使用能够区分分层的这三个注解。
6.怎么决定对象的作用范围
这里我们学习下如何确保User这个对象的作用范围的注解如何写,前面我们配置文件中是使用scope属性,注解也是使用这个单词。@Scope(scopeName = "prototype|singleten"), 默认是单例,我们演示一个多例。
package com.anthony.bean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("user")
@Scope(scopeName = "prototype")
public class User {
private int age;
private String name;
private Car car;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "User [age=" + age + ", name=" + name + ", car=" + car + "]";
}
}
然后再Demo1.java中判断u1是否等于u2,如果是多例,肯定不想等,如果是单例,这里相等。
package com.anthony.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.anthony.bean.User;
public class Demo1 {
@Test
public void fun1() {
//1 创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2 向容器 要 user对象
User u1 = (User) ac.getBean("user");
User u2 = (User) ac.getBean("user");
//3 打印user对象
System.out.println(u1 == u2);
}
}
7.值类型属性注入
接着来介绍对象中属性value注入,先来看看name和age的value如何注入。
User.java方式一写注入
package com.anthony.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("user")
@Scope(scopeName = "prototype")
public class User {
@Value("18")
private int age;
@Value("tom")
private String name;
private Car car;
上面节约篇幅,只写关键变动部分代码。然后Demo1.java中打印u1的内容
User [age=18, name=tom, car=null]
还有一种属性注入写法,那就是写到set方法上面
@Value("18")
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
@Value("jack")
public void setName(String name) {
this.name = name;
}
这两种方式赋值区别:第一个是通过反射field来赋值,第二种是通过set方法来赋值。推荐使用通过set方法赋值,反射赋值破坏了对象的封装性。
8.对象类型属性注入
下面看看Car这个对象属性如何通过注解赋值
方式1:@Autowired,翻译过来叫 自动装配
先再Car类中利用前面方法给属性注解。
package com.anthony.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Car {
@Value("玛莎拉蒂")
private String name;
@Value("红色")
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Car [name=" + name + ", color=" + color + "]";
}
}
然后回到User这个类中来学Car的属性注入
package com.anthony.bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("user")
@Scope(scopeName = "prototype")
public class User {
private int age;
private String name;
@Autowired
private Car car;
@Value("18")
public void setAge(int age) {
this.age = age;
}
@Value("jack")
public void setName(String name) {
this.name = name;
}
}
运行Demo1.java
User [age=18, name=jack, car=Car [name=玛莎拉蒂, color=红色]]
自动装配方式有一个问题:如果检测到Spring容器中存在多个类型一样的对象,无法选择具体哪一个。
例如容器中有car和car1对象,当前car1这里我没有模拟出来,我可以在applicationContext.xml中用前面旧知识创建一个car1对象出来。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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.anthony.bean包下所有类中的注解 -->
<context:component-scan base-package="com.anthony.bean"></context:component-scan>
<bean name="car1" class="com.anthony.bean.Car">
<property name="name" value="布拉迪威龙"></property>
<property name="color" value="黑色"></property>
</bean>
</beans>
然后User.java中private Car car;这里注解这样写。
@Component("user")
@Scope(scopeName = "prototype")
public class User {
private int age;
private String name;
@Autowired
@Qualifier("car1")
private Car car;
这样利用注解@Qualifier去限制找名称为car1的Car对象。运行Demo1.java结果
User [age=18, name=jack, car=Car [name=布拉迪威龙, color=黑色]]
注解方式2:@Resource(name="car1")
如果很多对象采用上面这种注解,每个都写两行,有时候很麻烦,所以一般使用这种不是自动装配的注解:@Resource(name="car1")
@Component("user")
@Scope(scopeName = "prototype")
public class User {
private int age;
private String name;
//@Autowired
//@Qualifier("car1")
@Resource(name="car1")
private Car car;
推荐使用这种方式@Resource,手动注入,告诉Spring指定注入哪个名称的对象。
9.初始化方法和销毁方法注解
在User.java中添加如下方法
public void init() {
System.out.println("这个是初始化方法");
}
public void destory() {
System.out.println("这个是销毁方法");
}
下面学习两个新注解,关于初始化和销毁方法
@PostConstruct //运行构造方法之后执行,相当于init方法
public void init() {
System.out.println("这个是初始化方法");
}
@PreDestroy //Spring对象销毁之前运行
public void destory() {
System.out.println("这个是销毁方法");
}
运行下Demo1.java
package com.anthony.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.anthony.bean.User;
public class Demo1 {
@Test
public void fun1() {
//1 创建容器对象
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2 向容器 要 user对象
User u1 = (User) ac.getBean("user");
//3 打印user对象
System.out.println(u1);
//4.测试销毁方法
ac.close();
}
}
运行之前记得把User.java中注解改成单例 : @Scope(scopeName = "singleton")
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
这个是初始化方法
User [age=18, name=jack, car=Car [name=布拉迪威龙, color=黑色]]
六月 11, 2019 4:04:17 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@75412c2f: startup date [Tue Jun 11 16:04:16 CST 2019]; root of context hierarchy
这个是销毁方法