Bean的生命周期(初始化)

Bean的生命周期(初始化)

一、初始化前----> 初始化----> 初始化后 实现原理

UserService —>无参的构造方法—> 对象----> 依赖注入----> 初始化前----> 初始化----> 初始化后 ----> 放入单例池Map ----> Bean

Aspect包

package com.baidu.Aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

@Component
@Aspect //标注这个类直接是切面
public class TulingAspect {

    @Before("execution(* com.baidu.Service.UserService.test())")
    public void TulingBefore(){

        System.out.println("TulingBefore");
    }
}

创建Service包,里面创建两个类,OrderService和User,用@Compent 管理Bean。配置到User Service中。
在创建一个User Service

创建AppConfig类,扫描baidu包下的所有的注解

package com.baidu;

import org.springframework.context.annotation.ComponentScan;

@ComponentScan("com.baidu")
public class AppConfig {
}
@Component
public class UserService implements InitializingBean{
    @Autowired
    private OrderService orderService;
    
    public void test(){
        System.out.println("test");
    }

    private User admin;//想要从数据库中查询(User)字段,自动的赋值给admin ,

    
    //对一种方法实现
    public void a(){
        //Mysql---> User对象 ---> this.admin
    }
    //第二种方法实现
    @Override
    public void afterPropertiesSet() throws Exception {
        //Mysql---> User对象 ---> this.admin
    }
}
1、在UserService 创建对象后,初始化前执行@PostConstroct。
/判断userService1 对象谁加了@Autowired
UserService userService1 = new UserService();
for (Field field : userSerivce.getClass().getFields()) {
    if (field.isAnnotationPresent(Autowired.class)){
        field.set(userService1,??);
    }
}

//判读是否加了PostConstruct 注解
for (Method method : userService1.getClass().getDeclaredMethods()) {
    if (method.isAnnotationPresent(PostConstruct.class)){
        method.invoke(userService1,null);
    }
}

2、初始化(InitializingBean) ,两种方法;

在一个类中,默认使用使无参的构造方法。当没有构造方法,有两个构造方法时,加@AutoWired,就用谁。

//    public UserService(){
//        System.out.println(0);
//    }

    public UserService(OrderService orderService) {
        this.orderService = orderService;
        System.out.println(1);
    }

    @Autowired
    public UserService(OrderService orderService,OrderService orderService1) {
        this.orderService = orderService;
        System.out.println(2);
    }


想要进行Aop,给AppConfig添加 @EnableAspectJAutoProxy注解,定义一个切面

User ServiceProxy对象,----> UserService 代理对象---->UserService 代理对象.target = 普通对象---> 完成。

Class UserServiceProxy extents UserService{
UserSercie  target;

Public void test(){
//@Before切面逻辑
//target.test();
}
}

通过userService.test() 的时候 orderService = null ,怎么能使orderService 有值那,通过代理对象.target,获得orderService 的对象

在这里插入图片描述

进行AOP操作:从Spring容器中找到带有切面的组件。再去判断每个切点。UserService会有匹配切点的过程。

小结

依赖注入:@Autowired
初始化前执行:@PostConstroct。
初始化执行:InitializingBean

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring 生命周期Bean 生命周期是密切相关的。在 Spring 容器中,每个 Bean 都有一个完整的生命周期,即从实例化、依赖注入,到销毁的过程,Spring 容器为我们管理了这些过程。Bean 生命周期包括 Bean 的创建、初始化、使用和销毁。具体来说,Spring 生命周期包括以下阶段: 1.实例化 Bean:容器根据配置文件或注解等方式创建 Bean 的实例。 2.设置 Bean 的属性值:容器会将 Bean 的属性值注入到 Bean 中,这是 Bean 生命周期的第二个阶段。Spring 提供了两种常用方式来实现 Bean 的属性注入:构造函数注入和 Setter 方法注入。 3.调用 Bean 的初始化方法:在 Bean 的所有属性被设置之后,Spring 容器会调用 Bean 的初始化方法,这个方法可以是自定义的方法,也可以是 Spring 提供的初始化方法。 4.使用 Bean:在 Bean 初始化完成之后,Spring 容器会将 Bean 注入到需要使用它的地方,比如其他 Bean 或者 Controller 等。 5.销毁 Bean:当 Bean 不再需要时,Spring 容器会调用 Bean 的销毁方法,这个方法可以是自定义的方法,也可以是 Spring 提供的销毁方法Bean 生命周期是 Spring 生命周期的一部分,它描述了一个 Bean 在 Spring 容器中的生命周期Bean 生命周期包括以下阶段: 1.实例化 Bean:容器根据配置文件或注解等方式创建 Bean 的实例。 2.设置 Bean 的属性值:容器会将 Bean 的属性值注入到 Bean 中,这是 Bean 生命周期的第二个阶段。Spring 提供了两种常用方式来实现 Bean 的属性注入:构造函数注入和 Setter 方法注入。 3.调用 Bean 的初始化方法:在 Bean 的所有属性被设置之后,Spring 容器会调用 Bean 的初始化方法,这个方法可以是自定义的方法,也可以是 Spring 提供的初始化方法。 4.使用 Bean:在 Bean 初始化完成之后,Spring 容器会将 Bean 注入到需要使用它的地方,比如其他 Bean 或者 Controller 等。 5.销毁 Bean:当 Bean 不再需要时,Spring 容器会调用 Bean 的销毁方法,这个方法可以是自定义的方法,也可以是 Spring 提供的销毁方法

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值