作者简介:zhz小白
公众号:小白的Java进阶之路
专业技能:
1、Java基础,并精通多线程的开发,熟悉JVM原理
2、熟悉Java基础,并精通多线程的开发,熟悉JVM原理,具备⼀定的线上调优经验
3、熟悉MySQL数据库调优,索引原理等,⽇志原理等,并且有出过⼀篇专栏
4、了解计算机⽹络,对TCP协议,滑动窗⼝原理等有⼀定了解
5、熟悉Spring,Spring MVC,Mybatis,阅读过部分Spring源码
6、熟悉SpringCloud Alibaba体系,阅读过Nacos,Sentinel,Seata,Dubbo,Feign,Gateway核⼼源码与设计,⼆次开发能⼒
7、熟悉消息队列(Kafka,RocketMQ)的原理与设计
8、熟悉分库分表ShardingSphere,具有真实⽣产的数据迁移经验
9、熟悉分布式缓存中间件Redis,对其的核⼼数据结构,部署架构,⾼并发问题解决⽅案有⼀定的积累
10、熟悉常⽤设计模式,并运⽤于实践⼯作中
11、了解ElasticSearch,对其核⼼的原理有⼀定的了解
12、了解K8s,Jekins,GitLab
13、了解VUE,GO
14、⽬前有正在利⽤闲暇时间做互游游戏,开发、运维、运营、推销等
本人著作git项目:https://gitee.com/zhouzhz/star-jersey-platform,有兴趣的可以私聊博主一起编写,或者给颗star
领域:对支付(FMS,FUND,PAY),订单(OMS),出行行业等有相关的开发领域
如果此文还不错的话,还请关注、点赞、收藏三连支持一下博主~
前面的BeanPostProcessor章节中,我们是不是讲了XXXAware接口,是不是很好奇他们各自有什么用呢,并且各自在底层中Spring是怎么实现的呢?下面让我来一一为大家讲解。
这个图上的基本都是Aware接口的常用实现类,下面让我来给大家一个一个的介绍
package com.zhz.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.Objects;
/**
* @description: Spring应用上下文工具类
* @motto: 代码源于生活,高于生活艺术
* @author: zhouhengzhe
* @date: 2022/12/8 18:58
* @since 1.0
**/
@Component
public class SpringApplicationContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringApplicationContextHolder.applicationContext = applicationContext;
}
public static <T> T getBean(Class<T> clazz) {
checkApplicationContext();
return applicationContext.getBean(clazz);
}
private static void checkApplicationContext() {
if (Objects.isNull(applicationContext)) {
throw new RuntimeException("未注入ApplicationContext,请注入");
}
}
}
假设我们有一个BookService类需要用到BookDao类,如下
package com.zhz.service;
import com.zhz.dao.BookDao;
import com.zhz.utils.SpringApplicationContextHolder;
import org.springframework.stereotype.Service;
/**
* @author zhouhengzhe
* @description: todo
* @date 2022/11/4 10:56
* @since v1
*/
@Service
public class BookService {
public void sout(){
BookDao bean = SpringApplicationContextHolder.getBean(BookDao.class);
System.out.println(bean);
}
}
接着我们运行一下测试类
@Test
public void test2(){
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
BookService bean = applicationContext.getBean(BookService.class);
System.out.println(bean);
}
代码示例来源:origin: spring-projects/spring-framework
private void invokeAwareMethods(final String beanName, final Object bean) {
if (bean instanceof Aware) {
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
if (bean instanceof BeanClassLoaderAware) {
ClassLoader bcl = getBeanClassLoader();
if (bcl != null) {
((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
}
}
if (bean instanceof BeanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}
}
}
代码示例来源:origin: org.springframework/spring-beans
private void invokeAwareMethods(final String beanName, final Object bean) {
if (bean instanceof Aware) {
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
if (bean instanceof BeanClassLoaderAware) {
ClassLoader bcl = getBeanClassLoader();
if (bcl != null) {
((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
}
}
if (bean instanceof BeanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}
}
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Invoke {@link BeanClassLoaderAware}, {@link BeanFactoryAware},
* {@link EnvironmentAware}, and {@link ResourceLoaderAware} contracts
* if implemented by the given object.
*/
public static void invokeAwareMethods(Object parserStrategyBean, Environment environment,
ResourceLoader resourceLoader, BeanDefinitionRegistry registry) {
if (parserStrategyBean instanceof Aware) {
if (parserStrategyBean instanceof BeanClassLoaderAware) {
ClassLoader classLoader = (registry instanceof ConfigurableBeanFactory ?
((ConfigurableBeanFactory) registry).getBeanClassLoader() : resourceLoader.getClassLoader());
if (classLoader != null) {
((BeanClassLoaderAware) parserStrategyBean).setBeanClassLoader(classLoader);
}
}
if (parserStrategyBean instanceof BeanFactoryAware && registry instanceof BeanFactory) {
((BeanFactoryAware) parserStrategyBean).setBeanFactory((BeanFactory) registry);
}
if (parserStrategyBean instanceof EnvironmentAware) {
((EnvironmentAware) parserStrategyBean).setEnvironment(environment);
}
if (parserStrategyBean instanceof ResourceLoaderAware) {
((ResourceLoaderAware) parserStrategyBean).setResourceLoader(resourceLoader);
}
}
}
代码示例来源:origin: org.springframework/spring-context
/**
* Invoke {@link BeanClassLoaderAware}, {@link BeanFactoryAware},
* {@link EnvironmentAware}, and {@link ResourceLoaderAware} contracts
* if implemented by the given object.
*/
public static void invokeAwareMethods(Object parserStrategyBean, Environment environment,
ResourceLoader resourceLoader, BeanDefinitionRegistry registry) {
if (parserStrategyBean instanceof Aware) {
if (parserStrategyBean instanceof BeanClassLoaderAware) {
ClassLoader classLoader = (registry instanceof ConfigurableBeanFactory ?
((ConfigurableBeanFactory) registry).getBeanClassLoader() : resourceLoader.getClassLoader());
if (classLoader != null) {
((BeanClassLoaderAware) parserStrategyBean).setBeanClassLoader(classLoader);
}
}
if (parserStrategyBean instanceof BeanFactoryAware && registry instanceof BeanFactory) {
((BeanFactoryAware) parserStrategyBean).setBeanFactory((BeanFactory) registry);
}
if (parserStrategyBean instanceof EnvironmentAware) {
((EnvironmentAware) parserStrategyBean).setEnvironment(environment);
}
if (parserStrategyBean instanceof ResourceLoaderAware) {
((ResourceLoaderAware) parserStrategyBean).setResourceLoader(resourceLoader);
}
}
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void postProcessWhenBeanClassLoaderAwareThenAwareInvoked() {
this.spring.register(Config.class).autowire();
BeanClassLoaderAware toPostProcess = mock(BeanClassLoaderAware.class);
this.objectObjectPostProcessor.postProcess(toPostProcess);
verify(toPostProcess).setBeanClassLoader(isNotNull());
}
代码示例来源:origin: spring-projects/spring-integration
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
if (this.jsonObjectMapper instanceof BeanClassLoaderAware) {
((BeanClassLoaderAware) this.jsonObjectMapper).setBeanClassLoader(classLoader);
}
}
代码示例来源:origin: spring-projects/spring-framework
((BeanClassLoaderAware) pfb.getHttpInvokerRequestExecutor()).setBeanClassLoader(getClass().getClassLoader());
代码示例来源:origin: camunda/camunda-bpm-platform
private void invokeAwareMethods(final String beanName, final Object bean) {
if (bean instanceof Aware) {
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
if (bean instanceof BeanClassLoaderAware) {
((BeanClassLoaderAware) bean).setBeanClassLoader(getBeanClassLoader());
}
if (bean instanceof BeanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}
}
}
代码示例来源:origin: spring-projects/spring-integration
private static void invokeAwareMethods(Object parserStrategyBean, Environment environment,
ResourceLoader resourceLoader, BeanDefinitionRegistry registry) {
if (parserStrategyBean instanceof Aware) {
if (parserStrategyBean instanceof BeanClassLoaderAware) {
ClassLoader classLoader = (registry instanceof ConfigurableBeanFactory ?
((ConfigurableBeanFactory) registry).getBeanClassLoader() : resourceLoader.getClassLoader());
if (classLoader != null) {
((BeanClassLoaderAware) parserStrategyBean).setBeanClassLoader(classLoader);
}
}
if (parserStrategyBean instanceof BeanFactoryAware && registry instanceof BeanFactory) {
((BeanFactoryAware) parserStrategyBean).setBeanFactory((BeanFactory) registry);
}
if (parserStrategyBean instanceof EnvironmentAware) {
((EnvironmentAware) parserStrategyBean).setEnvironment(environment);
}
if (parserStrategyBean instanceof ResourceLoaderAware) {
((ResourceLoaderAware) parserStrategyBean).setResourceLoader(resourceLoader);
}
}
}
代码示例来源:origin: org.springframework.integration/spring-integration-core
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
if (this.jsonObjectMapper instanceof BeanClassLoaderAware) {
((BeanClassLoaderAware) this.jsonObjectMapper).setBeanClassLoader(classLoader);
}
}
代码示例来源:origin: org.apache.james/james-server-lifecycle-spring
static void setBeanClassLoader(Object importer, ClassLoader cl) {
((BeanClassLoaderAware) importer).setBeanClassLoader(cl);
}
代码示例来源:origin: org.eclipse.gemini.blueprint/gemini-blueprint-extensions
static void setBeanClassLoader(Object importer, ClassLoader cl) {
((BeanClassLoaderAware) importer).setBeanClassLoader(cl);
}
代码示例来源:origin: apache/servicemix-bundles
private void forwardBeanClassLoader(BeanClassLoaderAware target) {
if (beanClassLoader != null) {
target.setBeanClassLoader(beanClassLoader);
}
}
代码示例来源:origin: org.springframework.osgi/spring-osgi-annotation
static void setBeanClassLoader(Object importer, ClassLoader cl) {
((BeanClassLoaderAware) importer).setBeanClassLoader(cl);
}
代码示例来源:origin: spring-projects/spring-integration
((BeanClassLoaderAware) bean).setBeanClassLoader(this.beanFactory.getBeanClassLoader()); // NOSONAR
代码示例来源:origin: apache/servicemix-bundles
private void invokeAwareMethods(final String beanName, final Object bean) {
if (bean instanceof Aware) {
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
if (bean instanceof BeanClassLoaderAware) {
((BeanClassLoaderAware) bean).setBeanClassLoader(getBeanClassLoader());
}
if (bean instanceof BeanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}
}
}
代码示例来源:origin: org.springframework.osgi/org.springframework.osgi.extender
public void postProcessBeanFactory(BundleContext bundleContext, ConfigurableListableBeanFactory beanFactory)
throws BeansException, OsgiException {
Bundle bundle = bundleContext.getBundle();
try {
// Try and load the annotation code using the bundle classloader
Class annotationBppClass = bundle.loadClass(ANNOTATION_BPP_CLASS);
// instantiate the class
final BeanPostProcessor annotationBeanPostProcessor = (BeanPostProcessor) BeanUtils.instantiateClass(annotationBppClass);
// everything went okay so configure the BPP and add it to the BF
((BeanFactoryAware) annotationBeanPostProcessor).setBeanFactory(beanFactory);
((BeanClassLoaderAware) annotationBeanPostProcessor).setBeanClassLoader(beanFactory.getBeanClassLoader());
((BundleContextAware) annotationBeanPostProcessor).setBundleContext(bundleContext);
beanFactory.addBeanPostProcessor(annotationBeanPostProcessor);
}
catch (ClassNotFoundException exception) {
log.info("Spring-DM annotation package could not be loaded from bundle ["
+ OsgiStringUtils.nullSafeNameAndSymName(bundle) + "]; annotation processing disabled...");
if (log.isDebugEnabled())
log.debug("Cannot load annotation injection processor", exception);
}
}
}
代码示例来源:origin: org.eclipse.gemini.blueprint/gemini-blueprint-extender
public void postProcessBeanFactory(BundleContext bundleContext, ConfigurableListableBeanFactory beanFactory)
throws BeansException, OsgiException {
Bundle bundle = bundleContext.getBundle();
try {
// Try and load the annotation code using the bundle classloader
Class<?> annotationBppClass = bundle.loadClass(ANNOTATION_BPP_CLASS);
// instantiate the class
final BeanPostProcessor annotationBeanPostProcessor = (BeanPostProcessor) BeanUtils.instantiateClass(annotationBppClass);
// everything went okay so configure the BPP and add it to the BF
((BeanFactoryAware) annotationBeanPostProcessor).setBeanFactory(beanFactory);
((BeanClassLoaderAware) annotationBeanPostProcessor).setBeanClassLoader(beanFactory.getBeanClassLoader());
((BundleContextAware) annotationBeanPostProcessor).setBundleContext(bundleContext);
beanFactory.addBeanPostProcessor(annotationBeanPostProcessor);
}
catch (ClassNotFoundException exception) {
log.info("Spring-DM annotation package could not be loaded from bundle ["
+ OsgiStringUtils.nullSafeNameAndSymName(bundle) + "]; annotation processing disabled...");
if (log.isDebugEnabled())
log.debug("Cannot load annotation injection processor", exception);
}
}
}
代码示例来源:origin: org.springframework.integration/spring-integration-core
private static void invokeAwareMethods(Object parserStrategyBean, Environment environment,
ResourceLoader resourceLoader, BeanDefinitionRegistry registry) {
if (parserStrategyBean instanceof Aware) {
if (parserStrategyBean instanceof BeanClassLoaderAware) {
ClassLoader classLoader = (registry instanceof ConfigurableBeanFactory ?
((ConfigurableBeanFactory) registry).getBeanClassLoader() : resourceLoader.getClassLoader());
if (classLoader != null) {
((BeanClassLoaderAware) parserStrategyBean).setBeanClassLoader(classLoader);
}
}
if (parserStrategyBean instanceof BeanFactoryAware && registry instanceof BeanFactory) {
((BeanFactoryAware) parserStrategyBean).setBeanFactory((BeanFactory) registry);
}
if (parserStrategyBean instanceof EnvironmentAware) {
((EnvironmentAware) parserStrategyBean).setEnvironment(environment);
}
if (parserStrategyBean instanceof ResourceLoaderAware) {
((ResourceLoaderAware) parserStrategyBean).setResourceLoader(resourceLoader);
}
}
}
代码示例来源:origin: pl.edu.icm.synat/synat-platform-connector
@Override
protected <T> T createService(ConnectionDescriptor connectionDescriptor, Class<T> serviceInterface, String serviceId, boolean stateful) {
final HttpInvokerProxyFactoryBean proxyFactoryBean = new HttpInvokerProxyFactoryBean();
proxyFactoryBean.setServiceInterface(serviceInterface);
proxyFactoryBean.setServiceUrl(connectionDescriptor.getLocation().toString());
final HttpInvokerRequestExecutor executor;
final HttpClient httpClient = createHttpClient();
if (stateful) {
executor = new SessionInvokerRequestExecutor(httpClient, serviceSecurityContext, requestConfig);
} else {
executor = new DefaultHttpInvokerRequestExecutor(httpClient, serviceSecurityContext, requestConfig);
}
if (executor instanceof BeanClassLoaderAware) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
((BeanClassLoaderAware) executor).setBeanClassLoader(classLoader);
}
proxyFactoryBean.setHttpInvokerRequestExecutor(executor);
proxyFactoryBean.afterPropertiesSet();
@SuppressWarnings("unchecked")
final T service = clientInterceptorUtil.addHttpInterceptors((T) proxyFactoryBean.getObject(), connectionDescriptor);
return service;
}
package com.zhz.bean;
import lombok.*;
/**
* @author zhouhengzhe
* @date 2022/12/13
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
public class User {
private Integer id;
private String name;
private String phoneNum;
private String email;
}
@RestController
@RequestMapping("/user")
public class UserRegisterController {
@Autowired
private UserRegisterService userRegisterService;
@RequestMapping("/register")
public String register(User user) {
//进行注册
userRegisterService.register(user);
return "[controller]注册用户成功!";
}
}
@Service
public class UserRegisterService implements ApplicationEventPublisherAware {
private ApplicationEventPublisher applicationEventPublisher;
public boolean register(User user) {
//用户注册
System.out.println("[service]用户[" + user + "]注册成功!");
//消息发布
applicationEventPublisher.publishEvent(new UserRegisterEvent(this, user));
return true;
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
}
@Component
public class EventListener implements ApplicationListener<UserRegisterEvent> {
@Override
public void onApplicationEvent(UserRegisterEvent event) {
//发邮件
System.out.println("正在发送邮件至: " + event.getUser().getEmail());
//发短信
System.out.println("正在发短信到: " + event.getUser().getPhoneNum());
}
}
可以使用注解
@EventListener
使用在类上表示是监听方法(未验证)
//监听事件
@EventListener
public void listenEvent(UserRegisterEvent event) {
//发邮件
System.out.println("正在发送邮件至: " + event.getUser().getEmail());
//发短信
System.out.println("正在发短信到: " + event.getUser().getPhoneNum());
}
@Getter
public class UserRegisterEvent extends ApplicationEvent {
private static final long serialVersionUID = -5481658020206295565L;
private User user;
//谁发布的这个事件,souce就是谁(对象)
public UserRegisterEvent(Object source, User user) {
super(source);
this.user = user;
}
}
修改监听事务的范围:@TransactionalEventListener(phase = TransactionPhase.AFTER_COMPLETION)
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMPLETION)
public void listenEvent1(UserRegisterEvent event) {
divide(event);
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void divide(UserRegisterEvent event) {
//发邮件
System.out.println("正在发送邮件至: " + event.getUser().getEmail());
//发短信
System.out.println("正在发短信到: " + event.getUser().getPhoneNum());
}
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
// Initialize conversion service for this context.
if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
beanFactory.setConversionService(
beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
}
// Register a default embedded value resolver if no BeanFactoryPostProcessor
// (such as a PropertySourcesPlaceholderConfigurer bean) registered any before:
// at this point, primarily for resolution in annotation attribute values.
if (!beanFactory.hasEmbeddedValueResolver()) {
beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
}
// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {
getBean(weaverAwareName);
}
// Stop using the temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(null);
// Allow for caching all bean definition metadata, not expecting further changes.
beanFactory.freezeConfiguration();
// Instantiate all remaining (non-lazy-init) singletons.
//初始化所有的(非懒加载的)单实例bean
beanFactory.preInstantiateSingletons();
}
我們在搞新的配置中心Nacos的時候,为了获取新的配置中心的配置文件中配置的 dataId,以及配置 serverAddr、nacosGroup 等信息,所以才研究 EnvironmentAware 接口的如果写死话那岂不是太不像话了,那就太多的魔法值了,所以我们可以通过 environmentAware 这个接口进行从配置文件中【application.properties】获取配置的配置中心的信息详情。
nacos.config.server-addr=IP地址
nacos.config.data-id=api.properties
nacos.config.group=DEFAULT_GROUP
nacos.config.namespace=public
nacos.config.username=nacos
nacos.config.password=nacos
nacos.config.auto-refresh=true
nacos.config.type=properties
nacos.config.bootstrap.enable=true
nacos.config.bootstrap.log-enable=true
/**
其实说白了就是哪个接口需要获取配置,那么那个接口就需要进行实现该接口并实现里面的set方法
*/
public interface EnvironmentAware extends Aware {
void setEnvironment(Environment environment);
}
@RestController
@RequestMapping("/nacos")
public class NacosController implements EnvironmentAware {
private final static String NACOS_DATA_ID = "nacos.config.data-id";
private final static String NACOS_GROUP = "nacos.config.group";
private static String dataId = "";
private static String group = "";
@NacosInjected
private ConfigService configService;
@Autowired
private Environment environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
// 通过 environment 获取配置信息
@PostConstruct
private void init() {
dataId = environment.getProperty(NACOS_DATA_ID);
group = environment.getProperty(NACOS_GROUP);
}
/**
* 发布配置
* @return
*/
@RequestMapping(value = "/publishConfig",method = RequestMethod.GET)
public boolean publishConfig() {
boolean res = false;
try {
res = configService.publishConfig(dataId, group, "发布配置");
} catch (NacosException e) {
e.printStackTrace();
}
System.out.println(res);
return res;
}
}
使用场景,比如一个service的实现类中,有两个接口的实现类,但是A需要调用B,涉及到事务,可能就会失效,就可以采用自己类调方法,就可以用这个了。
@Component
public class Tiger implements BeanNameAware {
@Override
public void setBeanName(String name) {
System.err.println("Bean的名字:" + name);
}
}
@Component
public class Tiger implements EmbeddedValueResolverAware {
@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
String val = resolver.resolveStringValue("当前操作系统为:${os.name},表达式(3*4)的结果:#{3*4}");
System.err.println("解析后的字符串=》" + val);
}
}
Resource banner = resourceLoader.getResource("file:c:/temp/filesystemdata.txt");
Resource banner = ctx.getResource("file:/user/local/filesystemdata.txt");
package com.zhz.aware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* @author zhouhengzhe
* @date 2022/12/16
*/
@Component
public class CustomResourceLoaderAwareTest implements ResourceLoaderAware {
@Autowired
private ResourceLoader resourceLoader;
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
try {
showResourceData();
} catch (IOException e) {
e.printStackTrace();
}
}
public void showResourceData() throws IOException
{
//This line will be changed for all versions of other examples
Resource banner = resourceLoader.getResource("classpath:spring-bean.xml");
InputStream in = banner.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
System.out.println(line);
}
reader.close();
}
}
具体核心方法:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean(java.lang.String, java.lang.Object, org.springframework.beans.factory.support.RootBeanDefinition)
protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
/**
* 主要是加载三大Aware:BeanNameAware->BeanClassLoaderAware->BeanFactoryAware
* 执行顺序:
* BeanNameAware
* BeanClassLoaderAware
* BeanFactoryAware
*/
invokeAwareMethods(beanName, bean);
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
/**
Bean的后置处理器前
* BeanPostProcessor处理器:
* ApplicationContextAwareProcessor
* BeanValidationPostProcessor
* InitDestroyAnnotationBeanPostProcessor
* AutowiredAnnotationBeanPostProcessor
* 处理对应的Aware接口:
* EnvironmentAware
* EmbeddedValueResolverAware
* ResourceLoaderAware
* ApplicationEventPublisherAware
* MessageSourceAware
* ApplicationStartupAware
* ApplicationContextAware
*/
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
//初始化接口
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null), beanName, ex.getMessage(), ex);
}
if (mbd == null || !mbd.isSynthetic()) {
//Bean的后置处理器后
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}