Bean种类

普通bean:之前操作的都是普通bean。<bean id="" class="A"> ,spring直接创建A实例,并返回

FactoryBean:是一个特殊的bean,具有工厂生成对象能力,只能生成特定的对象。

bean必须使用 FactoryBean接口,此接口提供方法 getObject() 用于获得特定bean。

<bean id="" class="FB"> 先创建FB实例,使用调用getObject()方法,并返回方法的返回值

FB fb = new FB();

return fb.getObject();

l BeanFactory 和 FactoryBean 对比?

BeanFactory:工厂,用于生成任意bean。

FactoryBean:特殊bean,用于生成另一个特定的bean。例如:ProxyFactoryBean ,此工厂bean用于生产代理。<bean id="" class="....ProxyFactoryBean"> 获得代理对象实例。AOP使用

 作用域

取值:

singleton 单例,默认值。

prototype 多例,每执行一次getBean将获得一个实例。例如:struts整合spring,配置action多例。

配置信息

<bean id="" class=""  scope="">

<bean id="userServiceId" class="com.itheima.d_scope.UserServiceImpl" 

scope="prototype" ></bean>

生命周期

初始化和销毁

目标方法执行前后执行后,将进行初始化或销毁。

<bean id="" class="" init-method="初始化方法名称"  destroy-method="销毁的方法名称">

spring配置

<!--  

init-method 用于配置初始化方法,准备数据等

destroy-method 用于配置销毁方法,清理资源等

-->

<bean id="userServiceId" class="com.itheima.e_lifecycle.UserServiceImpl" 

init-method="myInit" destroy-method="myDestroy" ></bean>

测试

@Test

public void demo02() throws Exception{

//spring 工厂

String xmlPath = "com/itheima/e_lifecycle/beans.xml";

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);

UserService userService = (UserService) applicationContext.getBean("userServiceId");

userService.addUser();

//要求:1.容器必须close,销毁方法执行; 2.必须是单例的

// applicationContext.getClass().getMethod("close").invoke(applicationContext);

// * 此方法接口中没有定义,实现类提供

applicationContext.close();

}

 BeanPostProcessor 后处理Bean

spring 提供一种机制,只要实现此接口BeanPostProcessor,并将实现类提供给spring容器,spring容器将自动执行,在初始化方法前执行before(),在初始化方法后执行after() 。 配置<bean class="">

l Factory hook(勾子) that allows for custom modification of new bean instances, e.g. checking for marker interfaces or wrapping them with proxies.

l spring提供工厂勾子,用于修改实例对象,可以生成代理对象,是AOP底层。

模拟

A a =new A();

a = B.before(a) --> 将a的实例对象传递给后处理bean,可以生成代理对象并返回。

a.init();

a = B.after(a);

a.addUser(); //生成代理对象,目的在目标方法前后执行(例如:开启事务、提交事务)

a.destroy()

1.编写实现类

MyBeanPostProcessor.java
package com.jd.lifecycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy; /**
* @author weihu
* @date 2018/8/12/012 23:15
*/
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
System.out.println("前方法:"+o);
return o;
} @Override
public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
System.out.println("后方法:"+o);
//o目标对象
//生成jdk代理
return Proxy.newProxyInstance(MyBeanPostProcessor.class.getClassLoader(),
o.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("---开启事物-----"); //执行目标方法
Object obj = method.invoke(o, args);
System.out.println("-----提交事物-------");
return obj;
}
});
}
}
UserService.java
package com.jd.lifecycle;

public interface UserService {

    public void addUser();

}
UserServiceImpl.java
package com.jd.lifecycle;

public class UserServiceImpl implements UserService {

    @Override
public void addUser() {
System.out.println("lifecycle add user");
} public void myInit(){
System.out.println("初始化");
} public void myDestroy(){
System.out.println("销毁");
} }

beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--
init-method 用于配置初始化方法,准备数据等
destroy-method 用于配置销毁方法,清理资源等
-->
<bean id="userServiceId" class="com.jd.lifecycle.UserServiceImpl" init-method="myInit"
destroy-method="myDestroy"></bean> <!--将后处理的实现类注册给spring-->
<bean class="com.jd.lifecycle.MyBeanPostProcessor"></bean> </beans>
TestCycle.java
package com.jd.lifecycle;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author weihu
* @date 2018/8/12/012 23:24
*/
public class TestCycle { @Test
public void testProx(){
String xmlPath="com/jd/lifecycle/beans.xml";
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
UserService userService = (UserService) applicationContext.getBean("userServiceId");
userService.addUser(); //要求:1.容器必须close,销毁方法执行; 2.必须是单例的
// applicationContext.getClass().getMethod("close").invoke(applicationContext);
// * 此方法接口中没有定义,实现类提供
applicationContext.close();
}
}

4.BeanPostProcessor 后处理Bean的更多相关文章

  1. 生命周期(初始化、销毁方法、BeanPostProcessor后处理Bean)

    1.初始化和销毁 在目标方法执行前后进行初始化或销毁 (1)在Service方法的实现类里面创建初始化方法和销毁方法: public class StudentServiceImpl implemen ...

  2. Bean 的生命周期 之 后处理Bean

    这里先把Bean 的生命周期总结一下,然后引出后处理Bean 首先,Bean 的生命周期总共有11步: 1.instantiate bean对象实例化 2.populate properties 封装 ...

  3. day38 09-Spring类的完整生命周期及后处理Bean

    可以配置Bean的这个类的初始化和销毁的方法. 如何销毁这个bean?销毁必须得手动地关闭掉容器才行.而且销毁必须是在scope="singleton"下才有效.因为如果你scop ...

  4. 04Spring_bean 后处理器(后处理Bean),BeanPostProcessor ,bean创建时序,动态代理

    这篇文章很重要,讲解的是动态代理,以及bean创建前后的所发生的事情.介绍一个接口:在Spring构造Bean对象过程中,有一个环节对Bean对象进行 后处理操作 (钩子函数) ----- Sprin ...

  5. IoC容器装配Bean(xml配置方式)(Bean的生命周期)

    1.Spring管理Bean,实例化Bean对象 三种方式 第一种:使用类构造器实例化(默认无参数) package cn.itcast.spring.initbean; /** * 使用构造方法 实 ...

  6. spring Bean的完整生命周期

    spring 容器中的bean的完整生命周期一共分为十一步完成. 1.bean对象的实例化 2.封装属性,也就是设置properties中的属性值 3.如果bean实现了BeanNameAware,则 ...

  7. Spring的Bean生命周期理解

    首先,在经历过很多次的面试之后,一直不能很好的叙述关于springbean的生命周期这个概念.今日对于springBean的生命周期进行一个总结. 一.springBean的生命周期: 如下图所示: ...

  8. 装配bean,基于xml

    一.bean的实例化方式 1.默认构造 <bean id="" class=""></bean> 必须提供默认构造方法 2.静态工厂 用 ...

  9. Spring 容器中 Bean 的生命周期

    Spring 容器中 Bean 的生命周期 1. init-method 和 destory-method 方法 Spring 初始化 bean 或销毁 bean 时,有时需要作一些处理工作,因此 s ...

随机推荐

  1. python基础之从认识python到python的使用

    python的历史: python的创始人是吉多·范罗苏姆(Guido van Rossum),人称“龟叔”,1989年圣诞节期间,Guido开始写Python语言的编译器.他希望这个叫做Python ...

  2. http 文件上传协议图览

  3. MySQL的show profile(已过时)简介以及该功能在MySQL 5.7中performance_schema中的替代

    本文出处:http://www.cnblogs.com/wy123/p/6979499.html show profile 命令用于跟踪执行过的sql语句的资源消耗信息,可以帮助查看sql语句的执行情 ...

  4. Linux:sudo,没有找到有效的 sudoers 资源。

    首先,这是因为用户的权限不够导致的. 使用 ls -l /etc/passwd 查看所有用户及权限.只有可读权限(r),说明用户的权限不够. 因此,我们可以用以下方法修改用户权限: 1. su roo ...

  5. ceph结构详解

    引言 那么问题来了,把一份数据存到一群Server中分几步? Ceph的答案是:两步. 计算PG 计算OSD 计算PG 首先,要明确Ceph的一个规定:在Ceph中,一切皆对象. 不论是视频,文本,照 ...

  6. spring @Scheduled 并发

    一.spring定时任务配置 applicationContext.xml:红色代码部分为需要配置的部分. <?xml version="1.0" encoding=&quo ...

  7. 大数据入门到精通7--对复合value做reducebykey

    培训系列7--对复合value做reduce 1.做基础数据准备 val collegesRdd= sc.textFile("/user/hdfs/CollegeNavigator.csv& ...

  8. GitLab代码行数统计--统计增加与删除行数

    #!/bin/bashmaster_dev='master'date_star='2018-11-01'date_end='2018-11-30'path1=`find /home/gitlab_da ...

  9. 9. Palindrome Number (JAVA)

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

  10. String 中intern

    首先贴上源码中的注释 在一个String类上调用这个方法的时候如果常量池中存在和这个String对象相同的对象的时候,直接返回常量池中的常量,如果常量池中不存在这个对象,就直接将其将其加入常量池,并且 ...