Spring 关于 Bean 的生命周期

Bean 的生命周期

Class(UserService) -----> 推断构造方法 -----> 实例化 -----> 对象 ------> 属性填充 -----> 初始化 -----> AOP -----> 代理对象 -----> bean

<?xml version="1.0" encoding="UTF8"?>
<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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--指定要扫描的包-->
    <context:component-scan base-package="com.jarvis"/>
    <context:annotation-config/>

    <!--开启注解支持 默认proxy-target-class="false"(JDK) proxy-target-class="true"(cglib)-->
    <aop:aspectj-autoproxy proxy-target-class="false"/>

</beans>
Class(UserService) -----> 推断构造方法 -----> 实例化

利用构造方法反射去实例化

问题:如果存在两个或以上的构造方法会怎么样?

有参 + 无参
@Component
public class UserService {

    @Autowired
    public OrderService orderService;

    public UserService() {
        System.out.println("无参构造");
    }

    public UserService(OrderService orderService) {
        this.orderService = orderService;
        System.out.println("有参构造");
    }
}
public class MyTest {
    @Test
    public void test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = applicationContext.getBean("userService", UserService.class);
    }
}

在这里插入图片描述

有参 + 有参
@Component
public class UserService {
    
    public OrderService orderService;

    public UserService(OrderService orderService) {
        this.orderService = orderService;
        System.out.println("有参构造1");
    }

    public UserService(OrderService orderService, OrderService orderService1) {
        this.orderService = orderService;
        System.out.println("有参构造2");
    }
}

在这里插入图片描述
推断构造方法

加上 @Autowired 可以告诉 Spring 用哪个构造方法

@Component
public class UserService {

    public OrderService orderService;

    @Autowired
    public UserService(OrderService orderService) {
        this.orderService = orderService;
        System.out.println("有参构造1");
    }

    public UserService(OrderService orderService, OrderService orderService1) {
        this.orderService = orderService;
        System.out.println("有参构造2");
    }
}

在这里插入图片描述

属性填充
@Component
public class OrderService {
}
    @Autowired
    public OrderService orderService;
初始化
public class User {
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

// 初始化方法一
@Component
public class UserService implements InitializingBean {

    @Autowired
    public OrderService orderService;

    public User defaultUser;

    public void test(){
        System.out.println("test");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // 初始化(例如:查询数据库,给 defaultUser 赋值)
        defaultUser = new User("胡图图", 1);
    }
}
// 初始化方法二
@Component
public class UserService {

    @Autowired
    public OrderService orderService;

    public User defaultUser;

    public void test(){
        System.out.println("test");
    }

    @PostConstruct
    public void enhance(){
        defaultUser = new User("胡图图", 1);
    }
}
AOP
// 测试 Bean 的生命周期中的 AOP
@Component
public class UserService {

    @Autowired
    public OrderService orderService;

    public void test(){
        System.out.println("test");
    }

}
@Aspect
@Component
public class OneAspect {

    @Before("execution(* com.jarvis.srevice.UserService.test())")
    public void beforeOperation(){
        System.out.println("Before");
    }

}

AOP 代理时的代码,仅便于理解

class UserServiceProxy extends UserService {
	// 这是之前实例化的对象
	private UserService target;
	
	public void test() {
		OneAspect.beforeOperation();
		target.test();
	}
}

测试

public class MyTest {
    @Test
    public void test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = applicationContext.getBean("userService", UserService.class);
        System.out.println(userService.orderService);
        userService.test();
    }
}

测试结果
在这里插入图片描述

userService.orderService 为 null,说明如果在 Bean 的生命周期中 AOP,最终 UserService 为代理对象而非原来实例化的对象,因为注重 UserService 中方法的调用,因此 UserService 中属性的值无关紧要

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值