01-Spring底层核心原理

Spring核心知识点

1.Bean的生命周期底层原理
2.依赖注入底层原理
3.初始化底层原理
4.推断构造方法底层原理
5.AOP底层原理
6.Spring事务底层原理
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(AppConfig.class);
        UserService userService = context.getBean(UserService.class);
        userService.getUser();


  • 包扫描及允许AOP切面
		@EnableAspectJAutoProxy
		@Configuration
		@ComponentScan("com.turing.springprinciple")
		public class AppConfig {
		
		}
  • AOP切面
	@Aspect
	@Component
	public class TuringAspect {
	
	    @Pointcut("execution(* com.turing.springprinciple.dev.UserService.getUser())")
	    public void pointCutMethod(){
	
	    }
	    @Before("pointCutMethod()")
	    public void before(){
	        System.out.println("切面编程--@Before");
	    }
	
	    @After("pointCutMethod()")
	    public void after(){
	        System.out.println("切面编程--@After");
	    }
	}
	@Component
	public class UserService implements InitializingBean {
	
	    @Autowired
	    OrderService orderService;
	
	    public void getUser() {
	        System.out.println("获取用户信息");
	    }
	
	    @PostConstruct
	    public void initBefore(){
	        System.out.println("初始化前...");
	    }
	
	    @Override
	    public void afterPropertiesSet() throws Exception {
	        System.out.println("初始化...");
	    }
	}

在这里插入图片描述

生命周期:

three
代理对象Bean
AOP对象
two
初始化后
初始化前$PostConstruct
初始化&InitializingBean
one
依赖注入
UserService类
推断构造方法
普通对象Bean
  • @Autowired 依赖注入

    for (Field field : userService.getClass().getFields()) {
        if (field.isAnnotationPresent(Autowired.class)){
            field.set(userService,??);
        }
    }
        
  • @PostConstruct 初始化化前
       
    for (Method method : userService.getClass().getMethods()) {
        if(method.isAnnotationPresent(PostConstruct.class)){
            method.invoke(userService,args);
        }
    }
    
  • InitializingBean 初始化
        
    if(userService instanceof InitializingBean){
        InitializingBean initializingBean = userService;
        initializingBean.afterPropertiesSet();
    }
        
  • AOP 原理

    public class UserServiceProxy extends UserService {

        UserService target;

        @Override
        public void getUser() {
            //切面编程--Before
            target.getUser();
            //切面编程--After
        }
    }
    
  • 事务管理
    public class UserServiceProxy extends UserService {

        UserService target;

        @Override
        public void getUser() {
            /**
             * 1.@Transactional
             * 2.创建一个连接conn,(事务管理器dataSource)
             * 3.conn.autoCommit=false
             */
            target.getUser();
            /**
             * 4.conn.autoCommit=true;
             * 5.conn.rollback()
             */
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值