Spring笔记--bean的生命周期与引用外部资源

bean的生命周期

  1. Spring IOC容器可以管理bean的生命周期,Spring允许在bean生命周期内特定的时间点执行指定的任务。
  2. Spring IOC容器对bean的生命周期进行管理的过程:
    ① 通过构造器或工厂方法创建bean实例
    ② 为bean的属性设置值和对其他bean的引用
    ③ 调用bean的初始化方法
    ④ bean可以使用了
    ⑤ 当容器关闭时,调用bean的销毁方法
  3. 在配置bean时,通过init-method和destroy-method 属性为bean指定初始化和销毁方法
  4. bean的后置处理器
    ① bean后置处理器允许在调用初始化方法前后对bean进行额外的处理
    ② bean后置处理器对IOC容器里的所有bean实例逐一处理,而非单一实例。
    其典型应用是:检查bean属性的正确性或根据特定的标准更改bean的属性。
    ③ bean后置处理器需要实现接口:
    org.springframework.beans.factory.config.BeanPostProcessor。在初始化方法被调用前后,Spring将把每个bean实例分别传递给上述接口的以下两个方法:
    ●postProcessBeforeInitialization(Object, String)
    ●postProcessAfterInitialization(Object, String)
  5. 添加bean后置处理器后bean的生命周期
    ①通过构造器或工厂方法创建bean实例
    ②为bean的属性设置值和对其他bean的引用
    ③将bean实例传递给bean后置处理器的postProcessBeforeInitialization()方法
    ④调用bean的初始化方法
    ⑤将bean实例传递给bean后置处理器的postProcessAfterInitialization()方法
    ⑥bean可以使用了
    ⑦当容器关闭时调用bean的销毁方法

使用外部的属性文件

  1. 创建properties属性文件
  2. 引入context名称空间
  3. 指定properties属性文件的位置
  4. 从properties属性文件中引入属性值

在这里插入图片描述
AfterHandler.java

package com.spring2;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class AfterHandler implements BeanPostProcessor{

	@Override
	public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
		student st=(student)arg0;
		if(st.getId().equals("1"))
			st.setName("小明");
		else
			st.setName("小红");
		return st;
	}

	@Override
	public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
		return arg0;
	}

}

student.java

package com.spring2;

public class student {
	private Integer id;
	
	private String name;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "student [id=" + id + ", name=" + name + "]";
	}

	public student(Integer id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	public student() {
		super();
	}
	
	
}

com.spring2.test.java

package com.spring2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("life.xml");
		student s1=ac.getBean("s1", student.class);
		System.out.println(s1);
	}
}

com.spring2.db.test.java

package com.spring2.db;

import java.sql.SQLException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.druid.pool.DruidDataSource;

public class test {
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("db.xml");
		DruidDataSource bean=ac.getBean("dataSource",DruidDataSource.class);
		try {
			System.out.println(bean.getConnection());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}	

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sms
jdbc.name=root
jdbc.psaaword=123456

db.xml

<?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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:util="http://www.springframework.org/schema/util"
    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-4.0.xsd 
    					http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    
    <!-- 引用外部文件使用 -->
    
    <!-- 直接使用 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="username" value="user"/>
		<property name="password" value="chenqing"/>
		<property name="Url" value="jdbc:mysql://localhost:3306/sms"/>
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
	</bean>
	
	<!-- 加载资源文件1 -->
	<bean class="org.springframework,beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="db.properties"></property>
	</bean>
	
	<!-- 加载资源文件2 -->
	<context:property-placeholder location="db.properties"/>	
	
	<bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="username" value="${jdbc.name}"/>
		<property name="password" value="${jdbc.password}"/>
		<property name="Url" value="${jdbc.url}"/>
		<property name="driverClassName" value="${jdbc.driver}"/>
	</bean>
</beans>

life.xml

<?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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:util="http://www.springframework.org/schema/util"
    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-4.0.xsd">
    
    <bean id="s1" class="com.spring2.student">
 		<property name="id" value ="1"></property>
    </bean>
    
    <bean class="com.spring2.AfterHandler"></bean>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值