spring Ioc的bean的学习笔记

来源:互联网 发布:网络中控品牌 编辑:程序博客网 时间:2024/06/04 23:53
<?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;    }}
原创粉丝点击