spring Ioc的bean的学习笔记
2017-08-18 16:44
459 查看
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--外部数据库配置文件--> <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> <!-- data connection setting --> <bean id="dataSoure" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="dataSourceName" value="${database.database}"></property> <property name="user" value="${database.username}"></property> <property name="password" value="${database.password}"></property> <property name="driverClass" value="${database.driverClassName}"></property> <property name="jdbcUrl" value="${database.url}"></property> <!-- 设置数据库连接池的最大连接数 --> <property name="maxPoolSize"> <value>50</value> </property> <!-- 设置数据库连接池的最小连接数 --> <property name="minPoolSize"> <value>5</value> </property> <!-- 设置数据库连接池的初始化连接数 --> <property name="initialPoolSize"> <value>5</value> </property> <!-- 设置数据库连接池的连接最大空闲时间 --> <property name="maxIdleTime"> <value>20</value> </property> <!-- c3p0缓存Statement的数量数 --> <property name="maxStatements"> <value>50</value> </property> <!-- 当连接池里面的连接用完的时候,C3P0一下获取新的连接数 --> <property name="acquireIncrement"> <value>20</value> </property> </bean> <!--配置事务管理器--> <bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" value="dataSource"/> </bean> <!--配置启用事务注解--> <tx:annotation-driven transaction-manager="TransactionManager"/> <!--SPEL表达式--> <bean id="a1" class="com.test1.A1" init-method="init" destroy-method="destroy"> <property name="pi" value="#{T(java.lang.Math).PI}"></property> </bean> <!--配置bean的后置处理器--> <bean class="com.test1.A3"></bean> <bean id="a4" class="com.test1.A4FactoryBean"> <property name="name" value="fa"></property> </bean> </beans>
package com.test1; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.sql.DataSource; import java.sql.SQLException; import static org.junit.Assert.*; public class A1Test { @Test public void test(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("test1.xml"); DataSource dataSource = (DataSource) ctx.getBean("dataSoure"); try { //测试数据库连接 System.out.println(dataSource.getConnection()); } catch (SQLException e) { e.printStackTrace(); } //测试bean的前置方法与后置方法 A1 a1 = ctx.getBean(A1.class); a1.a1(); ClassPathXmlApplicationContext cpa = (ClassPathXmlApplicationContext) ctx; cpa.close(); } }
package com.test1; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; public class A1 { private String pi; public A1() { System.out.println("A1 的构造器"); } public void a1(){ System.out.println("a1:"+pi); } public String getPi() { return pi; } public void setPi(String pi) { this.pi = pi; } //前置方法 public void init(){ System.out.println("a1-init"); } //后置方法 public void destroy(){ System.out.println("ai-destroy"); } }
bean的生命周期,可以捕获到所有bean,可以通过if判断不同的bean来处理,在init方法的前后执行
package com.test1; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class A3 implements BeanPostProcessor { public Object postProcessBeforeInitialization(Object o, String s) throws BeansException { System.out.println("postProcessBeforeInitialization:"+o+"--"+s); return o; } public Object postProcessAfterInitialization(Object o, String s) throws BeansException { System.out.println("postProcessAfterInitialization:"+o+"--"+s); return o; } }
工厂方法:
package com.test1; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class A3 implements BeanPostProcessor { public Object postProcessBeforeInitialization(Object o, String s) throws BeansException { System.out.println("postProcessBeforeInitialization:"+o+"--"+s); return o; } public Object postProcessAfterInitialization(Object o, String s) throws BeansException { System.out.println("postProcessAfterInitialization:"+o+"--"+s); return o; } }
相关文章推荐
- Spring 学习笔记2 - Bean的定义与Ioc的使用
- Spring IOC 学习笔记(二) BeanFactory
- Spring 学习笔记(二)—— IOC 容器(BeanFactory)
- J2EE系列之Spring4学习笔记(六)--IOC详解(bean之间的关系,bean的作用范围)
- [原创]java WEB学习笔记101:Spring学习---Spring Bean配置:IOC容器中bean的声明周期,Bean 后置处理器
- 《Spring 3.x 企业应用开发实战》学习笔记 第三章 IoC容器概述 3.5 Bean的生命周期
- JavaWeb学习笔记-spring-02-ioc-BeanFactory&ApplicationContext
- Spring的IoC学习笔记之BeanFactoryPostProcessor
- 《Spring 3.x 企业应用开发实战》学习笔记 第三章 IoC容器概述 3.5 Bean的生命周期
- [原创]java WEB学习笔记97:Spring学习---Spring 中的 Bean 配置:IOC 和 DI
- Spring学习笔记---2.1-IOC容器解析Bean配置信息
- JavaWeb学习笔记-spring-04-ioc-bean装载
- Spring学习笔记(二) IoC容器与Bean
- spring学习笔记 -- day03 IOC中的bean标签、DI依赖注入
- spring ioc容器的学习笔记4---XmlBeanFactory ioc容器的简单设计原理解析
- Spring学习笔记之 Spring IOC容器(一)
- spring学习笔记九 通过工厂方法配置bean
- Spring学习笔记之通过XML装配Bean的一些细节
- 学习笔记-Spring(一)--IOC
- spring in action 学习笔记(七)注入内部bean