经过测试对于具体的一个Bean,执行的流程应该是:

1.实例化:

  常见的有构造(有参、无参)实例化、静态工厂(方法是静态,通过类名.方法返回获取)、实例工厂(专门有个类负责生产对象,需要在bean中配置类和方法名~非静态)

2.注入

  注入有3种(set注入、构造注入、接口注入),如果有注入,则实例化注入对象,注入对象优先完成以下步骤,再注入,再完成bean类的以下步骤。没有注入直接完成下面步骤。

3.传id至方法

  如果当前bean类有实现BeanNameAware接口,并重写setBeanName()方法,先执行此方法。

4.传BeanFactory工厂至方法

  如果当前bean类实现BeanFactroyAware接口,并重写setBeanFactroy()方法,再执行此方法。

5.传ApplicationContext容器至方法

  如果当前bean类实现AapplicationContextAware接口,并重写setApplicationContext()方法,再执行此方法。

6.BeanPostProcessor处理器进行前后预处理

  另外如果存在bean类实现BeanPostProcessor接口,并重写postProcessBeforeInitialization和postProcessAfterInitialization方法。程序会先执行Before(同左)方法再执行init()方法,最后执行After(同左)方法。注意:每个对象实例化过程都会调用此方法。可以用传递的对象对对象内容进行更改。好东西啊。

7.使用代理管理事物(目标方法前打开事物,目标方法后关闭事物)

  在postProcessAfterInitialization方法的返回中使用代理返回。代码如下:

@Override
public Object postProcessAfterInitialization(final Object bean, String beanName)
throws BeansException {
System.out.println("后"+beanName);
return Proxy.newProxyInstance(MyBeanProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() { @Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("-----开启事物-------");
//执行模板方法
Object obj = method.invoke(bean, args);
System.out.println("--------提交事物--------");
return obj;
}
});
}

8.destroy()方法

  此方法可以在bean中进行声明,也可以通过bean类实现DisposableBean接口,重写destroy方法(),执行销毁bean。

小结:程序运行结果:

实现代码:

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 此类实现BeanPostProcessor接口 -->
<bean class="com.xx.service.MyBeanProcessor"/> <!-- service -->
<bean id="userService" class="com.xx.service.UserServiceImpl" init-method="init" destroy-method="destroy" scope="singleton">
<property name="userDao" ref="userDao"/>
</bean> <!-- dao -->
<bean id="userDao" class="com.xx.dao.UserDaoImpl"/>
</beans>

service层接口:

package com.xx.service;

public interface UserService {
public void run();
}

Service层实现类:

package com.xx.service;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.xx.dao.UserDao;
/**
* userService层实现类
* @author phoebe
*
*/
public class UserServiceImpl implements UserService,BeanNameAware,BeanFactoryAware,ApplicationContextAware,DisposableBean{ private UserDao userDao;
public void setUserDao(UserDao userDao) {
System.out.println("注入dao");
this.userDao = userDao;
}
public UserServiceImpl() {
System.out.println("实例化Service");
}
//目标方法
public void run(){
System.out.println("userService is running");
}
//测试方法
public void testBeanFactoryAware(){
System.out.println("证明bean对象被传送过来了");
} //初始方法
public void init(){
System.out.println("this is init method");
}
//销毁方法
public void destroy(){
System.out.println("this is destroy method");
} @Override
public void setBeanName(String name) {
System.out.println("BeanNameAware:"+name);
} @Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware:"+beanFactory.containsBean("userService"));
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
System.out.println("ApplicationContextAware:"+applicationContext.containsBean("userService"));;
}
}

Dao层接口:

package com.xx.dao;

public interface UserDao {

	//测试方法
public void testDao();
}

Dao层实现类:

package com.xx.dao;

public class UserDaoImpl implements UserDao{

	public UserDaoImpl() {
System.out.println("实例化Dao");
}
@Override
public void testDao()
{
System.out.println("Dao is running");
}
}

处理器进行前后预处理

package com.xx.service;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; /**
* 处理器进行前后预处理
* @author phoebe
*
*/
public class MyBeanProcessor implements BeanPostProcessor { @Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("init前:"+beanName);
return bean;
} @Override
public Object postProcessAfterInitialization(final Object bean, String beanName)
throws BeansException {
System.out.println("init后"+beanName);
return Proxy.newProxyInstance(MyBeanProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() { @Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("-----目标方法前:开启事物-------");
//执行模板方法
Object obj = method.invoke(bean, args);
System.out.println("--------目标方法后:提交事物--------");
return obj;
}
});
} }

测试:

package com.xx.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.xx.service.UserService;
/**
* spring生命周期测试
* @author phoebe
*
*/
public class UserServiceTest { private ApplicationContext context = null; @Before
public void before(){
String beanPath="classpath:applicationContext.xml";
context = new ClassPathXmlApplicationContext(beanPath);
}
@Test
public void TestApp() throws Exception { UserService userService = context.getBean("userService",UserService.class);
userService.run();
context.getClass().getMethod("close").invoke(context); } }

spring中Bean对象的生命周期的更多相关文章

  1. Spring IOC -bean对象的生命周期详解

    生命周期执行的过程如下:1) spring对bean进行实例化,默认bean是单例2) spring对bean进行依赖注入3) 如果bean实现了BeanNameAware接口,spring将bean ...

  2. Spring中Bean实例的生命周期及其行为

  3. Spring 了解Bean的一生(生命周期)

    转载 https://blog.csdn.net/w_linux/article/details/80086950 该篇博客就来了解IoC容器下Bean的一生吧,也可以理解为bean的生命周期. ## ...

  4. Hibernate中Java对象的生命周期

    一个对象的出生源于我们的一个new操作,当我们使用new语句创建一个对象,这个对象的生命周期就开始了,当我们不在有任何引用变量引用它,这个对象就的生命就此结束,它占用的内存就可以被JVM的垃圾回收器回 ...

  5. Spring 基础知识(二)Spring的bean初始化与生命周期,以及注入

    Spring bean 初始化: 参考博文: https://www.cnblogs.com/luyanliang/p/5567164.html 1. 加载xml 文件. 扫描注解 ,形成bean定义 ...

  6. 【Spring】Bean的LifeCycle(生命周期)

    菜瓜:水稻,上次说Bean的LifeCycle,还没讲完 水稻:啥?说人话? 菜瓜:spring,bean,生命周期 水稻:哦哦,下次直接说人话.说正事,先从BeanFactory.Applicati ...

  7. hibernate中持久化对象的生命周期(三态:自由态,持久态,游离态 之间的转换)

    三态的基本概念: 1,  暂时状态(Transient):也叫自由态,仅仅存在于内存中,而在数据库中没有对应数据.用new创建的对象,它没有持久化,没有处于Session中,处于此状态的对象叫暂时对象 ...

  8. hibernate中持久化对象的生命周期(转载)

    三态的基本概念 1, 临时状态(Transient):也叫自由态,只存在于内存中,而在数据库中没有相应数据.用new创建的对象,它没有持久化,没有处于Session中,处于此状态的对象叫临时对象: 2 ...

  9. Storm中重要对象的生命周期

    Spout方法调用顺势 declareOutputFields()(调用一次) open() (调用一次) activate() (调用一次) nextTuple() (循环调用 ) deactiva ...

随机推荐

  1. [51nod1462]树据结构

    题面: 给一颗以1为根的树. 每个点有两个权值:vi, ti,一开始全部是零. Q次操作: 读入o, u, d o = 1 对u到根上所有点的vi += d  o = 2 对u到根上所有点的ti += ...

  2. Gym100814B Gym100814F Gym100814I(异或) ACM International Collegiate Programming Contest, Egyptian Collegiate Programming Contest (2015) Arab Academy for Science and Technology

    今日份的训练题解,今天写出来的题没有昨天多,可能是因为有些事吧... Gym100814B 这个题就是老师改卷子,忘带标准答案了,但是他改了一部分卷子,并且确定自己改的卷子没出错,他想从改过的卷子里把 ...

  3. 三 : spring-uploadify上传文件

    一 : applicationContext.xml中:必须声明不然获取不到<!-- 上传文件的配置 --> <bean id="multipartResolver&quo ...

  4. 【C#附源码】数据库文档生成工具支持(Excel+Htm)

    数据库文档生成工具是用C#开发的基于NPOI组件的小工具.软件源码大小不到10MB.支持生成Excel 和Html 两种文档形式.了解更多,请访问:http://www.oschina.net/cod ...

  5. mysql 手册关于修改列字符编码的一个bug

    项目因为历史原因使用了 GBK编码,遇到非GBK编码字符时出现乱码问题,情况比较严重,暂时先打算修改 列的字符编码为 utf8mb4. 查看 mysql 手册: 用 GBK 编码转 utf8 进行说明 ...

  6. 炫酷线条动画--svg

    我们经常可以在一些页面看到看起来很酷的线条动画,有些可以用css实现,有些css就无能为力了,今天来研究另一种实现方式,svg 如果对svg是什么还不了解的话,可以先去看看svg的基础教程: 一般对于 ...

  7. 记node前后端代码共用的一次坑

    项目背景 nodejs项目,webpack打包,用axios请求,Promise封装,nunjucks模板引擎: 之前已将nunjucks模板通过webpack打包策略,做成前后端共用: 目前需要将网 ...

  8. 学而精计算机公共基础学习之路TEST1

    算法 一:算法基本概念 算法是个什么概念学了这么久的程序尽然没有听说过,其实算法就是为了解决问题那么怎么准确完整的解决这个问题就是算法.所以我们所写的程序就可以说为对算法的描述,但是程序编制是不能有于 ...

  9. PHP网站常见安全漏洞,及相应防范措施总结

    目前,基于PHP的网站开发已经成为目前网站开发的主流,本文笔者重点从PHP网站攻击与安全防范方面进行探究,旨在减少网站漏洞,希望对大家有所帮助! 一.常见PHP网站安全漏洞 对于PHP的漏洞,目前常见 ...

  10. dedecms_分页技术

    <ul>{dede:list pagesize='30'} <li><a href="[field:arcurl/]">[field:title ...