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 中属性的值无关紧要