Spring学习day3-Spring配置开发(基于注解开发)
在上一篇博客中我们大致学习了Spring配置文件的用处以及Spring基于ApplicationContext文件的开发,今天我们来学习一下另一种开发方式
Spring注解开发
配置开发的约束引入
在学习配置文件开发时,我们最开始先在配置文件头部引入了bean的约束,所以我们可以再其中使用bean标签,现在我们要学习注解开发,就要引入新的约束(可以先把之前写的bean标签注释掉不会记混)
<?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>
我们在配置文件中新增context的相关约束,然后我们可以测试一下在beans标签下打一个“<”会不会出现context联想,如果有就说明引入成功了
开启组件扫描并为对象添加注解
我们现在beans标签下加入这么一句话
<context:component-scan base-package="com.wfh.bean"/>
有了这句话之后我们就对com.wfh.bean这个基本包开启了组件扫描,spring会自动扫描该包下以及子包的所有类中的注解(spring会把我们的类当成一个组件–所以叫component-scan)
然后我们尝试在类中添加一些注解,让spring帮我们管理这些类,我们以User类为例
Component注解
import org.springframework.stereotype.Component;
@Component("user")
public class User {
private Integer u_id;
private String u_username;
private String u_password;
private Animal pet;
public User() {
System.out.println("没有参数的构造方法");
}
....//下面就不写了
User类头部的Component注解类似于我们之前在xml配置文件中写的
<bean name="user" class="com.wfh.bean.User">
只不过这次我们是在java文件中对其进行配置,我们可以进行如下测试:
@Test
public void Test_Annotation(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
User u= (User) ac.getBean("user");
System.out.println(u);
}
输出结果:
没有参数的构造方法
User{u_id=null, u_username='null', u_password='null', pet=null}
Process finished with exit code 0
可以看到和之前的用配置文件开发的效果相同。
除了Component注解(component已经相对早期了),在现在的项目中,为了区别某个类属于哪一个层,spring有增加了三个与component类似的注解:
@Controller:对应web层
@Service:对应service层
@Repository:对应dao层
这些注解的功能都是将对象交给容器管理,但是在日后的web项目开发中我们通常用这三个注解来代替Component注解,这样看着会比较清晰
Scope注解
另外,我们还可以在类的头部添加Scope注解,在里面添加value="prototype"字样对应之前bean标签中的scope属性
测试代码:
@Test
public void Test_Annotation(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
User u1= (User) ac.getBean("user");
User u2=(User) ac.getBean("user");
System.out.println(u1==u2);
}
输出:
没有参数的构造方法
没有参数的构造方法
false
PostConstruct注解
主要放在某个方法的头部,顾名思义,就是紧跟执行构造器之后要执行的方法,也就是之前在bean标签中执行过的init-method属性;
测试代码:
@Test
public void Test1(){
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
User u1= (User) ac.getBean("user");
}
输出:
没有参数的构造方法
Init
PreDestory注解
放在某方法头部,表示在对象被销毁前执行的方法,对应bean标签中的destory-methed
这里提醒一下,如果在多例模式下,加上ProDestory注解后是不会执行destory方法的,而单例模式仍然执行,所以为了演示我先把Scope改成了singleton
测试代码:
@Test
public void Test1(){
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
User u1= (User) ac.getBean("user");
ac.close();
}
输出:
没有参数的构造方法
Init
Destroy
类的属性注入
值类型注入
如果我们想给一个类的某个成员变量赋初值,我们可以在那个变量对应的set方法前添加Value("?")注解,就像这个样子
这样每次从容器中的到的对象就都有一部分成员变量已含有初值了
测试代码:
@Test
public void Test1(){
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
User u= (User) ac.getBean("user");
System.out.println(u);
}
输出:
没有参数的构造方法
Init
User{u_id=1, u_username='老王', u_password='123456', pet=null}
当然,你也可以直接在成员变量声明处的头部添加Value注解,如下图,可以起到相同的效果,但是这种赋值方式并不是通过set方法,而是通过暴力反射的方式对类的私有变量进行了赋值,有违面向对象思想的封装性,所以一般不推荐使用
引入类型注入
如果类的某些成员变量不是基础数据类型,比如User类里的pet变量,我们就需要引入一个具体对象。
我们先打开Animal类,为其添加Component(“pet”)注解注册到容器,然后在type的set方法前添加Value(“cat”)注解
然后回到User类,在setPet方法前添加Autowired注解,意为自动装配,容器会自动在已注册的类中找到匹配的类进行注入
测试代码:
@Test
public void Test1(){
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
User u= (User) ac.getBean("user");
System.out.println(u);
}
输出:
没有参数的构造方法
Init
User{u_id=1, u_username='老王', u_password='123456', pet=Animal{type='cat'}}