Spring5 介绍
- Spring 是轻量级的开源的 JavaEE 框架 
- Spring 可以解决企业应用开发的复杂性 
- Spring 有两个核心部分:IOC 和 Aop - (1) IOC : 控制反转,把创建对象过程给 Spring 进行管理 - (2) Aop: 面向切面,不修改源代码进行功能增强 
- Spring 特点 - (1)方便解耦,简化开发 - (2)Aop 编程支持 - (3)方便程序测试 - (4) 方便和其他框架进行整合 - (5)方便进行事务操作 - (6)降低 API 开发难度 
Spring5 入门案例
- 使用 idea 创建一个普通的 java 工程 
- 导入 Spring5 相关的 jar 包(先去 Spring 官网下载)  
 

- 创建普通类,在这个类创建普通方法
| 12
 3
 4
 5
 
 | public class User{public void add(){
 System.out.println("add....");
 }
 }
 
 | 
- 创建 Spring 配置文件,在配置文件配置创建的对象(bean1.xml)

| 12
 3
 4
 5
 6
 7
 
 | <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 <!--配置User对象的创建-->
 <bean id="user" class="com.oy.online.spring.User"></bean>
 </beans>
 
 | 
5.进行测试代码编写
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | @Testpublic void userTest(){
 
 ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
 
 
 User user = context.getBean("user", User.class);
 System.out.println(user);
 user.add();
 }
 
 | 

一、IOC(概念和原理)
1、IOC
2、IOC 底层原理

二、 IOC(BeanFactory 接口)
- IOC 思想基于 IOC 容器完成,IOC 容器底层就是对象工厂 
- Spring 提供 IOC 容器实现的两种方式:(两个接口) - (1) BeanFactory: IOC 容器基本实现,是 Spring 内部的使用接口,不提供开发人员进行使用 -  加载配置文件时候不会加载,在获取对象(使用)才会去创建对象 - (2)ApplicationContext: BeanFactory 接口的子接口,提供更多更强大的功能,一般有开发人员进行使用 -  加载配置文件时候就会把配置文件对象进行创建 
- ApplicationContext 接口有实现类 

三、IOC 操作 Bean 管理(概念)
1、Bean 管理
- Bean 管理指的是两个操作
- Spring 创建对象
- Spring 注入属性
2、Bean 管理的操作的两种方式
- 基于 xml 配置文件方式实现
- 基于注解方式实现
四、IOC 操作 Bean 管理(基于 xml 方式)
1、基于 xml 方式创建对象
| 12
 
 | <!--配置User对象的创建--><bean id="user" class="com.oy.online.spring.User"></bean>
 
 | 
(1) 在 spring 配置文件中,使用 bean 标签,标签里面添加对应属性,就可以实现对象的创建
(2) 在 bean 标签有很多属性,介绍常用属性
- Id 属性 : 唯一标识符
- class 属性:类全路径(包类路径)
(3) 创建对象时候,默认也是执行无参构造器完成对象的创建
2、基于 xml 方式注入属性
(1)DI:依赖注入,就是注入属性
3、第一种注入方式:使用 set 方法进行注入
 (1) 创建类,定义属性和对应的 set 方法
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 
 | public class Book {
 private String bname;
 private String bauthor;
 
 public void setBname(String bname){
 this.bname = bname;
 }
 public void setBauthor(String bauthor){
 this.bauthor = bauthor;
 }
 
 
 public void show(){
 System.out.println(bname+"::"+bauthor);
 }
 }
 
 | 
 (2) 在 spring 配置文件配置对象创建,配置属性注入
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | <!--配置Book对象的创建--><!--set 方法注入属性-->
 <bean id="book" class="com.oy.online.spring.Book">
 <!--使用property 完成属性的注入
 name : 类里面属性名称
 value: 向属性注入的值
 -->
 <property name="bname" value="Java编程之美"></property>
 <property name="bauthor" value="Java"></property>
 </bean>
 
 | 
4、第二种注入方式:使用有参数构成进行注入
 (1) 创建类,定义属性,创建属性对应有参数构造方法
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | public class Order {
 private String oname;
 private String address;
 
 
 public Order(String oname, String address){
 this.oname = oname;
 this.address = address;
 }
 
 
 public void show(){
 System.out.println(oname+"::"+address);
 }
 }
 
 | 
 (2) 在 spring 配置文件中的进行配置
| 12
 3
 4
 5
 
 | <!--有参构造函数注入属性--><bean id="order" class="com.oy.online.spring.Order">
 <constructor-arg name="oname" value="电脑"></constructor-arg>
 <constructor-arg name="address" value="China"></constructor-arg>
 </bean>
 
 | 
5、p 空间名称注入(了解)
 (1) 使用 p 名称空间注入,可以简化基于 xml 配置方式
第一步 添加 p 名称空间在配置文件中
xmlns:p=”http://www.springframework.org/schema/p"
| 12
 3
 4
 5
 
 | <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 			   http://www.springframework.org/schema/beans/spring-beans.xsd">
 </beans>
 
 | 
第二步 进行属性注入,在 bean 标签里面进行操作
| 12
 
 | <!--set 方法注入属性--><bean id="book" class="com.oy.online.spring.Book" p:bname="java编程之美" p:bauthor="java"></bean>
 
 | 
五、IOC 操作 Bean 管理(xml 注入其他类型属性)
1、 字面量
(1)null 值
| 12
 3
 
 |  <property name="address"><null/>
 </property>
 
 | 
 (2) 属性包含特殊符号
| 12
 3
 4
 5
 6
 7
 
 | <!--属性值包含特殊符号1 把<>进行转义 < >
 2 把带特殊符号内容写到 CDATA
 -->
 <property name="address">
 <value><![CDATA[<<南京>>]]></value>
 </property>
 
 | 
2、注入属性-外部 bean
(1) 创建两个类 service 类 和 dao 类
(2) 在 service 调用 dao 里面的方法
(3) 在 spring 配置文件中进行配置
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | public class UserService {
 private UserDao userDao;
 public void setUserDao(UserDao userDao) {
 this.userDao = userDao;
 }
 public void add() {
 System.out.println("service add...............");
 userDao.update();
 }
 }
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | <!--1 service 和 dao 对象创建--><bean id="userService" class="com.atguigu.spring5.service.UserService">
 <!--注入 userDao 对象
 name 属性:类里面属性名称
 ref 属性:创建 userDao 对象 bean 标签 id 值
 -->
 <property name="userDao" ref="userDaoImpl"></property>
 </bean>
 <bean id="userDaoImpl" class="com.atguigu.spring5.dao.UserDaoImpl"></bean>
 
 | 
3、注入属性-内部 bean
(1) 一对多关系:部门和员工
 一个部门有多个员工,一个员工属于一个部门
 部门是一,员工是多
(2)在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示
 部门:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | public class Dept {private String dname;
 
 public void setDname(String dname) {
 this.dname = dname;
 }
 
 @Override
 public String toString() {
 return "Dept{" +
 "dname='" + dname + '\'' +
 '}';
 }
 }
 
 | 
 员工:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 
 | public class Emp {private String ename;
 private String gender;
 
 private Dept dept;
 
 public void setEname(String ename) {
 this.ename = ename;
 }
 
 public void setGender(String gender) {
 this.gender = gender;
 }
 
 public void setDept(Dept dept) {
 this.dept = dept;
 }
 
 public void show(){
 System.out.println(ename + "::" + gender + "::" + dept);
 }
 }
 
 | 
 (3) 在 Spring 配置文件中进行配置
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | <!--内部bean--><bean id="emp" class="com.oy.online.spring.bean.Emp">
 <!--设置两个普通属性-->
 <property name="ename" value="jack"></property>
 <property name="gender" value="女"></property>
 <!--设置对象类型属性-->
 <property name="dept">
 <bean id="dept" class="com.oy.online.spring.bean.Dept">
 <property name="dname" value="设计部"></property>
 </bean>
 </property>
 </bean>
 
 | 
 测试:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 |  @Testpublic void EmpTest(){
 
 ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
 
 
 Emp emp = context.getBean("emp", Emp.class);
 System.out.println(emp);
 emp.show();
 }
 
 | 

4、注入属性-级联赋值
- 第一种写法 | 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | <bean id="emp" class="com.oy.online.spring.bean.Emp">
 
 <property name="ename" value="lucy"></property>
 <property name="gender" value="女"></property>
 
 <property name="dept" ref="dept"></property>
 </bean>
 <bean id="dept" class="com.oy.online.spring.bean.Dept">
 <property name="dname" value="技术部"></property>
 </bean>
 
 |  
 
- 第二种写法  
 | 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 |   <bean id="emp" class="com.oy.online.spring.bean.Emp">
 
 <property name="ename" value="lucy"></property>
 <property name="gender" value="女"></property>
 
 <property name="dept" ref="dept"></property>
 <property name="dept.dname" value="财务部"></property>
 </bean>
 <bean id="dept" class="com.oy.online.spring.bean.Dept">
 <property name="dname" value="技术部"></property>
 </bean>
 
 |  
 
六、IOC 操作 Bean 管理(xml 注入集合属性)
1、注入数组类型属性
2、注入 List 集合类型属性
3、注入 Map 集合属性
(1) 创建类,定义属性、list、map、set 类型属性,生成对应的 set 方法
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 
 | public class Stu {
 private String[] courses;
 
 private List<String> list;
 
 private Map<String, String> maps;
 
 private Set<String> set;
 
 public void setCourses(String[] courses) {
 this.courses = courses;
 }
 
 public void setList(List<String> list) {
 this.list = list;
 }
 
 public void setMaps(Map<String, String> maps) {
 this.maps = maps;
 }
 
 public void setSet(Set<String> set) {
 this.set = set;
 }
 
 @Override
 public String toString() {
 return "Stu{" +
 "courses=" + Arrays.toString(courses) +
 ", list=" + list +
 ", maps=" + maps +
 ", set=" + set +
 '}';
 }
 }
 
 | 
 (2)在 Spring 配置文件进行配置
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 
 | <bean id="stu" class="com.oy.online.Spring.Stu">
 
 <property name="courses">
 <array>
 <value>java课程</value>
 <value>数据库课程</value>
 </array>
 </property>
 
 <property name="list">
 <list>
 <value>张三</value>
 <value>李四</value>
 </list>
 </property>
 
 <property name="maps">
 <map>
 <entry key="Java" value="java"></entry>
 <entry key="PHP" value="php"></entry>
 </map>
 </property>
 
 <property name="set">
 <set>
 <value>MySql</value>
 <value>Redis</value>
 </set>
 </property>
 </bean>
 
 | 
4、在集合里面设置对象类型值
 在 Stu 中添加属性和方法:
| 12
 3
 4
 5
 6
 
 | private List<Course> courseList;
 
 public void setCourseList(List<Course> courseList) {
 this.courseList = courseList;
 }
 
 | 
在 Spring 配置文件中
| 12
 3
 4
 5
 6
 7
 
 | <property name="courseList">
 <list>
 <ref bean="course1"></ref>
 <ref bean="course2"></ref>
 </list>
 </property>
 
 | 
| 12
 3
 4
 5
 6
 7
 
 | <bean id="course1" class="com.oy.online.Spring.Course">
 <property name="cname" value="Spring5框架"></property>
 </bean>
 <bean id="course2" class="com.oy.online.Spring.Course">
 <property name="cname" value="Mybaits框架"></property>
 </bean>
 
 | 
5、把集合注入部分提取出来
(1)在 Spring 配置文件中引入名称 util
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
| 12
 3
 4
 5
 
 | <?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:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
 
 | 
(2) 使用 util 标签完成 list 集合注入提取
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | <util:list id="bookList">
 <value>java编程之美</value>
 <value>java核心卷</value>
 <value>MYSQL必知必会</value>
 </util:list>
 
 <bean id="book" class="com.oy.online.Spring.book">
 <property name="list" ref="bookList"></property>
 </bean>
 
 | 
七、IOC 操作 Bean 管理(FactoryBean)
- Spring 有两种类型 bean, 一种普通 bean, 另外一种工厂 bean(FactroyBean)
- 普通 bean: 在配置文件中定义 bean 类型就是返回类型
- 工厂 bean:在配置文件定义 bean 类型可以和返回类型不一样- 第一步 创建类,让这个类为工厂 bean,实现接口 FactoryBean
- 第二步 实现接口里面的方法,在实现的方法中定义返回的 bean 类型
 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 
 | public class Mybean implements FactoryBean {
 @Override
 public Course getObject() throws Exception {
 Course course = new Course();
 course.setCname("abc");
 return course;
 }
 
 @Override
 public Class<?> getObjectType() {
 return null;
 }
 
 @Override
 public boolean isSingleton() {
 return false;
 }
 }
 
 | 
| 1
 | <bean id="myBean" class="com.oy.online.Spring.bean.Mybean"></bean>
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | @org.junit.Testpublic void FactoryTest(){
 
 ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
 
 
 Course course = context.getBean("myBean", Course.class);
 System.out.println(course);
 }
 
 | 
八、IOC 操作 Bean 管理(bean 作用域)
1.在 Spring 里面,默认情况下,bean 是单实例对象
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | @org.junit.Testpublic void  BookTest(){
 
 ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
 
 
 book book1 = context.getBean("book", book.class);
 book book2 = context.getBean("book", book.class);
 System.out.println(book1);
 System.out.println(book2);
 }
 
 | 

2.设置单实例或多实例
- 在 Spring 配置文件 bean 标签里面有属性**(scope)**用于设置单实例还是多实例
- scope 属性值- 第一个值 默认值,singleton,表示是单实例
- 第二个值 prototype, 表示第多实例对象
 
| 12
 3
 
 | <bean id="book" class="com.oy.online.Spring.book" scope="prototype"><property name="list" ref="bookList"></property>
 </bean>
 
 | 

- singleton 和 prototype 区别 - 第一 singleton 单实例,prototype 多实例 
- 第二 设置 scope 值是 singleton 时候,加载 spring 配置文件时候就会创建单实例对象。 -  设置 scope 值是 prototype 时候,不是加载 spring 配置文件时候创建 对象,在调用 getBean 方法时候创建多实例对象。 
 
九、IOC 操作 Bean 管理(bean 生命周期)
1、生命周期
2、bean 生命周期
- 通过构造器创建 bean 实例(无参数构造)
- 为 bean 的属性设置值 和 对其他 bean 引用(调用 set 方法)
- 调用 bean 的初始化的方法(需要进行配置初始化方法)
- bean 可以使用了(对象获取到了)
- 当容器关闭时候,调用 bean 的销毁方法(需要进行配置销毁的方法)
3、演示 bean 生命周期
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | public class Orders {private String oname;
 
 
 public Orders(){
 System.out.println("第一步 执行无参构造创建 bean 实例");
 }
 
 public void setOname(String oname) {
 this.oname = oname;
 System.out.println("第二步 调set方法设置属性值");
 }
 
 
 public void initMethod(){
 System.out.println("第三步 执行初始化的方法");
 }
 
 
 public void destroyMethod(){
 System.out.println("第五步 执行销毁的方法");
 }
 }
 
 | 
| 12
 3
 
 | <bean id="orders" class="com.oy.online.Spring.bean.Orders" init-method="initMethod"  destroy-method="destroyMethod"><property name="oname" value="手机"></property>
 </bean>
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 |     @org.junit.Testpublic void OrdersTest(){
 
 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
 
 Orders orders = context.getBean("orders", Orders.class);
 System.out.println("第四步 获取创建Bean 实例对象");
 System.out.println(orders);
 
 context.close();
 }
 
 | 

4、bean 的后置处理器,bean 生命周期
- 通过构造器创建 bean 实例(无参构造器)
- 为 bean 的属性设置值和其他 bean 引用(调用 set 方法)
- **把 bean 的实例传递 bean 后置处理器的方法 **postProcessBefoInitialization
- 调用 bean 的初始化的方法
- 把 bean 实例传递 bean 后置处理器的方法 postProcessAfterInitialization
- bean 可以使用(对象获取到了)
- 当前容器关闭时,调用 bean 的销毁方法(需要精心配制销毁的方法)
5、添加后置处理器
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | public class MybeanPost implements BeanPostProcessor {@Override
 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
 System.out.println("在初始化之前执行的方法");
 return bean;
 }
 
 @Override
 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
 System.out.println("在初始化之后执行的方法");
 return bean;
 }
 }
 
 | 
| 12
 
 | <bean id="myBeanPost" class="com.oy.online.Spring.bean.MybeanPost"></bean>
 
 | 

十、IOC 操作 Bean 管理(XML 自动装配)
1、自动装配
- 根据指定装配规则(属性名称或者属性类型),Spring 自动将匹配的属性值进行注入
2、自动装配过程
(1)根据属性名称自动注入
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 
 | public class Emp {private Dept dept;
 
 public void setDept(Dept dept) {
 this.dept = dept;
 }
 
 @Override
 public String toString() {
 return "Emp{" +
 "dept=" + dept +
 '}';
 }
 public void test(){
 System.out.println(dept);
 }
 }
 
 | 
| 12
 3
 4
 5
 6
 
 | public class Dept {@Override
 public String toString() {
 return "Dept{}";
 }
 }
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | 
 
 
 
 
 <bean id="emp" class="com.oy.online.Spring.autowire.Emp" autowire="byName"></bean>
 
 <bean id="dept" class="com.oy.online.Spring.autowire.Dept"></bean>
 
 | 
(2) 根据属性类型自动注入
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | 
 
 
 
 
 <bean id="emp" class="com.oy.online.Spring.autowire.Emp" autowire="byType"></bean>
 
 <bean id="dept" class="com.oy.online.Spring.autowire.Dept"></bean>
 
 | 
十一、IOC 操作 Bean 管理(外部属性文件)
- 直接配置数据库信息 - (1)配置德鲁伊连接池 - (2)引入德鲁伊连接池依赖 jar 包  
 | 12
 3
 4
 5
 6
 7
 
 | <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
 <property name="driver" value="com.mysql.jdbc.Driver"></property>
 <property name="url" value="jdbc:mysql://localhost:8080/test"></property>
 <property name="username" value="root"></property>
 <property name="password" value="root"></property>
 </bean>
 
 |  
 
- 引入外部属性文件配置数据库连接池 - (1) 创建外部属性文件,properties 格式文件,写数据库信息 
 | 12
 3
 4
 
 | prop.driverClass=com.mysql.jdbc.Driverprop.url=jdbc:mysql://localhost:8080/test
 prop.username=root
 prop.password=root
 
 |  
 
-  (2) 把外部 properties 属性文件引入到 spring 配置文件中 | 12
 3
 4
 5
 
 | <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">
 
 |  
 
- 在 spring 配置文件使用标签引入外部属性文件
 | 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | <context:property-placeholder location="classpath:jdbc.properties"/>
 
 
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
 <property name="driverClassName" value="${prop.driverClass}"></property>
 <property name="url" value="${prop.url}"></property>
 <property name="username" value="${prop.username}"></property>
 <property name="password" value="${prop.password}"></property>
 </bean>
 
 |  
 
十二、IOC 操作 Bean 管理(基于注解方式)
1、注解
 (1)注解是代码的特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值…)
 (2)使用注解,注解作用在类上面,方法上面,属性上面
 (3)使用注解目的:简化 xml 配置
2、Spring 针对 Bean 管理中创建对象提供注解
 (1)@Component
 (2)@Service
 (3)@Controller
 (4)@Repository
上面四个注解功能是一样的,都可以用来创建 bean 实例
3、基于注解方式实现对象创建
第一步 引入依赖

第二步:开始组件扫描
| 12
 3
 4
 5
 
 | 
 
 
 <context:component-scan base-package="com.oy.online.Spring"></context:component-scan>
 
 | 
第三步 创建类,在类上面添加创建对象的注解
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | 
 
 @Component(value = "userService")
 public class UserService {
 public void add() {
 System.out.println("service add.......");
 }
 }
 
 | 
4、开启组件扫描细节设置
| 12
 3
 4
 5
 6
 7
 
 | 
 
 
 <context:component-scan base-package="com.oy.online.Spring" use-defaultfilters="false">
 <context:include-filter type="annotation"expression="org.springframework.stereotype.Controller"/>
 </context:component-scan>
 
 | 
| 12
 3
 4
 5
 6
 7
 
 | 
 
 
 <context:component-scan base-package="com.oy.online.Spring">
 <context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller"/>
 </context:component-scan>
 
 | 
5、基于注解方式实现属性注入
(1)**@Autowired**: 根据属性类型进行自动装配
 第一步:把 service 和 dao 对象创建,在 service 和 dao 类创建对象注解
 第二步:把 service 注入 dao 对象, 在 service 类添加 dao 类型属性,在属性上面使用注解
| 12
 3
 
 | public interface UserDao {public void add();
 }
 
 | 
| 12
 3
 4
 5
 6
 7
 
 | @Repositorypublic class UserDaoImpl implements UserDao{
 @Override
 public void add() {
 System.out.println("dao add...");
 }
 }
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | @Servicepublic class UserService {
 
 
 
 @Autowired
 private UserDao userDao;
 
 public void add(){
 System.out.println("service add....");
 userDao.add();
 }
 }
 
 | 
测试:
| 12
 3
 4
 5
 6
 7
 8
 
 | @Testpublic void test(){
 ApplicationContext context = new ClassPathXmlApplicationContext("bean6.xml");
 
 UserService userService = context.getBean("userService", UserService.class);
 System.out.println(userService);
 userService.add();
 }
 
 | 
(2)**@Qualifier**:根据名称进行注入
这个@Qualifier 注解的使用 ,和上面@Autowired 一起使用
| 12
 3
 4
 5
 6
 
 | 
 
 @Autowired
 @Qualifier(value = "userDaoImpl1")
 private UserDao userDao;
 
 | 
(3)**@Resource**: 可以根据类型注入,可以根据名称注入
| 12
 3
 
 | @Resource(name = "userDaoImpl1")
 private UserDao userDao;
 
 | 
(4)**@Value**:注入普通类型的属性
| 12
 
 | @Value(value = "abc")private String name;
 
 | 
6、完全注解开发
(1)创建配置类,替代 xml 配置文件
| 12
 3
 4
 
 | @Configuration @ComponentScan(basePackages = {"com.oy.online.Spring"})
 public class SpringConfig {
 }
 
 | 
(2)测试类
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | @Testpublic void SpringConfigTest(){
 
 ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
 
 UserService userService = context.getBean("userService", UserService.class);
 System.out.println(userService);
 userService.add();
 }
 
 |