Bean的生命周期(初始化)

导读:本篇文章讲解 Bean的生命周期(初始化),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/71852.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!