`
Luob.
  • 浏览: 1568659 次
  • 来自: 上海
社区版块
存档分类
最新评论

spring 第7天 Bean,BeanFactory处理器,配置器

阅读更多
spring 两种后处理器
第一种,Bean 后处理器
对容器中bean进行处理,对bean的功能进行额外的增强
package cn.sh.processor;

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

import cn.sh.springmvc.applicationContextAware.MyContent;
import cn.sh.springmvc_java.American;

/**
 * Spring Bean的后置处器
 * @author Bin
 *
 */
public class MyBeanPostProcessor implements BeanPostProcessor {
	
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		
		System.out.println("Bean后处理器在初始化之前对"+beanName+"进行增强处理....");
		//bean 容器中的Bean
		//返回的Bean 可以和原Bean截然不同
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {

		System.out.println("Bean后处理器在初始化之后对"+beanName+"进行增强处理...");
		if(bean instanceof MyContent){
			System.out.println("修改BeanName的名字");
			MyContent a=(MyContent)bean;
			a.setBeanName("Struts2权威指南");
		}
		return bean;
	}

}
  <!--Bean 后置处理器  -->
   <bean id="beanPostProcessor" class="cn.sh.processor.MyBeanPostProcessor"/>

//如果采用ApplicationContext 作为容器,不需要手动注入Bean后处理器
//测试 BeanPostProcessor 
	@Test
	public void test13() {  
		AbstractApplicationContext act=new ClassPathXmlApplicationContext("classpath*:applicationContent.xml");
		MyContent p=act.getBean("ZG",MyContent.class);
		//获取容器
		System.out.println(p.getContext());
		System.out.println(act==p.getContext());

		//获取配置的bean id
		System.out.println("BeanName:"+p.getBeanName());
		//销毁
		act.registerShutdownHook();
	}
//如果采用 BeanFactory 作为spring的容器,需要手动注入bean后处理器
@Test
	public void test18() { 
		ClassPathResource isr=new ClassPathResource("applicationContent.xml");
		XmlBeanFactory beanFactory=new XmlBeanFactory(isr);
		MyBeanPostProcessor proc=beanFactory.getBean("beanPostProcessor", MyBeanPostProcessor.class);
		//如果采用beanfactory, bean后处理器必须显示的注入到 factory中
		beanFactory.addBeanPostProcessor(proc);
		
		MyContent p=beanFactory.getBean("ZG",MyContent.class);
		//获取容器
		System.out.println(p.getContext());
		//获取配置的bean id
		System.out.println("BeanName:"+p.getBeanName());
		
	}



容器后处理器
package cn.sh.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.Ordered;

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor,Ordered {

	@Override
	public void postProcessBeanFactory(
			ConfigurableListableBeanFactory beanFactory) throws BeansException {
		System.out.println("程序对Spring所做的BeanFactory的初始化没有改变..");
		System.out.println("Spring容器是:"+beanFactory);

	}

	@Override
	public int getOrder() {
		System.out.println("采用Ordered 来指定 后处理器的order属性");
		return 0;
	}
	

}

<bean id="beanFactoryProcessor" class="cn.sh.processor.MyBeanFactoryPostProcessor"/>

//如果采用 BeanFactory 作为spring的容器,需要手动注入beanFactory后处理器
	@Test
	public void test19() { 
		ClassPathResource isr=new ClassPathResource("applicationContent.xml");
		XmlBeanFactory beanFactory=new XmlBeanFactory(isr);
		MyBeanFactoryPostProcessor bfprocessor=beanFactory.getBean("beanFactoryProcessor", MyBeanFactoryPostProcessor.class);
		bfprocessor.postProcessBeanFactory(beanFactory);
		
		MyContent p=beanFactory.getBean("ZG",MyContent.class);
		//获取容器
		System.out.println(p.getContext());
		//获取配置的bean id
		System.out.println("BeanName:"+p.getBeanName());
		
	}
	
//如果采用 ApplicationContext作为spring的容器,则不用进行手动注入


spring已经提供了如下几个容器后处理器
PropertyPlaceHolderConfigurer:属性占位符配置器
PropertyOverrideConfigurer:重写占位符配置器
CustomAutowireConfigurer:自定义自动装配的配置器
CustomScopeConfigurer:自定义作用配置器


PropertyPlaceHolderConfigurer 属性占位符配置器

<!--两种配置方式 属性占位符 后处理器  如果导入了context Schema 可以用cotent: -->
   <!--  <context:property-placeholder location="classpath:dbconn.properties"/> -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	<property name="locations">
    		<list>
    			<value>dbconn.properties</value>
    		</list>
    	</property>
    </bean>
   <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    	<property name="driverClass" value="${jdbc.driverClassName}"/>
    	<property name="jdbcUrl" value="${jdbc.url}"/>
    	<property name="user" value="${jdbc.username}"/>
    	<property name="password" value="${jdbc.password}"/>
    	<property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
    	<property name="minPoolSize" value="${jdbc.minPoolSize}"/>
    	<property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>
    	<property name="MaxIdleTime" value="${jdbc.MaxIdleTime}"/>
    </bean>

dbconn.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mytest
jdbc.username=root
jdbc.password=123456
jdbc.maxPoolSize=40
jdbc.minPoolSize=1
jdbc.initialPoolSize=1
jdbc.MaxIdleTime=20


PropertyOverrideConfigurer:重写占位符配置器
  <!-- 采用 重写 占位符配置器  
   	发现:  重写 占位符  可以覆盖 属性占位符  中的值
   	两种配置方式
   -->
   <context:property-override location="classpath:db.properties"/>
   
   <!-- <bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
    	<property name="locations">
    		<list>
    			<value>db.properties</value>
    		</list>
    	</property>
    </bean> -->
    
    
    <!--采用 重写 占位符后 就可以不用 指定 属性了 -->
  <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
   </bean>

db.properties
#beanName.property=value 的命名格式 
datasource.driverClass=com.mysql.jdbc.Driver
datasource.jdbcUrl=jdbc:mysql://localhost:3306/mytest
datasource.user=root
datasource.password=123456
datasource.maxPoolSize=40
datasource.minPoolSize=1
datasource.initialPoolSize=1
datasource.MaxIdleTime=20

分享到:
评论

相关推荐

    Spring的Bean配置

    Spring IOC和DI概述,Bean的配置形式,IOC容器BeanFactory和ApplicationContext概述,依赖注入的方式,属性注入,构造器注入等案例

    Spring中ApplicationContext和beanfactory区别.rar

    Spring中ApplicationContext和beanfactory区别.rar

    Spring的IoC容器之BeanFactory[定义].pdf

    Spring的IoC容器之BeanFactory[定义].pdf

    二、Spring源码分析——BeanFactory

    NULL 博文链接:https://ylxy3058.iteye.com/blog/2223489

    Spring源码学习三:BeanFactory解析1

    BeanFactory是用于访问Spring Bean容器的根接口,是一个单纯的Bean工厂,也就是常说的IOC容器的顶层定义,各种IOC容器是在其基础上为了满

    Spring中BeanFactory解析bean详解

    本篇文章主要介绍了Spring中BeanFactory解析bean详解 ,详细的介绍了使用BeanFactory对bean进行解析的实例,有兴趣的可以了解一下。

    简单模拟Spring的beanFactory

    NULL 博文链接:https://lgd-java2eye.iteye.com/blog/756599

    Spring的BeanFactory的接口源码中文解释

    Spring的BeanFactory的接口的中文解释

    25个经典的Spring面试问答

    如何用基于XML配置的方式配置Spring 如何用基于Java配置的方式配置Spring 怎样用注解的方式配置Spring 请解释Spring Bean的生命周期 Spring Bean的作用域之间有什么区别 什么是Spring inner beans Spring框架中的...

    BeanFactory的实例化

    spring3中实例化BeanFactory的3中方法

    spring之beanfactory

    spring之beanfactory 的一些基本知识与其关系使用

    spring第二天.pdf

    课程目标 1. 搞清楚BeanFactory家族的接口和类的作用 ...8. 可以自主完成阅读Spring框架中Bean实例创建流程的源码 9. 可以自主完成阅读Spring框架中依赖注入流程的源码 10. 可以确定aop流程的源码阅读入口

    spring bean的源码

    Spring 大量引入了Java 的Reflection机制,通过动态调用的方式避免硬编码方式的约束,并在此基础上建立了其核心组件BeanFactory,以此作为其依赖注入机制的实现基础。org.springframework.beans包中包括了这些核心...

    spring 容器.docx

    Bean是Spring管理的基本单位,在基于Spring的Java EE应用中,所有的组件都被当成Bean处理,包括数据源、Hibernate的SessionFactory、事务管理器等。在Spring中,Bean的是一个非常广义的概念,任何的Java对象、Java...

    spring培训-笔记

    BeanFactory管理Bean(组件)的生命周期 15 Bean的定义 16 Bean的之前初始化 19 Bean的准备就绪(Ready)状态 21 Bean的销毁 21 ApplicationContext 21 Spring的AOP框架 21 Spring的数据层访问 22 Spring的...

    Spring的BeanFactory的接口注解

    对BeanFactory的相关所有接口的功能的关键点进行了总结描述,中文版本

    Spring Bean 的生命周期.docx

    Spring的生命周期是指实例化Bean时所经历的一系列阶段,即通过getBean()获取bean对象及设置对象属性时,Spring框架做了哪些事。Bean的生命周期从Spring容器实例化Bean到销毁Bean。 本文分别对 BeanFactory 和 ...

    Spring in Action(第2版)中文版

    第7章保护spring 7.1springsecurity介绍 7.2验证用户身份 7.2.1配置providermanager 7.2.2根据数据库验证身份 7.2.3根据ldap仓库进行身份验证 7.3控制访问 7.3.1访问决策投票 7.3.2决定如何投票 7.3.3处理...

    Spring教程  主要内容:介绍Spring的历史,Spring的概论和它的体系结构,重点阐述它在J2EE中扮演的角色。

    BeanFactory管理Bean(组件)的生命周期 15 Bean的定义 16 Bean的之前初始化 19 Bean的准备就绪(Ready)状态 21 Bean的销毁 21 ApplicationContext 21 Spring的AOP框架 21 Spring的数据层访问 22 Spring的声明式...

    Spring in Action(第二版 中文高清版).part2

    第7章 保护Spring 7.1 Spring Security介绍 7.2 验证用户身份 7.2.1 配置Provider Manager 7.2.2 根据数据库验证身份 7.2.3 根据LDAP仓库进行身份验证 7.3 控制访问 7.3.1 访问决策投票 7.3.2 决定如何投票...

Global site tag (gtag.js) - Google Analytics