Spring实战之Bean的后处理器操作示例

网友投稿 189 2022-12-19


Spring实战之Bean的后处理器操作示例

本文实例讲述了Spring实战之Bean的后处理器操作。分享给大家供大家参考,具体如下:

一 配置文件

xmlns="http://springframework.org/schema/beans"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd">

init-method="init" p:axe-ref="steelAxe" p:name="依赖注入的值"/>

xmlns="http://springframework.org/schema/beans"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd">

init-method="init" p:axe-ref="steelAxe" p:name="依赖注入的值"/>

init-method="init" p:axe-ref="steelAxe" p:name="依赖注入的值"/>

二 接口

Axe

package org.crazyit.app.service;

public interface Axe

{

public String chop();

}

Person

package org.crazyit.app.service;

public interface Person

{

public void useAxe();

}

三 Bean

Chinese

package org.crazyit.app.service.impl;

import org.springframework.beans.factory.InitializingBean;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.config.BeanPostProcessor;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.crazyit.app.service.*;

public class Chinese

implements Person,InitializingBean

{

private Axe axe;

private String naJujDiMwme;

public Chinese()

{

System.out.println("Spring实例化主调bean:Chinese实例...");

}

public void setAxe(Axe axe)

{

this.axe = axe;

}

public void setName(String name)

{

System.out.println("Spring执行setName()方法注入依赖关系...");

this.name = name;

}

public void useAxe()

{

System.out.println(name + axe.chop());

}

// 下面是两个生命周期方法

public void init()

{

System.out.println("正在执行初始化方法 init...");

}

public void afterPropertiesSet() throws Exception

{

System.out.println("正在执行初始化方法 afterPropertiesSet...");

}

}

SteelAxe

package org.crazyit.app.service.impl;

import org.crazyit.app.service.*;

public class SteelAxe

implements Axe

{

public SteelAxe()

{

System.out.println("Spring实例化依赖bean:SteelAxe实例...");

}

pJujDiMwublic String chop()

{

return "钢斧砍柴真快";

}

}

四 Bean后处理器

package org.crazyit.app.util;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.config.BeanPostProcessor;

import org.crazyit.app.service.*;

import org.crazyit.app.service.impl.*;

public class MyBeanPostProcessor

implements BeanPostProcessor

{

/**

* 对容器中的Bean实例进行后处理

* @param bean 需要进行后处理的原Bean实例

* @param beanName 需要进行后处理的Bean的配置id

* @return 返回后处理完成后的Bean

*/

public Object postProcessBeforeInitialization

(Object bean , String beanName)

{

System.out.println("Bean后处理器在初始化之前对"

+ beanName + "进行增强处理...");

// 返回的处理后的Bean实例,该实例就是容器中实际使用的Bean

// 该Bean实例甚至可与原Bean截然不同

return bean;

}

public Object postProcessAfterInitialization

(Object bean , String beanName)

{

System.out.println("Bean后处理器在初始化之后对"

+ beanName + "进行增强处理...");

// 如果该Bean是Chinese类的实例

if (bean instanceof Chinese)

{

// 修改其name成员变量

Chinese c = (Chinese)bean;

c.setName("疯狂iOS讲义");

}

return bean;

}

}

五 测试结果

Spring实例化主调bean:Chinese实例...

Spring实例化依赖bean:SteelAxe实例...

Bean后处理器在初始化之前对steelAxe进行增强处理...

Bean后处理器在初始化之后对steelAxe进行增强处理...

Spring执行setName()方法注入依赖关系...

Bean后处理器在初始化之前对chinese进行增强处理...

正在执行初始化方法 afterPropertiesSet...

正在执行初始化方法 init...

Bean后处理器在初始化之后对chinese进行增强处理...

Spring执行setName()方法注入依赖关系...

疯狂iOS讲义钢斧砍柴真快

更多关于java相关内容感兴趣的读者可查看本站专题:《Spring框架入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot使用thymeleaf模板过程解析
下一篇:Spring AOP的五种通知方式代码实例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~