2、Spring_DI

news2024/10/6 20:41:28

DI

1.概述

  • 概述:Dependency Injection 依赖注入,给对象设置属性,曾经我们需要自己去创建 mapper 对象,才能调用,现在交给 spring 创建,并且使用 DI 注入,直接拿来用,程序员就可以更加关注业务代码而不是创建对象(ioc已经创建好了对象,通过DI来拿到对象使用)
  • 给对象设置属性方式:
    • 构造器
    • set 方法
  • spring 也是通过构造器以及set方法来实现属性设置

2.回顾问题

  • 如果只给了 mapper 对象,那么调用的时候会出现空指针

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KbluKU07-1692585169900)(picture/image-20221027112638958.png)]

  • 解决方式:使用 DI 注入,解决方案如下

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kOfek2SM-1692585169902)(picture/image-20221027113117921.png)]

3.构造器依赖注入

3.1.创建学生类

public class Student {
}

3.2.创建Mapper 接口以及实现类

  • 创建 Mapper 接口

    public interface StudentMapper {
        void insert(Student stu);
        
        int delete(Long id);
    }
    
  • 创建 Mapper 实现类

    public class StudentMapperImpl implements StudentMapper{
        public void insert(Student stu) {
            System.out.println("保存学生信息");
        }
    
        public int delete(Long id) {
            System.out.println("删除id="+id+"的学生信息");
            return 1;
        }
    }
    
  • 将 Mapper 交给容器管理

    <bean id="studentMapper" class="cn.sycoder.di.mapper.StudentMapperImpl"></bean>
    

3.3.创建 service 接口以及实现类

  • 创建 service 接口

    public interface IStudentService {
        void insert(Student stu);
    
        int delete(Long id);
    }
    
  • 创建 service 实现类

    public class StudentServiceImpl implements IStudentService {
    
        private StudentMapper mapper;
    
        public void insert(Student stu) {
            mapper.insert(stu);
        }
    
        public int delete(Long id) {
            return mapper.delete(id);
        }
    }
    
  • 将 service 交给容器管理

    <bean id="iStudentService" class="cn.sycoder.di.service.impl.StudentServiceImpl"></bean>
    

3.4.如果没有使用DI注入直接调用

  • 会产生如下问题

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SUVySdXY-1692585247607)(picture/image-20221027115025423.png)]

3.5.配置构造器注入属性

  • 配置 service 构造器

    public class StudentServiceImpl implements IStudentService {
    
        private StudentMapper mapper;
    
        public StudentServiceImpl(StudentMapper mapper){
            this.mapper = mapper;
        }
    
        public void insert(Student stu) {
            mapper.insert(stu);
        }
    
        public int delete(Long id) {
            return mapper.delete(id);
        }
    }
    
  • 配置 xml

    <!--    配置 service-->
        <bean id="iStudentService" class="cn.sycoder.di.service.impl.StudentServiceImpl">
            <constructor-arg name="mapper" ref="studentMapper"></constructor-arg>
        </bean>
    <!--    配置 mapper-->
        <bean id="studentMapper" class="cn.sycoder.di.mapper.StudentMapperImpl"></bean>
    
  • 注意:

    • name:构造器的参数名称

    • ref:配置文件中其它 bean 的名称

    • 图示如下

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9Zu0o5S2-1692585289533)(picture/image-20221027115907311.png)]

3.6.构造器配置多个引用类型参数

  • service

    public class StudentServiceImpl implements IStudentService {
    
        private StudentMapper mapper;
    
        private UserMapper userMapper;
    
        public StudentServiceImpl(StudentMapper mapper,UserMapper userMapper){
            this.mapper = mapper;
            this.userMapper = userMapper;
        }
    
        public void insert(Student stu) {
            mapper.insert(stu);
        }
    
        public int delete(Long id) {
            userMapper.delete(id);
            return mapper.delete(id);
        }
    }
    
  • mapper

    public interface UserMapper {
        int delete(Long id);
    }
    
  • mapper 实现类

    public class UserMapperImpl implements UserMapper{
    
        public int delete(Long id) {
            System.out.println("删除id="+id+"的用户信息");
            return 1;
        }
    }
    
  • 配置

    <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">
    <!--    配置 service-->
        <bean id="iStudentService" class="cn.sycoder.di.service.impl.StudentServiceImpl">
            <constructor-arg name="mapper" ref="studentMapper"></constructor-arg>
            <constructor-arg name="userMapper" ref="userMapper"></constructor-arg>
        </bean>
    <!--    配置学生mapper-->
        <bean id="studentMapper" class="cn.sycoder.di.mapper.StudentMapperImpl"></bean>
    <!--    配置用户mapper-->
        <bean id="userMapper" class="cn.sycoder.di.mapper.UserMapperImpl"></bean>
    </beans>
    

3.7.构造器配置多个基本数据类型参数

  • service

    public class StudentServiceImpl implements IStudentService {
    
        private String name;
    
        private int age;
    
        private StudentMapper mapper;
    
        private UserMapper userMapper;
    
        public StudentServiceImpl(String name,int age,StudentMapper mapper,UserMapper userMapper){
            this.name = name;
            this.age = age;
            this.mapper = mapper;
            this.userMapper = userMapper;
        }
    
        public void insert(Student stu) {
            mapper.insert(stu);
        }
    
        public int delete(Long id) {
            System.out.println( name+":"+age);
            userMapper.delete(id);
            return mapper.delete(id);
        }
    }
    
  • xml

    <?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">
    <!--    配置 service-->
        <bean id="iStudentService" class="cn.sycoder.di.service.impl.StudentServiceImpl">
            <constructor-arg name="userMapper" ref="userMapper"></constructor-arg>
            <constructor-arg name="mapper" ref="studentMapper"></constructor-arg>
            <constructor-arg type="int" value="18"></constructor-arg>
            <constructor-arg type="java.lang.String" value="sy"></constructor-arg>
        </bean>
    <!--    配置学生mapper-->
        <bean id="studentMapper" class="cn.sycoder.di.mapper.StudentMapperImpl"></bean>
    <!--    配置用户mapper-->
        <bean id="userMapper" class="cn.sycoder.di.mapper.UserMapperImpl"></bean>
    </beans>
    
  • 这种方式会存在参数覆盖的问题,解决方式,删除 type 添加 index 属性

    <?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">
    <!--    配置 service-->
        <bean id="iStudentService" class="cn.sycoder.di.service.impl.StudentServiceImpl">
            <constructor-arg name="userMapper" ref="userMapper"></constructor-arg>
            <constructor-arg name="mapper" ref="studentMapper"></constructor-arg>
            <constructor-arg index="2" value="18"></constructor-arg>
            <constructor-arg index="1" value="1"></constructor-arg>
            <constructor-arg type="java.lang.String" value="sy"></constructor-arg>
    
        </bean>
    <!--    配置学生mapper-->
        <bean id="studentMapper" class="cn.sycoder.di.mapper.StudentMapperImpl"></bean>
    <!--    配置用户mapper-->
        <bean id="userMapper" class="cn.sycoder.di.mapper.UserMapperImpl"></bean>
    </beans>
    

    4.setter依赖注入

  • 使用 set 方法实现属性的注入

  • 使用 property 属性

    • name:属性名称
    • value:直接给值
    • ref:其它bean的引用

4.1.创建员工类

public class Employee {
}

4.2.创建 mapper 接口以及实现类

  • mapper 接口

    public interface EmployeeMapper {
        int delete(Long id);
    }
    
  • mapper 实现类

    public class EmployeeMapperImpl implements EmployeeMapper {
        public int delete(Long id) {
            System.out.println("删除当前员工id:"+id);
            return 1;
        }
    }
    

4.3.创建 servie 接口以及实现类

  • 创建 service 接口

    public interface IEmployeeService {
        int delete(Long id);
    }
    
  • 创建 service 接口实现类

    public class EmployeeServiceImpl implements IEmployeeService {
    
        private EmployeeMapper mapper;
    
        public int delete(Long id) {
            return mapper.delete(id);
        }
    }
    

4.4.配置 setter 注入

  • 配置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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--    配置mapper实现类-->
    <!--    配置mapper-->
        <bean id="empMapper" class="cn.sycoder.di.setter.mapper.EmployeeMapperImpl"></bean>
    <!--    配置service-->
        <bean id="empService" class="cn.sycoder.di.setter.service.impl.EmployeeServiceImpl"></bean>
    </beans>
    
  • service 实现中提供 mapper 的setter 方法

    public class EmployeeServiceImpl implements IEmployeeService {
    
        private EmployeeMapper employeeMapper;
    
        public int delete(Long id) {
            return employeeMapper.delete(id);
        }
        
        public void setEmployeeMapper(EmployeeMapper employeeMapper){
            this.employeeMapper = employeeMapper;
        }
    }
    
  • 修改 beans.xml 通过 setter 注入

    <?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"><!--    配置mapper实现类-->
    <!--    配置mapper-->
        <bean id="empMapper" class="cn.sycoder.di.setter.mapper.EmployeeMapperImpl"></bean>
    <!--    配置service-->
        <bean id="empService" class="cn.sycoder.di.setter.service.impl.EmployeeServiceImpl">
            <property name="employeeMapper" ref="empMapper"></property>
        </bean>
    </beans>
    
  • 获取 service 执行 delete 方法

    @Test
        public void testSetDi(){
            final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("DiSetterBeans.xml");
            final IEmployeeService empService = (IEmployeeService) context.getBean("empService");
            empService.delete(2L);
        }
    
  • setter 注入过程分析

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-odF2GG1G-1692585327469)(picture/image-20221027134811805.png)]

    4.5.配置多个 setter 方法注入多个属性

  • 给service 添加新的属性以及新的setter方法

    public class EmployeeServiceImpl implements IEmployeeService {
    
        private EmployeeMapper employeeMapper;
    
        private UserMapper userMapper;
    
        public int delete(Long id) {
            return employeeMapper.delete(id);
        }
    
        public void setEmployeeMapper(EmployeeMapper employeeMapper){
            System.out.println("=======使用 setter 注入=======");
            this.employeeMapper = employeeMapper;
        }
    
        public void setUserMapper(UserMapper mapper){
            this.userMapper = mapper;
        }
    }
    
  • 配置 userMapper 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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--    配置mapper实现类-->
    <!--    配置mapper-->
        <bean id="empMapper" class="cn.sycoder.di.setter.mapper.EmployeeMapperImpl"></bean>
    <!--    配置service-->
        <bean id="empService" class="cn.sycoder.di.setter.service.impl.EmployeeServiceImpl">
            <property name="employeeMapper" ref="empMapper"></property>
        </bean>
    <!--    配置 userMapper-->
        <bean id="userMapper" class="cn.sycoder.di.constructor.mapper.StudentMapperImpl"></bean>
    </beans>
    
  • 通过 setter 注入

    <?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"><!--    配置mapper实现类-->
    <!--    配置mapper-->
        <bean id="empMapper" class="cn.sycoder.di.setter.mapper.EmployeeMapperImpl"></bean>
    <!--    配置service-->
        <bean id="empService" class="cn.sycoder.di.setter.service.impl.EmployeeServiceImpl">
            <property name="employeeMapper" ref="empMapper"></property>
            <property name="userMapper" ref="userMapper"></property>
        </bean>
    <!--    配置 userMapper-->
        <bean id="userMapper" class="cn.sycoder.di.constructor.mapper.UserMapperImpl"></bean>
    </beans>
    
  • 获取 service 操作delete 方法

    @Test
        public void testSetterSDi(){
            final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("DiSetterBeans.xml");
            final IEmployeeService empService = (IEmployeeService) context.getBean("empService");
            empService.delete(2L);
        }
    

4.6.使用 setter 注入简单类型

  • 修改 service 类,提供两个属性 int age = 18,String name = “sy”

    public class EmployeeServiceImpl implements IEmployeeService {
    
        private EmployeeMapper employeeMapper;
    
        private UserMapper userMapper;
        
        private String name;
        
        private int age;
        
        public void setName(String name){
            this.name = name;
        }
        public void setAge(int age){
            this.age = age;
        }
    
        public int delete(Long id) {
            System.out.println(name + ":" + age);
            userMapper.delete(id);
            return employeeMapper.delete(id);
        }
    
        public void setEmployeeMapper(EmployeeMapper employeeMapper){
            System.out.println("=======EmployeeMapper使用 setter 注入=======");
            this.employeeMapper = employeeMapper;
        }
    
        public void setUserMapper(UserMapper mapper){
            System.out.println("=======UserMapper使用 setter 注入=======");
            this.userMapper = mapper;
        }
    }
    
  • 配置 xml 设置值

    <?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"><!--    配置mapper实现类-->
    <!--    配置mapper-->
        <bean id="empMapper" class="cn.sycoder.di.setter.mapper.EmployeeMapperImpl"></bean>
    <!--    配置service-->
        <bean id="empService" class="cn.sycoder.di.setter.service.impl.EmployeeServiceImpl">
            <property name="employeeMapper" ref="empMapper"></property>
            <property name="userMapper" ref="userMapper"></property>
            <property name="name" value="sy"></property>
            <property name="age" value="18"></property>
        </bean>
    <!--    配置 userMapper-->
        <bean id="userMapper" class="cn.sycoder.di.constructor.mapper.UserMapperImpl"></bean>
    </beans>
    
  • 可能出现的问题

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HHHkinex-1692585354055)(picture/image-20221027140105809.png)]

4.7.setter 注入总结

  • 对于引用数据类型来说使用
    • < property name=“” ref=“”>
  • 对于简单数据类型
    • < property name=“” value=“”>

5.集合注入

  • List
  • Set
  • Map
  • Array
  • Properties

5.1.添加CollectiosDemo类

public class CollectionsDemo {
    private List<Integer> list;
    private Map<String,String> map;
    private Set<String> set;
    private Properties properties;
    private int[] arr;

    public void print(){
        System.out.println("list:"+list);
        System.out.println("map:"+map);
        System.out.println("set:"+set);
        System.out.println("properties:"+properties);
        System.out.println("arr:"+ Arrays.toString(arr));
    }

    public void setList(List<Integer> list) {
        this.list = list;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public void setArr(int[] arr) {
        this.arr = arr;
    }
}

5.2.配置 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="collectionsDemo" class="cn.sycoder.collections.CollectionsDemo">
<!--        注入 list-->
        <property name="list">
            <list>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="name" value="sy"/>
                <entry key ="age" value="18"/>
            </map>
        </property>
        <property name="set">
            <set>
                <value>just some string</value>
                <value>just string</value>
            </set>
        </property>
        <property name="properties">
            <props>
                <prop key="url">@example.org</prop>
                <prop key="user">root</prop>
                <prop key="password">123456</prop>
            </props>
        </property>
        <property name="arr">
            <array>
                <value>2</value>
                <value>2</value>
                <value>2</value>
            </array>
        </property>
    </bean>
</beans>
  • 如果不提供setter 方法会出现如下错误

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XgfkMr5C-1692585496263)(picture/image-20221027144758504.png)]

6.自动装配

1.概述

  • 概述:IOC容器根据bean所依赖的属性,自动查找并进行自动装配。

2.分类

  • 不启用自动装配
  • byName 通过名称
  • byType 通过类型
  • constructor 通过构造器

3.实操

  • 准备工作

    public class EmployeeService {
        private EmployeeMapperImpl employeeMapper;
        public int delete(Long id) {
            return employeeMapper.delete(id);
        }
        public void setEmployeeMapper(EmployeeMapperImpl employeeMapper){
            System.out.println("=======EmployeeMapper使用 setter 注入=======");
            this.employeeMapper = employeeMapper;
        }
    }
    
    public class EmployeeMapperImpl{
        public int delete(Long id) {
            System.out.println("删除当前员工id:"+id);
            return 1;
        }
    }
    
  • 配置 bean 并且通过 bype 自动装配

    <?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">
        <bean id="empService" class="cn.sycoder.autowired.EmpService" autowire="byType"></bean>
        <bean id="empMapperImpl" class="cn.sycoder.autowired.EmpMapperImpl"></bean>
    
    </beans>
    
  • 配置 bean 并且通过 byName 自动装配

    <?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">
        <bean id="empService" class="cn.sycoder.autowired.EmpService" autowire="byName"></bean>
        <bean id="empMapperImpl" class="cn.sycoder.autowired.EmpMapperImpl"></bean>
    
    </beans>
    
  • 通过名称和类型的自动装配

    • byName

      • 使用 id 或者是 name 别名
      • 如果自动注入时,有多个相同对象,只能使用 byName
    • byType

      • 根据类型注入

      • 通过 byType 注入要保证容器中只有一个 bean 对象,否则会出现如下错误

        [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UdeMkLo9-1692608089557)(picture/image-20221027154317294.png)]

  • 注意:

    • 自动注入的优先级是低于 setter 和 构造器注入的
    • 自动注入只能用于引用类型,不能用于基本数据类型
    • 推荐使用 byType 方式实现自动注入
    • 注入流程
      • byType 根据 getClass 去注入
      • byName 根据属性名称去注入

7.bean scopes

  • 常见的作用域

    作用域说明
    singleton单例的
    prototype多例
    request请求
    session会话
  • 单例 singleton

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QMfMrBhZ-1692608089561)(picture/image-20221027162413097.png)]

  • 修改对象变成多个实例的

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qGhkZpdS-1692608089562)(picture/image-20221027162515954.png)]

  • 注意:容器模式就是以单例的方式创建对象的,如果需要修改成非单例,使用 scope 属性修改即可

  • 以后开发中适合将那些bean对象交给 spring 管理

    • 持久层 mapper
    • 业务层 service
    • 控制层 controller
  • 单例bean会出现线程安全吗

    • 判断bean 对象是否存储数据,如果用来存储数据了,会导致线程安全问题
    • 使用局部变量做存储,方法调用结束就销毁了,所以不存在线程安全问题

8.bean 生命周期

1.概述

  • 概述:生命周期就是一个对象从出生到死亡的过程

2.使用用户类观察生命周期

  • 创建用户类

    public class User {
        private String name;
        public User(){
            System.out.println("构造器执行====");
        }
    
        public void setName(String name) {
            System.out.println("调用 set 方法");
            this.name = name;
        }
        
        public void init(){
            System.out.println("调用 init 方法");
        }
        
        public void destroy(){
            System.out.println("调用销毁方法");
        }
    }
    
  • 配置 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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean id="user" class="cn.sycoder.lifecycle.User" init-method="init" destroy-method="destroy">
            <property name="name" value="sy"></property>
        </bean>
    </beans>
    
  • 获取 bean 出现如下问题,没有打印销毁方法

    • 原因:

      • spring ioc 容器是运行在 jvm 虚拟机中的

      • 执行 test 方法后 jvm 虚拟机开启,spring 加载配置文件创建 bean 对象,调用构造器以及 init 方法

      • test 方法执行完毕的时候, jvm 退出,spring ioc 容器来不及关闭销毁 bean,所以没有去调用 destroy 方法

        [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yBA6JiDb-1692608089564)(picture/image-20221027165336449.png)]

    • 解决办法,正常关闭容器

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5Lh12ibv-1692608089565)(picture/image-20221027165723773.png)]

3.BeanPostProcessor

  • 自定义自己 bean 处理器

    public class MyBeanPostProcessor  implements BeanPostProcessor{
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            //bean 前置处理器
            System.out.println("bean 的前置处理器");
            return bean;
        }
    
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("bean 的后置处理器");
            //bean 后置处理器
            return bean;
        }
    }
    
  • 配置 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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean id="user" class="cn.sycoder.lifecycle.User" init-method="init" destroy-method="destroy">
            <property name="name" value="sy"></property>
        </bean>
    
        <bean class="cn.sycoder.lifecycle.MyBeanPostProcessor"></bean>
    </beans>
    

4.生命周期总结

  • bean 对象创建(调用无参构造器)
  • 设置属性通过 setter 方法
  • init 方法前调用 bean 的前置处理器
  • bean 的 init 方法
  • bean 的后置处理器
  • 对象可以正常使用
  • destroy 销毁方法
  • ioc 容器关闭
  • jvm 虚拟机的退出

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/910298.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

小说作者分享:如何利用爱校对使我的作品更出彩?

在创作小说的过程中&#xff0c;校对和修改是至关重要的步骤。许多作家已经开始利用“爱校对”这一工具&#xff0c;有效地提高他们作品的质量。本篇文章将通过实际案例&#xff0c;展示一些小说作者是如何成功地利用爱校对来精雕细琢他们的文字&#xff0c;并将作品提升到一个…

数据结构,二叉树,前中后序遍历

二叉树的种类 最优二叉树 最优二叉树画法 排序取最小两个值和&#xff0c;得到新值加入排序重复1&#xff0c;2 前序、中序和后序遍历是树形数据结构&#xff08;如二叉树&#xff09;中常用的遍历方式&#xff0c;用于按照特定顺序遍历树的节点。这些遍历方式在不同应用中有不…

x86 分段机制

本篇只是做个记录,把重要的东西记录一下. 不保证正确x86中的物理地址计算过程 8086 时代的分段机制 这要从8086实模式说起 8086实模式 是基于 分段机制的.该模式下有段寄存器该模式下存在 逻辑地址[a:b] 和 (线性地址) 物理地址 // a 存储在段寄存器中,[a:b] 存储了 逻辑地址…

记一次由于整型参数错误导致的任意文件上传

当时误打误撞发现的&#xff0c;觉得挺奇葩的&#xff0c;记录下 一个正常的图片上传的点&#xff0c;文件类型白名单 但是比较巧的是当时刚对上面的id进行过注入测试&#xff0c;有一些遗留的测试 payload 没删&#xff0c;然后在测试上传的时候就发现.php的后缀可以上传了&a…

PostMan 测试项目是否支持跨域

使用PostMan可以方便快速的进行跨域测试。 只需要在请求头中手动添加一个Origin的标头&#xff0c;声明需要跨域跨到的域&#xff08;IP&#xff1a;端口&#xff09;就行&#xff0c;其余参数PostMan会自动生成。添加此标头后&#xff0c;请求会被做为一条跨域的请求来进行处…

Python编程基础-文件的打开和读取

文件的访问 使用 open() 函数 打开文件 &#xff0c;返回一个 file 对象 使用 file 对象的读 / 写方法对文件进行读 / 写的 操作 使用 file 对象的 close() 方法关闭文件 打开文件 open()方法&#xff0c;需要一个字符串路径&#xff0c;并返回一个文件对象&#xff0c;默认是”…

学习maven工具

文章目录 &#x1f412;个人主页&#x1f3c5;JavaEE系列专栏&#x1f4d6;前言&#xff1a;&#x1f3e8;maven工具产生的背景&#x1f993;maven简介&#x1fa80;pom.xml文件(project object Model 项目对象模型) &#x1fa82;maven工具安装步骤两个前提&#xff1a;下载 m…

Android基础——英文复习资料

一.填空题 1.An Android project must be bulit before it is run&#xff0c;compiling the java source code(.java flises)into Java bytetcode(.class files)and the into .dex files 2.In Android an activity stores the code for a screen of an app&#xff0c; …

.NET6.0 System.Drawing.Common 通用解决办法

最近有不少小伙伴在升级 .NET 6 时遇到了 System.Drawing.Common 的问题&#xff0c;同时很多库的依赖还都是 System.Drawing.Common &#xff0c;而 .NET 6 默认情况下只在 Windows 上支持使用&#xff0c;Linux 上默认不支持这就导致在 Linux 环境上使用会有问题&#xff0c;…

# 电脑好用的工具推荐

电脑好用的工具推荐 文章目录 电脑好用的工具推荐必装工具浏览器火绒安全卸载工具Geek迅雷 记笔记工具Typora印象笔记 开发工具IntelliJ IDEAVisual Studio CodeDbeaverAnother Redis Desktop Manager 备份工具百度网盘阿里云盘一刻相册蓝奏云 必装工具 浏览器 就别装那些乱七…

vue2.x项目从0到1(七)之用户权限

此章节偏理论知识 对于小一点的项目 比如说角色都是平级的 那我们直接像之前 vue2.x项目从0到1&#xff08;二&#xff09;之后台管理侧边栏&#xff08;动态渲染路由以及高亮&#xff09;_vue动态渲染侧边栏_关忆北_的博客-CSDN博客这样渲染就行了 但是一旦项目大了 …

Reids 的整合 Spring Data Redis使用

大家好 , 我是苏麟 , 今天带来强大的Redis . REmote DIctionary Server(Redis) 是一个由 Salvatore Sanfilippo 写的 key-value 存储系统&#xff0c;是跨平台的非关系型数据库。 Redis 是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式、可选…

如何使用CSS实现一个拖拽排序效果?

聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ 实现拖拽排序效果的CSS和JavaScript示例⭐ HTML 结构⭐ CSS 样式 (styles.css)⭐ JavaScript 代码 (script.js)⭐ 实现说明⭐ 写在最后 ⭐ 专栏简介 前端入门之旅&#xff1a;探索Web开发的奇妙世界 记得点击上方或者右侧链接订阅本专栏哦…

【HCIP】02.MSTP

运行RSTP/STP&#xff0c;局域网内所有的VLAN共享一棵生成树&#xff0c;被阻塞后的链路将不承载任何流量&#xff0c;无法在VLAN间实现数据流量的负载均衡&#xff0c;导致链路带宽利用率、设备资源利用率较低。802.1S,MSTP兼容STP和RSTP&#xff0c;通过建立多棵无环路的树&a…

论文及代码详解——HRNet

文章目录 论文详解 &#xff08;High-Resolution Networks&#xff09;Parallel Multi-Resolution ConvolutionsRepeated Multi-Resolution FusionsRepresentation Head 代码详解 论文&#xff1a;《Deep High-Resolution Representation Learning for Visual Recognition》 代…

MySQL报错:Incorrect integer value: ‘None‘ for column ‘managerId‘ at row 1

错误如下&#xff1a; 今天在建表的时候突然报错 上网查询了原因&#xff0c;把None改成NULL就行了。 5以上的版本如果是空值应该要写NULL&#xff0c;这种问题一般mysql 5.x上出现。 改完后就运行正常了

07 mysql5.6.x docker 启动, 无 config 目录导致客户端连接认证需要 10s

前言 呵呵 最近再一次 环境部署的过程中碰到了这样的一个问题 我基于 docker 启动了一个 mysql 服务, 然后 挂载出了 数据目录 和 配置目录, 没有手动复制配置目录出来, 所以配置目录是空的 然后 我基于 docker 启动了一个 nacos, 配置数据库设置为上面的这个 mysql 然后 启…

【2023年11月第四版教材】《第6章-项目管理概论》(第二部分)

《第6章-项目管理概论》&#xff08;第二部分&#xff09; 3 项目经理的角色3.1 项目经理的影响力范围3.2 项目经理领导力风格 4 价值驱动的项目管理知识体系4.1 开发生命周期类型 5 五大过程组6 五个过程组和十大知识领域 3 项目经理的角色 3.1 项目经理的影响力范围 范围影…

从业务层的代码出发,去排查通用框架代码崩溃的问题

目录 1、问题说明 1.1、Release下崩溃&#xff0c;Debug下很难复现 1.2、用Windbg打开dump文件&#xff0c;发现崩溃在通用的框架代码中 2、进一步分析 2.1、使用IDA查看汇编代码尝试寻找崩溃的线索 2.2、在Windbg中查看相关变量的值 2.3、查看最近代码的修改记录&#…

渗透和红队快速打点工具

&#x1f525; POC-bomber &#x1f984; POC bomber 是一款漏洞检测/利用工具&#xff0c;旨在利用大量高危害漏洞的POC/EXP快速获取目标服务器权限 本项目收集互联网各种危害性大的 RCE 任意文件上传 反序列化 sql注入 等高危害且能够获取到服务器核心权限的漏洞POC/EXP…