【Spring】2、 IOC

IOC(概念和原理)

  1. 什么是 IOC(Inversion of Control)

    • 控制反转,把对象创建和对象之间的调用过程,交给 Spring 进行管理

    • 使用 IOC 的目的:为了耦合度降低

      之前的入门案例就是IOC的实现

  2. IOC 底层原理

    • xml 解析、工厂模式、反射
  3. IOC 过程(比工厂模式进一步降低耦合度)

    • 第一步:xml 配置文件,配置创建的对象

      <bean id="user" class="com.demo1.Spring5.User"></bean>
      
    • 第二步:有 service 类和 dao 类,创建工厂类

      class UserFactory{
          public static UserDao getDao(){
              String classValue = class属性值; //1.xml解析
              Class clazz = Class.forName(classValue);// 2.通过反射创建对象
              return (UserDao) clazz.newInstance();
          }
      }
      

IOC(接口)

  1. IOC 思想基于 IOC 容器完成,IOC 容器底层就是对象工厂

  2. Spring 提供 IOC 实现的两种方式:(两个接口)

    • BeanFactory:IOC 容器基本实现,是 Spring 内部的使用接口,不提供开发人员进行使用

      *加载配置文件的时候不会创建对象,在获取对象(使用)才去创建对象

    • ApplicationContext:BeanFactory 接口的子接口,提供更多更强大的功能,一般由开发人员进行使用

      *加载配置文件的时候就会把在配置文件中的对象进行创建

  3. ApplicationContext接口主要的实现类

    • FileSystemXmlApplicationContext:参数为绝对路径
    • ClassPathXmlApplicationContext:参数为相对路径

IOC 操作 Bean 管理(概述)

  1. 什么是Bean管理
    • Bean 管理指的是两个操作:Spring 创建对象、Spring 注入属性
  2. Bean 管理操作有两种方式
    • 基于 xml 配置文件方式实现
    • 基于注解方式实现

IOC操作 Bean 管理(基于 xml 方式)

  1. 基于 xml 方式创建对象

    <!--1.配置User对象创建-->
    <bean id="user" class="com.demo1.Spring5.User"></bean>
    
    • 在 spring 配置文件中,使用 bean 标签,标签里面添加对应属性,就可以实现对象创建
    • 在 bean 标签里有很多属性,常用属性:
      • id 属性:唯一标识
      • class 属性:类全路径(包类路径)
    • 创建对象时候,默认是执行无参数的构造方法
  2. 基于 xml 方式注入属性

    • DI:依赖注入,就是注入属性

      DI 是 IOC 中一种具体实现,它就表示依赖注入,它需要在创建对象的基础之上进行完成

    第一种注入方式:使用 set 方法进行注入

    1. 创建类,定义属性和对应的 set 方法

      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;
          }
      }
      
    2. 在 spring 配置文件配置对象创建,配置属性注入

          <!--2.set方法注入属性-->
          <bean id="book" class="com.demo1.Spring5.Book">
              <!--使用 property 完成属性注入
                  name:类里面属性名称
                  value:向属性注入的值
               -->
              <property name="bname" value="易筋经"></property>
              <property name="bauthor" value="达摩老祖"></property>
          </bean>
      </beans>
      

    第二种注入方式:使用有参数的构造函数进行注入

    1. 创建类,定义属性,创建属性对应的有参数构造方法

      public class Orders {
          private String oname;
          private String address;
      
          public Orders(String oname, String address) {
              this.oname = oname;
              this.address = address;
          }
      }
      
    2. 在 Spring 配置文件中进行配置

      <!--3.有参数构造注入属性-->
      <bean id="orders" class="com.demo1.Spring5.Orders">
          <constructor-arg name="oname" value="abcd"></constructor-arg>
          <constructor-arg name="address" value="China"></constructor-arg>
      </bean>
      

      <!--3.有参数构造注入属性-->
      <bean id="orders" class="com.demo1.Spring5.Orders">
          <constructor-arg index="0" value="abcd"></constructor-arg>
          <constructor-arg index="1" value="China"></constructor-arg>
      </bean>
      

      index 0表示第一个参数,1表示第二个参数

    p名称空间注入(了解)

    使用p名称空间注入,可以简化基于 xml 配置方式

    1. 第一步 在配置文件汇中添加p名称空间

      <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">
      
    2. 第二步 在 bean 标签里面进行操作,进行属性注入

      <bean id="book" class="com.demo1.Spring5.Book" p:bname="九阳神功" p:bauthor="无名氏">
      </bean>
      

IOC操作 Bean 管理(xml 注入其他类型属性)

  1. 字面量

    • null值
    <!--null值-->
    <property name="address">
        <null/>
    </property>
    
    • 属性值包含特殊符号
    <!--属性值包含特殊符号
        1. 把<>进行转义&lt; &gt;  or
        2. 把带特殊符号内容写到CDATA
    -->
    <property name="bname">
        <value><![CDATA[<<易筋经>>]]></value>
    </property>
    
  2. 注入属性 - 外部bean

    (1)创建两个类 service类 和 dao类

    (2)在 service 调用 dao 里面的方法

    (3)在spring配置文件中进行配置

    实现如下

    UserDao.java

    public interface UserDao {
        public void update();
    }
    

    UserDaoImpl.java

    public class UserDaoImpl implements UserDao{
        @Override
        public void update() {
            System.out.println("dao update...");
        }
    }
    

    UserService.java

    public class UserService {
    
        //创建UserDao类型属性,生成set方法
        private UserDao userDao;
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }
    
        public void add(){
            System.out.println("service add...");
            userDao.update();
            
    /*      此处为原始做法
            //原始方式:创建UserDao对象
            UserDao userDao = new UserDaoImpl();
            userDao.update();
    */
        }
    }
    

    xml配置

    <!--1.service和dao对象创建-->
    <bean id="userService" class="com.demo1.Spring5.service.UserService">
        <!--2.注入userDao对象
            name属性值:类里面属性名称
            ref属性:创建userDao对象bean标签的id值
        -->
        <property name="userDao" ref="userDaoImpl"></property>
    </bean>
    <bean id="userDaoImpl" class="com.demo1.Spring5.dao.UserDaoImpl"></bean>
    
  3. 注入属性 - 内部bean

    (1)一对多关系:部门和员工

    一个部门有多个员工,一个员工属于一个部门;部门是一,员工是多

    (2)在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示

    (3)在spring配置文件中进行配置

    实现如下

    Department.java

    //部门类
    public class Department {
        private String dname;
        public void setDname(String dname) {
            this.dname = dname;
        }
    }
    

    Employee.java

    //员工类
    public class Employee {
        private String ename;
        private String gender;
        //员工属于某一个部门,使用对象形式表示
        private Department department;
    
        public void setEname(String ename) {
            this.ename = ename;
        }
        public void setGender(String gender) {
            this.gender = gender;
        }
        public void setDepartment(Department department) {
            this.department = department;
        }
    }
    

    xml配置

    <!--内部bean-->
    <bean id="employee" class="com.demo1.Spring5.bean.Employee">
        <!--设置两个普通属性-->
        <property name="ename" value="lucy"></property>
        <property name="gender" value=""></property>
        <!--设置对象类型属性-->
        <property name="department">
            <bean id="department" class="com.demo1.Spring5.bean.Department">
                <property name="dname" value="安保部"></property>
            </bean>
        </property>
    </bean>
    
  4. 注入属性 - 级联赋值

    第一种写法

    部门类和员工类与内部bean 中一致,xml 配置如下:

    <!--级联赋值-->
    <bean id="employee" class="com.demo1.Spring5.bean.Employee">
        <!--设置两个普通属性-->
        <property name="ename" value="lucy"></property>
        <property name="gender" value=""></property>
        <!--级联赋值-->
        <property name="department" ref="department"></property>
    </bean>
    <bean id="department" class="com.demo1.Spring5.bean.Department">
        <property name="dname" value="财务部"></property>
    </bean>
    

    第二种写法

    员工类中需要添加部门类的 get 方法,部门类保持不变

    public Department getDepartment() {
        return department;
    }
    

    xml 配置

    <!--级联赋值-->
    <bean id="employee" class="com.demo1.Spring5.bean.Employee">
        <!--设置两个普通属性-->
        <property name="ename" value="lucy"></property>
        <property name="gender" value=""></property>
        <!--级联赋值-->
        <property name="department" ref="department"></property>
        <property name="department.dname" value="技术部"></property>
    </bean>
    <bean id="department" class="com.demo1.Spring5.bean.Department">
    </bean>
    

IOC操作 Bean 管理(xml 注入集合属性)

  • 注入数组类型属性

  • 注入 List 集合类型属性

  • 注入 Map 集合类型属性

  • 注入 Set 集合类型属性

  1. 创建类,定义数组、List、Map、Set 类型属性,生成对应 set 方法

    public class Student {
        //1 数组类型属性
        private String[] courses;
        //2 List集合类型属性
        private List<String> list;
        //3 Map集合类型属性
        private Map<String,String> maps;
        //4 Set集合类型属性
        private Set<String> sets;
    
        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 setSets(Set<String> sets) {
            this.sets = sets;
        }
    }
    
  2. 在 spring 配置文件进行配置

    <bean id="student" class="com.demo1.Spring5.collectiontype.Student">
        <!--1.集合类型属性注入-->
        <property name="courses">
            <array>
                <value>java课程</value>
                <value>数据库课程</value>
            </array>
        </property>
        <!--2.List类型属性注入-->
        <property name="list">
            <list>
                <value>张三</value>
                <value>小张</value>
            </list>
        </property>
        <!--3.Map类型属性注入-->
        <property name="maps">
            <map>
                <entry key="JAVA" value="java"></entry>
                <entry key="PYTHON" value="python"></entry>
            </map>
        </property>
        <!--4.Set类型属性注入-->
        <property name="sets">
            <set>
                <value>MySQL</value>
                <value>Redis</value>
            </set>
        </property>
    </bean>
    

IOC操作 Bean 管理(xml 注入集合属性_进阶)

  1. 在集合里面设置对象类型值

    1)在 student 的 bean 外创建多个对象

    <!--创建多个Course对象-->
    <bean id="course1" class="com.demo1.Spring5.collectiontype.Course">
        <property name="name" value="Spring5框架"></property>
    </bean>
    <bean id="course2" class="com.demo1.Spring5.collectiontype.Course">
        <property name="name" value="MyBatis框架"></property>
    </bean>
    

    2)在 student 的 bean 内注入 List 集合类型

    <!--注入List集合类型,值是对象-->
    <property name="courseList">
        <list>
            <ref bean="course1"></ref>
            <ref bean="course2"></ref>
        </list>
    </property>
    
  2. 把集合注入部分提取出来

    1)在 spring 配置文件中引入名称空间 util

    <?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 集合注入提取

    <!--1 提取List集合类型属性注入-->
    <util:list id="bookList">
        <value>《易筋经》</value>
        <value>《九阴真经》</value>
        <value>《九阳神功》</value>
    </util:list>
    <!--2 提取List集合类型属性注入使用-->
    <bean id="book" class="com.demo1.Spring5.collectiontype.Book">
        <property name="list" ref="bookList"></property>
    </bean>
    

IOC操作 Bean 管理(FactoryBean)

  • Spring 有两种类型 bean,一种普通 bean,另外一种工厂 bean(FactoryBean)

    普通 bean:在配置文件中定义 bean 类型就是返回类型

    工厂 bean:在配置文件定义 bean 类型可以和返回类型不一样

工厂 bean 演示

  1. 第一步 创建类,让这个类作为工厂 bean ,实现接口 FactoryBean
  2. 第二步 实现接口里面的方法,在实现的方法中定义返回的 bean 类型
public class MyBean implements FactoryBean<Course> {

    //定义返回 bean
    @Override
    public Course getObject() throws Exception {
        Course course = new Course();
        course.setName("abc");
        return course;
    }
    @Override
    public Class<?> getObjectType() {
        return null;
    }
    @Override
    public boolean isSingleton() {
        return false;
    }
}
<bean id="myBean" class="com.demo1.Spring5.factorybean.MyBean">
</bean>
@Test
public void testCollection3(){
    ApplicationContext context =
            new ClassPathXmlApplicationContext("bean3.xml");
    Course course = context.getBean("myBean", Course.class);
    System.out.println(course);
}

IOC操作 Bean 管理(bean 的作用域)

  1. 在 Spring 里面,设置创建 bean 实例是单实例还是多实例

  2. 在 Spring 里面,默认情况下,bean 是单实例对象

    @Test
    public void testCollection2(){
        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);
    }
    

    此时,book1 和 book2 的地址是一样的,说明是单实例

  3. 如何设置单实例还是多实例

    • 在 Spring 配置文件 bean 标签里面有属性(scope)用于设置单实例还是多实例

    • scope 属性值

      第一个值:默认值,singleton,表示是单实例对象

      第二个值:prototype,表示是多实例对象

      <bean id="book" class="com.demo1.Spring5.collectiontype.Book" scope="prototype">
          <property name="list" ref="bookList"></property>
      </bean>
      

      重新运行 testCollection2,此时,book1 和 book2 的地址是不一样的,说明是多实例

    • singleton 和 prototype 区别

      • singleton 单实例,prototype 多实例
      • 设置 scope 值是 singleton 时候,加载 spring 配置文件时候就会创建单实例对象;设置 scope 值是 prototype 时候,不是在加载 spring 配置文件时候创建对象,而是在调用 getBean 方法时候创建多实例对象

IOC操作 Bean 管理(bean 生命周期)

  1. 生命周期:从对象创建到对象销毁的过程

  2. bean 的生命周期

    1)通过构造器创建 bean 实例(无参数构造)

    2)为 bean 的属性设置值和对其他 bean 引用(调用 set 方法)

    3)调用 bean 的初始化方法(需要进行配置初始化的方法)

    4)bean 可以使用了(对象获取到了)

    5)当容器关闭的时候,调用 bean 的销毁方法(需要进行配置销毁的方法)

    演示 bean 生命周期

    Orders.java

    public class Orders {
        private String oname;
    
        //无参构造
        public Orders(){
            System.out.println("第一步 执行无参数构造bean实例");
        }
        //set方法
        public void setOname(String oname) {
            this.oname = oname;
            System.out.println("第二步 调用set方法设置属性值");
        }
        //创建执行的初始化方法
        private void initMethod(){
            System.out.println("第三步 执行初始化的方法");
        }
        //创建执行的销毁方法
        private void destroyMethod(){
            System.out.println("第五步 执行销毁的方法");
        }
    }
    

    bean4.xml 配置

    <bean id="orders" class="com.demo1.Spring5.bean.Orders" init-method="initMethod" destroy-method="destroyMethod">
        <property name="oname" value="abcd"></property>
    </bean>
    

    测试方法

    @Test
    public void testBean1(){
        //ApplicationContext context =
        //        new ClassPathXmlApplicationContext("bean4.xml");
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("bean4.xml");
    
        Orders orders = context.getBean("orders", Orders.class);
        System.out.println("第四步 获取创建bean实例对象");
        System.out.println(orders);
    
        //手动销毁bean实例
        context.close();
    

    最后运行,输出结果如下:

    第一步 执行无参数构造bean实例
    第二步 调用set方法设置属性值
    第三步 执行初始化的方法
    第四步 获取创建bean实例对象
    com.demo1.Spring5.bean.Orders@e720b71
    第五步 执行销毁的方法
    
  3. 加上 bean 的后置处理器,bean 生命周期一共有七步

    1)通过构造器创建 bean 实例(无参数构造)

    2)为 bean 的属性设置值和对其他 bean 引用(调用 set 方法)

    3)把 bean 实例传递 bean 后置处理器的方法 postProcessBeforeInitialization

    4)调用 bean 的初始化方法(需要进行配置初始化的方法)

    5)把 bean 实例传递 bean 后置处理器的方法 postProcessAfterInitialization

    6)bean 可以使用了(对象获取到了)

    7)当容器关闭的时候,调用 bean 的销毁方法(需要进行配置销毁的方法)

    演示添加后置处理器后的效果

    1. 创建类,实现接口 BeanPostProcessor,创建后置处理器

      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;
          }
      }
      
    2. 在 xml 中配置后置处理器

      <!--配置后置处理器-->
      <bean id="myBeanPost" class="com.demo1.Spring5.bean.MyBeanPost"></bean>
      
    3. 最后运行,输出结果如下:

      第一步 执行无参数构造bean实例
      第二步 调用set方法设置属性值
      在初始化之前执行的方法
      第三步 执行初始化的方法
      在初始化之后执行的方法
      第四步 获取创建bean实例对象
      com.demo1.Spring5.bean.Orders@74ad1f1f
      第五步 执行销毁的方法
      

IOC操作 Bean 管理(xml 自动装配)

  • 什么是自动装配?

    根据指定的装配规则(属性名称或者属性类型),Spring 自动将匹配的属性值进行注入

演示自动装配过程

  • bean 标签属性 autowire,配置自动装配
    autowire 属性常用两个值:
    • byName:根据属性名称注入,注入bean的id值和类属性名称要一样
    • byType:根据属性类型注入,不能有相同类型的 bean
  1. 根据名称自动注入

    <bean id="employee" class="com.demo1.Spring5.autowire.Employee" autowire="byName">
        <!--<property name="department" ref="department"></property>-->
    </bean>
    <bean id="department" class="com.demo1.Spring5.autowire.Department"></bean>
    
  2. 根据类型自动注入

    <bean id="employee" class="com.demo1.Spring5.autowire.Employee" autowire="byType">
        <!--<property name="department" ref="department"></property>-->
    </bean>
    <bean id="department" class="com.demo1.Spring5.autowire.Department"></bean>
    

IOC操作 Bean 管理(外部属性文件)

  1. 直接配置数据库信息

    1)引入 Druid 连接池依赖 jar 包

    2)配置 Druid 连接池

    <!--直接配置连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/dbem"></property>
        <property name="username" value="root"></property>
        <property name="password" value="mysql700"></property>
    </bean>
    
  2. 引入外部属性文件配置数据库连接池

    1)创建外部属性文件,properties 格式文件,写数据库信息

    prop.driverClass = com.mysql.cj.jdbc.Driver
    prop.url = jdbc:mysql://localhost:3306/dbem
    prop.userName = root
    prop.password = mysql700
    

    2)把外部 properties 属性文件引入到 spring 配置文件中

    *需要先引入名称空间 context *

    <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 配置文件使用标签引入外部属性文件

    <!--引入外部属性文件-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <!--配置连接池-->
    <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. 什么是注解?

    • 注解是代码特殊标记,格式:@注解名称(属性名称 = 属性值, 属性名称 = 属性值 … )
    • 使用注解,注解可以作用在类上面,方法上面,属性上面
    • 使用注解的目的:简化 xml 配置
  2. Spring 针对 Bean 管理中创建对象提供注解:

    • @Component :Spring 容器中提供的普通组件,可直接创建对象
    • @Service :一般用在业务逻辑层(service 层)
    • @Controller :一般用在 web 层(control 层)
    • @Repository :一般用在持久层(dao 层)

    上面四个注解功能是一样的,都可以用来创建 bean 实例

  3. 基于注解方式实现对象创建

    1)第一步 引入依赖 (spring-aop-5.2.6.RELEASE.jar)

    2)第二步 开启组件扫描

    <!--开启组件扫描
        如果扫描多个包
            1 可以使用逗号隔开  或
            2 扫描包上层目录
    -->
    <context:component-scan base-package="com.demo1.Spring5"></context:component-scan>
    

    3)第三步 创建类,在类上面添加创建对象注解

    //在注解里面value属性值可以省略不写
    //其默认值是类名称,首字母小写
    //eg: UserService -- userService
    @Component(value = "userService") //和<bean id="userService" class="..."/>类似
    public class UserService {
        public void add(){
            System.out.println("service add...");
        }
    }
    

IOC操作 Bean 管理(基于注解方式_组件扫描配置)

开启组件扫描中的细节配置

  • 设置扫描哪些内容

    <!--示例1
        use-default-filters="false" : 表示现在不使用默认filter,自己配置filter
        context:include-filter :设置扫描哪些内容,例如下面是扫描@Controller注解
    -->
    <context:component-scan base-package="com.demo1.Spring5" use-default-filters="false">
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
  • 设置不扫描哪些内容

    <!--示例2
        下面配置扫描包里所有内容
        context:exclude-filter :设置哪些内容不进行扫描,例如下面是不扫描@Controller注解
    -->
    <context:component-scan base-package="com.demo1.Spring5">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    

IOC操作 Bean 管理(基于注解方式_注入属性)

Spring 提供以下四种常用注解实现属性注入:@Autowired、@Qualifier、@Resource、@Value

  • @Autowired :根据属性类型进行自动装配

    第一步:创建 service 和 dao 对象,即在 service 和 dao 类添加创建对象注解

    第二步: 在 service 里注入 dao 对象,即在 service 类添加 dao 类型属性,再在属性上面使用注解

    实现如下

    public interface UserDao {
        public void add();
    }
    
    @Repository
    public class UserDaoImpl implements UserDao{
        @Override
        public void add(){
            System.out.println("dao add...");
        }
    }
    
    @Service
    public class UserService {
        //添加注入属性注解,不需要添加set方法
        @Autowired  //根据类型进行注入
        private UserDao userDao;
        
        public void add(){
            System.out.println("service add...");
            userDao.add();
        }
    }
    
  • @Qualifier :根据属性名称进行注入

    这个@Qualifier 注解的使用,要和上面@Autowired 一起使用

        //添加注入属性注解,不需要添加set方法
        @Autowired //根据类型进行注入
        @Qualifier(value = "userDaoImpl1") //根据名称进行注入
        private UserDao userDao;
    
    
  • @Resource :可以根据类型注入,也可以根据名称注入

    需要注意的是,@Resource注解 是 javax 扩展包里的,而 @Autowired、@Qualifier注解 是 Spring 包里的

        //添加注入属性注解,不需要添加set方法
        /*@Resource //根据类型进行注入*/
        @Resource(name = "userDaoImpl1") //根据名称注入
        private UserDao userDao;
    
  • @Value :注入普通类型属性(上面三个注解都是注入对象类型属性)

    	@Value(value = "张三")
    	private String name;
    

IOC操作 Bean 管理(基于注解方式_完全注解开发)

  1. 创建配置类,替代 xml 配置文件

    @Configuration //注解作用:让该类作为配置类,替代xml文件
    @ComponentScan(basePackages = {"com.demo1.Spring5"})
    public class SpringConfig {
    
    }
    
  2. 编写测试类

    @Test
    public void testService2(){
        //加载配置类
        ApplicationContext context
                = new AnnotationConfigApplicationContext(SpringConfig.class);
        UserService userService = context.getBean("userService",UserService.class);
        System.out.println(userService);
        userService.add();
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值