java spring初始化_Spring框架初始化解析

一、Spring能做什么?

Spring的主要目的是使J2EE易用和促进好编程习惯。

倒置控制容器 Spring的设计核心是 org.springframework.beans 包, 为与JavaBeans一起工作而设计。 这个包一般不直接被用户使用, 但作为基础为更多的其他功能服务. 下一个较高层面的抽象是"Bean Factory"。 Spring bean factory 是一个普通的Factory,它使对象能够按名称获取,并且能管理对象之间的关系。 Bean factories 支持两种对象模式: . Singleton:在此模式中,有一个具有特定名称的共享对象实例,它在查找时被获取。这是默认的,而且是最为经常使用的。它对于无状态对象是一种理想的模式。 .Prototype:在此模式中,每次获取将创建一个独立的对象。

二、spring启动加载及实现方式

第一种:通过注解@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

第二种:通过 在xml中定义init-method 和 destory-method方法

第三种:通过bean实现InitializingBean和 DisposableBean接口

第四种:写一个类,实现BeanPostProcessor接口,这个接口有两个方法。

(1):postProcessBeforeInitialization方法,在spring中定义的bean初始化前调用这个方法

(2):postProcessAfterInitialization方法,在spring中定义的bean初始化后调用这个方法

或实现

InstantiationAwareBeanPostProcessor,是BeanPostProcessor的子接口

Spring 容器加载完成后执行

从spring监听器作为入口。

org.springframework.web.context.ContextLoaderListener

找到初始化spring的方法

/**

* Initialize the root web application context.

*/

@Override

public void contextInitialized(ServletContextEvent event) {

initWebApplicationContext(event.getServletContext());

}

进入initWebApplicationContext 方法

if (this.context == null) {

this.context = createWebApplicationContext(servletContext);

}

if (this.context instanceof ConfigurableWebApplicationContext) {

ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;

if (!cwac.isActive()) {

// The context has not yet been refreshed -> provide services such as

// setting the parent context, setting the application context id, etc

if (cwac.getParent() == null) {

// The context instance was injected without an explicit parent ->

// determine parent for root web application context, if any.

ApplicationContext parent = loadParentContext(servletContext);

cwac.setParent(parent);

}

configureAndRefreshWebApplicationContext(cwac, servletContext);

}

}

ApplicationListener

1、编写一个实现ApplicationListener的listener类,

import org.springframework.context.ApplicationListener;

import org.springframework.context.event.ContextRefreshedEvent;

import org.springframework.stereotype.Service;

@Service

public class StartupListenerimplements

ApplicationListener

{

@Override

public void onApplicationEvent(ContextRefreshedEvent event)

{

if(event.getApplicationContext().getParent() == null)//root application context 没有parent,他就是老大.

{

//需要执行的逻辑代码,当spring容器初始化完成后就会执行该方法。

System.out.println("\n\n\n\n\n______________\n\n\n加载了\n\n_________\n\n");

}

//或者下面这种方式

if(event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext"))

{

System.out.println("\n\n\n_________\n\n加载一次的 \n\n ________\n\n\n\n");

}

}

}

2、在配置文件(applicationContext-servlet.xml)中设置Service扫描的包

3、部署启动项目,即可在加载完spring后就打印出“加载”

applicationontext和使用MVC之后的webApplicationontext会两次调用上面的方法,如何区分这个两种容器呢?

但是这个时候,会存在一个问题,在web项目中(springmvc),系统会存在两个容器,一个是rootapplicationcontext,另一个就是我们自己的projectName-servletcontext(作为rootapplicationcontext的子容器)。

这种情况下,就会造成onApplicationEvent方法被执行两次。为了避免上面提到的问题,我们可以只在rootapplicationcontext初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理,修改后代码

如下:

@Override

public void onApplicationEvent(ContextRefreshedEvent event) {

if(event.getApplicationContext().getParent() == null){//root application context 没有parent,他就是老大.

//需要执行的逻辑代码,当spring容器初始化完成后就会执行该方法。

}

}

初始化的顺序是:

Constructor > @PostConstruct > InitializingBean > init-method

总结

以上就是本文关于Spring框架初始化解析的全部内容,希望对大家有所帮助。如有问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值