承接上两篇IOC的介绍,我们继续...
9)ApplicationContextAware 和 BeanNameAware
加载Spring配置文件时,如果Spring配置文件中所定义的Bean类,如果该类实现了ApplicationContextAware接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware接口中的
public void setApplicationContext(ApplicationContext context) throws BeansException
方法,并且自动可获得ApplicationContext 对象。前提必须在Spring配置文件中指定改类。
一个Demo程序如下:
Spring配置文件中配置:
<bean id="springContext" class="com.shine.spring.SpringContextHelper"></bean>
/**
* ApplicationContext的帮助类
* 自动装载ApplicationContext
*/
public class SpringContextHelper implements ApplicationContextAware{
private static ApplicationContext context ;
//注入ApplicationContext
@Override
public void setApplicationContext(ApplicationContext context)throws BeansException {
//在加载Spring时自动获得context
SpringContextHelper.context = context;
System.out.println(SpringContextHelper.context);
}
public static Object getBean(String beanName){
return context.getBean(beanName);
}
}
继承了BeanNameAware的类会在初始化函数之前,普通bean属性加载之后调用setBeanName()方法:
public interface BeanNameAware {
void setBeanName(string name) throws BeansException;
}
类似的Aware接口spring还提供了很多,功能也是个不相同。这里就不一一例举了。以后碰到在介绍吧~~
10)bean定义的“继承”
注意一个属性设置:abstract="true"
<bean id="inheritedTestBean" abstract="true" class="org.springframework.beans.TestBean">
<property name="name" value="parent"/>
<property name="age" value="1"/>
</bean>
<bean id="inheritsWithDifferentClass" class="org.springframework.beans.DerivedTestBean" parent="inheritedTestBean" init-method="initialize">
<property name="name" value="override"/>
<!-- the age property value of 1 will be inherited from parent -->
</bean>
并且:depends on, autowire mode,dependencycheck,singleton,scope,lazyinit.这些属性不具有“继承”性,仍然保持子bean的默认设置。
11)BeanPostProcessor
BeanPostProcessor的作用域是容器级的,它只和所在容器有关。如果你在容器中定义了BeanPostProcessor,它仅仅对此容器中的bean进行作用。它不会对定义在另一个容器中的bean进行任何处理。该接口作用是:如果我们需要在Spring容器完成Bean的实例化,配置和其他的初始化后添加一些自己的逻辑处理,我们就可以定义一个或者多个BeanPostProcessor接口的实现。以下我们看个实例:
<bean class="com.spring.test.di.BeanPostPrcessorImpl"/>
package com.spring.test.di;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class BeanPostPrcessorImpl implements BeanPostProcessor {
// Bean 实例化之前进行的处理
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("对象" + beanName + "开始实例化");
return bean;
}
// Bean 实例化之后进行的处理
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("对象" + beanName + "实例化完成");
return bean;
}
}
测试代码如下:
// 得到ApplicationContext对象
ApplicationContext ctx = new FileSystemXmlApplicationContext("applicationContext.xml");
// 得到Bean
ctx.getBean("logic");
控制台打印结果将会是:
对象logic开始实例化
对象logic实例化完成
12) BeanFactoryPostProcessor
在BeanFactory加载Bean定义档的所有内容,但还没正式产生Bean实例之前,您可以对该BeanFactory进行一些处理,您只要实例化一个继承了org.springframework.beans.factory.config.BeanFactoryPostProcessor接口的类。
package org.springframework.beans.factory.config;
public interface BeanFactoryPostProcessor {
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}
Spring已经定义了几种实例化BeanFactoryPostProcessor接口的类。它们的功能也各不相同。下面是一些实例介绍:
1'PropertyPlaceholderConfigurer
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:com/foo/jdbc.properties"/>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
jdbc.properties里的内容是:
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root
Spring加载相应的bean之前,${jdbc.username}等将会被properties里相应的配置sa等值所取代。
2'PropertyOverrideConfigurer
调用PropertyOverrideConfigurer之后,xml配置文件的格式将可以转换为property的配置方式来实现。
<context:property-override location="classpath:override.properties"/>
//override.properties里的内容是
beanName.property=value //beanName属性值为value
dataSource.driverClassName=com.mysql.jdbc.Driver //一个叫dataSource的bean,它的driverClassName属性值是com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql:mydb
foo.fred.bob.sammy=123 //foo bean的fred属性里的bob属性里的sammy属性的值是123
13) Annotation
当你在你的配置文件中加入以下配置:
<context:annotation-config/>
你就可以在你的代码里使用annotation了,但你需要知道,这个而配置的背后是,你已通过此配置加载了AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor.通过它们来最终实现了annotation的功能。
至于annotation的具体使用,其内容丰富,我们将在以后以专门的篇幅来介绍。
分享到:
相关推荐
Spring5 框架 ---- IOC容器 ---- 代码 Spring5 框架 ---- IOC容器 ---- 代码 Spring5 框架 ---- IOC容器 ---- 代码 Spring5 框架 ---- IOC容器 ---- 代码 Spring5 框架 ---- IOC容器 ---- 代码 Spring5 框架 ---- ...
Spring IOC 技术的必备开发包,有了这些包就可以放心研究Spring IOC 技术了
spring IoC, 使用配置文件方式配置Bean的实例化和依赖注入
Java-Spring-SpringIoC容器-SpringIoC的学习 SpringIoC容器的学习笔记 主要介绍了IoC容器工作原理以及如何配置IoC容器 通过标签和注解配置容器
来自极客学院平台的springIoc文档
spring-IOC的一些笔记心得
spring-**cntext**-4.3.6.RELEASE.jar:spring提供了基础IOC功能上的扩展服务,提供了很多企业级服务的支持,如邮件服务,任务调度,JNDI定位,EJB集成,远程访问,缓存以及各种试图层框架的封装等。 spring-...
Spring-IOC的简单实现,包括注解和注解解析,欢迎大家拍砖~!
Spring-IOC实例
spring-ioc学习 新手可以下过来学习下, spring-ioc简介
其中,有4个是Spring的基础包,对应Spring核心容器的4个模块,是Spring项目...spring-context-5.1.8.RELEASE.jar //提供在基础IoC上的扩展服务 spring-expression-5.1.8.RELEASE.jar //提供对Spring表达式语言的支持
spring 3.2.4 Realease 的所有jar包: spring-context-3.2.4.RELEASE.jar spring-core-3.2.4.RELEASE.jar spring-beans-3.2.4.RELEASE.jar spring-test-3.2.4.RELEASE.jar spring-web-3.2.4.RELEASE.jar spring-aop-...
spring-demo10-注解-IOC.zip
spring-context-support:对IOC的扩展,以及IOC子容器 spring-context-indexer:类管理组件和Classpath扫描 spring-expression:表达式语句 切面编程: spring-aop:面向切面编程,CGLB,JDKProxy spring-aspects:集成...
springmvc-springioc-lib.rar springmvc-springioc-lib.rar
spring的IOC-DI案例。
Hibernate3.2 核心包作用 包 作用 说明 jta.jar 标准的JTA API 必要 ...如果应用只需基本的IoC /DI支持,引入spring-core.jar及spring- beans.jar文件就可以了。 还包含 Eclipse无提示的解决办法!
Spring_IOC-v(上)笔记Spring_IOC-v(上)笔记Spring_IOC-v(上)笔记Spring_IOC-v(上)笔记
spring version: 5.0.0; jdk: 1.8 IOC大致调用顺序(IOC调用的AOP标签解析)
它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层 中必须使用Spring,因为它模块化的很好,允许你根据自己的需要选择使用它的某一个模块;它实现了很优雅的MVC,对不同的数据访问技术提供了...