hadoop28---注解

本文详细介绍了Spring框架中Bean的初始化顺序,包括通过构造函数初始化、实现特定接口的影响及注解使用方法。深入探讨了ApplicationContextAware与InitializingBean接口在Bean初始化过程中的作用。
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="one" class="cn.itcast_04_springannotation.springrunorder.One">
        <constructor-arg name="one" value="one"/>
    </bean>
    <context:component-scan base-package="cn.itcast_04_springannotation.springrunorder"/>
    <bean id="two" class="cn.itcast_04_springannotation.springrunorder.Two">
        <constructor-arg name="two" value="two" />
    </bean>

</beans>
2.Spring(IOC/AOP)注解学习:IOC的反射,AOP是反射和动态代理。
1.4.spring的初始化顺序
在spring的配置文件中配置bean,如下

在One类和Two类中,分别实现一个参数的构造如下,constructor-arg是构造函数,并且传参数:

加载spring配置文件,初始化bean如下

那么。结果如何呢?

结论:spring会按照bean的顺序依次初始化xml中配置的所有bean

1.1.1. 通过ApplicationContextAware加载Spring上下文环境

One中实现ApplicationContextAware接口会出现如何的变换呢?

结果

1.1.1. InitializingBean的作用

One中实现InitializingBean接口呢?

结果:

结论

1、 spring先检查注解注入的bean,并将它们实例化

2、 然后spring初始化bean的顺序是按照xml中配置的顺序依次执行构造

3、 如果某个类实现了ApplicationContextAware接口,会在类初始化完成后调用setApplicationContext()方法进行操作

4、 如果某个类实现了InitializingBean接口,会在类初始化完成后,并在setApplicationContext()方法执行完毕后,调用afterPropertiesSet()方法进行操作

1.1. 注解使用回顾

1、在spring中,用注解来向Spring容器注册Bean。需要在applicationContext.xml中注册<context:component-scan base-package=pagkage1[,pagkage2,,pagkageN]/>

2、如果某个类的头上带有特定的注解@Component/@Repository/@Service/@Controller,就会将这个对象作为Bean注册进Spring容器

3、在使用spring管理的bean时,无需在对调用的对象进行new的过程,只需使用@Autowired将需要的bean注入本类即可

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值