hadoop28---注解

时间:2023-03-10 04:56:35
hadoop28---注解
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,如下

hadoop28---注解

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

hadoop28---注解

hadoop28---注解

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

hadoop28---注解

那么。结果如何呢?

hadoop28---注解

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

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

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

hadoop28---注解

结果

hadoop28---注解

1.1.1. InitializingBean的作用

在One中实现InitializingBean接口呢?

hadoop28---注解

结果:

hadoop28---注解

结论

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注入本类即可