Spring学习-02day

Spring学习-day02

创建带有生命周期方法的bean

单实例

  • 生命周期:bean的创建到销毁
  • 在bean中定义指定的创建和销毁方法,Spring在创建或者销毁的时候就会被调用
  • 但指定的创建和销毁方法不能含有参数,但可以抛出异常
  • destroy-method:销毁方法 ···· 在容器销毁的时候才会被调用,即容器的close()方法
  • init-method:初始化方法

多实例

  • 生命周期:bean的创建到销毁
  • 在bean中定义指定的创建和销毁方法,Spring在创建或者销毁的时候就会被调用
  • 但指定的创建和销毁方法不能含有参数,但可以抛出异常
  • destroy-method:销毁方法 ···· 在多实例中始终不会被调用destroy()
  • init-method:初始化方法 ···· 在创建容器的时候就会被调用init()
    <bean id="book" class="com.study.bean.Book"
    destroy-method="destory"
    init-method="init"></bean>
public class Book {
    private String bookName;
    private String authName;
    private Integer price;

    public void destory(){
        System.out.println("调用了书籍的销毁方法");
    }

    public void init(){
        System.out.println("调用了书籍的加载方法");
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthName() {
        return authName;
    }

    public void setAuthName(String authName) {
        this.authName = authName;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }
}


测试bean的后置处理器(BeanPostProcessor接口作用于bean的init()方法前后

  • 无论bean是否有初始化方法,后置处理器都会默认其有,且每次都执行

  • 在bean的初始化之前调用BeanPostProcessor接口的postProcessBeforeInitialization(Object bean, String beanName)方法

  • 在bean的初始化后前调用BeanPostProcessor接口的postProcessAfterInitialization(Object bean, String beanName)方法

在xml文件中配置后置处理器

    <bean id="beanPostProcessor" class="com.study.Process.MyBeanPostProcessor"></bean>

java实现BeanPostProcessor接口类

public class MyBeanPostProcessor implements BeanPostProcessor {
    /**
     * bean的初始化方法(init())调用之前调用
     * @param bean 将要初始化的bean
     * @param beanName bean在xml中配置的id
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("调用了bean的初始化方法");
        return bean;
    }

    /**
     * bean的初始化方法(init())调用之后调用
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("初始化方法后调用");
        return bean;
    }
}

Spring管理连接池(数据库连接池c3p0.jar)

user 数据库用户名
password 数据库密码
jdbcUrl 连接地址及数据库名(本地)
driverClass 连接驱动(MySQL)

  1. 将内容直接嵌入代码中
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="user" value="root"></property>
    <property name="password" value="xxxx"></property>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/xxx"></property>
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
</bean>
  1. 加载外部配置文件的方法
  • username是Spring中的关键字不能作为配置文件中的key使用
user = xxx
password = xxx
jdbcUrl = jdbc:mysql://localhost:3306/xxx
driverClass = com.mysql.jdbc.Driver
  • 需要在beans标签中引入入 xmlns:context="http://www.springframework.org/schema/context"
  • 在beans标签的xsi下引入 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  • <context:property-placeholder location="classpath:dbconfig.properties"></context:property-placeholder>
    location=“classpath:dbconfig.properties” 中的classpath:代表类路径

<?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"
       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-4.0.xsd">
    <!-- 引入的外部properties配置文件
            ${}前后不可以有空格
     -->
    <context:property-placeholder location="classpath:dbconfig.properties"></context:property-placeholder>
    <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
        <property name="driverClass" value="${driverClass}"></property>
    </bean>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值