Spring笔记——Bean后处理器
来源:互联网 发布:电视阿里云与安卓区别 编辑:程序博客网 时间:2024/05/29 07:49
1.Bean后处理器
Bean后处理器是一种特殊的Bean,这种特殊的Bean并不对外提供服务,它主要负责对容器中的其他Bean执行后处理。
Bean后处理器必须实现BeanPostProcessor接口,BeanPostProcessor包含如下两个方法:
☞ public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException
该方法第一个参数,是系统即将惊醒后处理的Bean实例,第二个参数是该Bean实例的名字
☞ public Object postProcessAfterInitialization(Object bean, String name) throws BeansException
该方法第一个参数,是系统即将惊醒后处理的Bean实例,第二个参数是该Bean实例的名字
实现该接口的Bean后处理器必须实现这两个方法,这两个方法会对容器的Bean进行后处理,会在目标Bean初始化之前、初始化之后分别被回调,这两个方法用于对容器中Bean实例进行增强处理。
2.Bean后处理器的用处
下面是spring提供的两个常用的后处理器:
☞ BeanNameAutoProxyCreator:根据Bean实例的name属性,创建bean实例的代理
☞ DefaultAdvisorAutoProxyCreator:根据提供的Advisor,对容器中所有的Bean实例创建代理
3.容器后处理器
容器后处理器负责处理容器本身,容器后处理器必须实现BeanFactoryPostProcessor接口。实现该接口必须实现如下方法:
☞ postProcessorBeanFactory(ConfigurableListableBeanFactory beanFactory)
Spring 已经提供了如下几个常用的容器后处理器:
☞ PropertyPlaceholderConfigurer:属性占位符配置器,负责读取Properties属性文件里的属性值,并将这些属性值设置成Spring配置文件的元数据
☞ PropertyOverrideConfigurer:重写占位符配置器
☞ CustomAutowireConfigurer:自定义自动装配的配置器
☞ CustomScopeConfigurer:自定义作用域的配置器
4.属性占位符配置器:PropertyPlaceholderConfigurer
bean.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" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- PropertyPlaceholderConfigurer是一个Bean后处理器,它会读取属性文件信息,并将这些信息设置成Spring配置文件的元数据 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>dbconn.properties</value> <!-- 如果有多个属性文件,依次在下面列出来 --> </list> </property> </bean> <!-- 定义数据源bean --> <bean id="dataSource" class="main.java.dao.DataSource"> <!-- 指定连接数据库的驱动 --> <property name="driverClass" value="${jdbc.driverClassName}" /> <!-- 指定连接数据库的url --> <property name="url" value="${jdbc.url}" /> <!-- 指定连接数据库的用户名 --> <property name="username" value="${jdbc.username}" /> <!-- 指定连接数据库的密码 --> <property name="password" value="${jdbc.password}" /> </bean></beans>
dbconn.properties:
jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/javaeejdbc.username=rootjdbc.password=qx.29682
5.PropertyOverrideConfigurer:重写占位符配置器
PropertyOverrideConfigurer的属性文件指定的信息可以直接覆盖Spring配置文件中的元数据,比PropertyPlaceholderConfigurer要强大。
使用PropertyOverrideConfigurer的属性文件,每条属性应保持如下的格式:
beanName.property=value
beanName是属性占位符试图覆盖的Bean名,property是试图覆盖的属性名。
bean.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" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- PropertyOverrideConfigurer是一个Bean后处理器,它会读取属性文件信息,并将这些信息设置覆盖Spring配置文件的元数据 --> <bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer"> <property name="locations"> <list> <value>dbconn.properties</value> <!-- 如果有多个属性文件,依次在下面列出来 --> </list> </property> </bean> <!-- 定义数据源bean,配置该Bean时没有指定任何信息,但properties文件里的信息将会直接覆盖该Bean的属性值 --> <bean id="dataSource" class="main.java.dao.DataSource" /></beans>
dbconn.properties:
dataSource.driverClass=com.mysql.jdbc.DriverdataSource.url=jdbc:mysql://localhost:3306/javaeedataSource.username=rootdataSource.password=qx.29682
main.java.dao.DataSource.java:
main.java.dao.DataSource:package main.java.dao;public class DataSource { private String driverClass; private String url; private String username; private String password; public void setDriverClass(String driverClass) { this.driverClass = driverClass; } public void setUrl(String url) { this.url = url; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public void jdbcMessage() { System.out.println("driverClass:" + driverClass); System.out.println("url:" + url); System.out.println("username:" + username); System.out.println("password:" + password); }}
- Spring笔记——Bean后处理器
- Spring Bean后处理器
- Spring入门学习——创建Bean后处理器
- Spring学习记录—容器后处理器和Bean后处理器
- spring--后处理器笔记
- 七、Spring Bean后处理器以及容器后处理器
- Spring Bean后处理器以及容器后处理器
- Spring的bean后处理器和容器后处理器
- Spring的bean后处理器和容器后处理器
- Spring BeanFactoryPostProcessor Bean后处理器 执行时机
- 【Spring笔记】(三) 后处理器
- Spring--IoC--Bean的装配--Bean后处理器
- Spring的配置依赖 协调不同步的Bean Bean的后处理器 容器的后处理器
- Spring框架——后处理器
- Bean后处理器
- Bean 后处理器
- Spring IoC之高级特性--创建 Bean后处理器
- Spring Bean的自动装配,注入及后处理器
- C++【多态】和【覆盖】
- android 界面布局-各个布局的属性介绍
- 关于VS2015Winform程序连接acess2013数据库的问题
- papa的儿子 matlab plot3 color peaks
- 【程序1】看商品猜价格
- Spring笔记——Bean后处理器
- crsctl stat res -t 和crsctl stat res -t -init 中STATE_DETAILS的解释
- 自定义Label的行间距
- Core Data
- ubuntu 设置共享文件夹
- 统计机器学习笔记(一)---感知机模型
- unity 关于模型的边界框bounds
- Iterator的数据访问方式
- codeforces 498D