spring Ioc的bean的学习笔记

<?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;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

走到无路可退

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值