`

外部属性文件,SpEL表达式,bean的生命周期

 
阅读更多

1.导入外部文件

1)编写db.properties配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/blog
jdbc.user=root
jdbc.password=root

 2)编写applicationContext.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:util="http://www.springframework.org/schema/util"
	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/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

	<!-- 加载配置文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	<!-- 初始化c3p0数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"/>	
		<property name="jdbcUrl" value="${jdbc.url}"></property>	
		<property name="user" value="${jdbc.user}"></property>	
		<property name="password" value="${jdbc.password}"></property>	
	</bean>
	
</beans>

 3)编写测试代码,自己处理吧

 

2.SpEl表达式

SpEL使用#{}做为定界符,引用其他对象,属性,方法

支持运算符,正则,直接调用静态方法或属性

 

<?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:util="http://www.springframework.org/schema/util"
	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/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

	<bean id="address" class="com.hous.spring.Address">
		<property name="city" value="#{'suqian'}"></property>
		<property name="street" value="双河大道"></property>
	</bean>
	
	<bean id="car" class="com.hous.spring.Car">
		<property name="brand" value="aodi"></property>
		<property name="price" value="300000"></property>
		<property name="tyerPerimeter" value="#{T(java.lang.Math).PI*20}"></property>
	</bean>
	
	<bean id="person" class="com.hous.spring.Person">
		<property name="age" value="24"></property>
		<property name="car" value="#{car}"></property>
		<property name="info" value="#{car.price>20000 ? '程序员' : '搬砖的'}"></property>
	</bean>
	
</beans>

 

IoC容器中bean的生命周期

1)通过构造器或工厂方法创建bean实例

2)为bean属性设置值或引用其他bean

3)调用bean的初始化方法

4)使用bean

5)当容器关闭时,调用bean的销毁方法

在bean的声明中设置init-method和destroy-method属性

为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:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util"
	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/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

	<bean id="address" class="com.hous.spring.Address" init-method="init" destroy-method="destroy">
		<property name="city" value="#{'suqian'}"></property>
		<property name="street" value="双河大道"></property>
	</bean>
	
</beans>

 

package com.hous.spring;

public class Address {
	private String city;
	private String street;

	/**
	 * @return the city
	 */
	public String getCity() {
		return city;
	}

	/**
	 * @param city
	 *            the city to set
	 */
	public void setCity(String city) {
		this.city = city;
	}

	/**
	 * @return the street
	 */
	public String getStreet() {
		return street;
	}

	/**
	 * @param street
	 *            the street to set
	 */
	public void setStreet(String street) {
		this.street = street;
	}
	
	public void init() {
		System.out.println("初始化bean。。。");
	}
	
	public void destroy() {
		System.out.println("销毁bean。。。");
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Address [city=" + city + ", street=" + street + "]";
	}

}

 

package com.hous.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
	public static void main(String[] args) {
		//1.创建spring的IoC容器
		ClassPathXmlApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		System.out.println(cxt.getBean("address"));
		
		//关闭容器
		cxt.close();
	}
	
}

 Bean的后置处理器

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:util="http://www.springframework.org/schema/util"
	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/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

	<bean id="address" class="com.hous.spring.Address" init-method="init" destroy-method="destroy">
		<property name="city" value="#{'suqian'}"></property>
		<property name="street" value="双河大道"></property>
	</bean>
	
	<!-- 
		实现BeanPostProcessor接口,并提供具体方法
		Object postProcessAfterInitialization(Object bean, String beanName) 在init-method之前执行
		Object postProcessBeforeInitialization(Object bean, String beanName) 在init-method之后执行
		
		bean:bean实例本身
		beanName: IOC容器配置bean的名字
		返回值:是返回给用户的bean 注意: 以上方法可以修改返回的bean,甚至返回新的bean
	 -->
	<bean class="com.hous.processor.MyBeanProcessor"></bean>
	
</beans>

 实现BeanPostProcessor接口

package com.hous.processor;

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

public class MyBeanProcessor implements BeanPostProcessor {

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("postProcessAfterInitialization" + beanName + "," + bean);
		return bean;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("postProcessAfterInitialization" + beanName + "," + bean);
		return bean;
	}

}

 

 

 

 

分享到:
评论

相关推荐

    37 SpEL Bean方法属性引用原理慕课专栏1

    每一层调用都是Spring框架对Bean生命周期管理的一部分,确保了属性引用的正确性和效率。 总结来说,SpEL Bean方法属性引用是Spring中实现Bean间通信和数据共享的关键机制。它简化了代码,提高了可读性,同时提供了...

    day38 13-Spring的Bean的属性的注入:SpEL注入

    同时,它与Spring的其他特性,如AOP(面向切面编程)和Bean的生命周期管理,无缝集成,提供了高度灵活和强大的依赖注入解决方案。 在实际开发中,结合`@ConfigurationProperties`和SpEL,我们可以实现更高级的配置...

    36 SpEL 正则表达式应用示例及背后原理探究慕课专栏1

    这些调用链展示了Spring如何管理应用程序的生命周期,并在合适的时间执行SpEL表达式。 总的来说,SpEL的正则表达式支持使得在运行时动态验证字符串格式变得非常方便,而其背后的实现则涉及到Spring框架的深入理解,...

    尚硅谷佟刚Spring4代码及PPT.rar

    Spring4.0 的所有核心内容:在 Eclipse 中安装 SpringIDE 插件、IOC & DI、在 Spring 中配置 Bean、自动装配、Bean 之间的关系(依赖、继承)、Bean 的作用域、使用外部属性文件、SpEL、管理 Bean 的生命周期、通过...

    day38 16-Spring的Bean的装配:注解的方式

    4. **自定义配置**:对于非Bean属性的复杂配置,可以使用`@Value`注解,注入环境变量、属性文件中的值,甚至SpEL表达式。 ```java @Value("${db.url}") private String dbUrl; ``` 5. **配置Bean作用域**:使用`@...

    spring 中特殊bean用法

    在Spring框架中,Bean是核心概念,它代表了应用程序中的对象,这些对象由Spring容器管理其生命周期和依赖关系。特殊Bean用法主要涉及Spring提供的多种高级特性,包括但不限于工厂方法、 prototype scope、AOP代理、...

    bean学习笔记

    SpEL是Spring的表达式语言,用于在运行时查询和操作Bean的属性。它可以用于配置文件中的属性值设置,也可以在注解中动态计算值。 通过以上知识点的学习,你将能更好地理解和使用Spring框架中的Bean,从而提升你的...

    Spring中与Bean相关的接口

    在Bean的生命周期管理中,`InitializingBean`和`DisposableBean`接口起着关键作用。实现了`InitializingBean`的Bean会在初始化之后自动调用`afterPropertiesSet()`方法,用于执行初始化逻辑。而实现了`...

    spring的学习笔记

    - **5.4 在Bean定义中使用EL**:展示如何在Bean的属性值或初始化方法中使用SpEL表达式。 以上内容涵盖了Spring框架的关键知识点,从IoC和DI的使用,到AOP和资源管理,再到强大的SpEL表达式语言,这些都是理解和掌握...

    spring管理bean应用实例代码

    SpEL是Spring提供的强大表达式语言,用于在运行时查询和操作Bean的属性。 10. **Bean的自动扫描** 使用`@ComponentScan`注解可以自动发现并注册带有特定注解的类作为Bean。 11. ** Profiles** Spring支持配置多...

    spring4示例代码

    spring-2 演示了外部配置文件的引入(connection),spel(spring el)表达式 ,静态工厂方式及实例工厂方式及factorybean方式创建bean, spring的生命周期及BeanPostProcessor的使用,注解方式创建bean 及使用...

    Spring - -setter方式 向bean中注入各种类型的值

    6. **使用`@Value`注解**:除了注入bean,`@Value`还可以用于注入属性值,包括硬编码的值、环境变量、系统属性,甚至SpEL表达式。 7. **属性文件的值注入**:Spring允许从属性文件中读取值并注入bean。这通常与`@...

    springioc的详细讲解

    这允许我们根据需求选择合适的Bean生命周期。 5. **Bean的生命周期管理**:Spring容器控制Bean的创建、初始化、使用和销毁过程。我们可以自定义初始化和销毁方法,或者注册生命周期处理器来扩展Bean的生命周期行为...

    Spring相关测试1全部

    5. **使用外部属性文件**:Spring允许将配置信息存储在外部的properties文件中,这样可以提高代码的可维护性和灵活性。通过`@PropertySource`注解可以加载属性文件,并用`@Value`注入到bean中。 6. **SpEL(Spring ...

    bean_lifecycle.zip

    4. `@Value`:此注解可以用来注入基本类型的值或者SpEL(Spring Expression Language)表达式的结果,例如配置文件中的属性值。 了解并熟练掌握这些注解,对于管理和控制Spring Bean的生命周期以及有效地注入依赖至...

    SpringIoc示例代码

    在配置中,可以使用SpEL表达式来设置Bean的属性值或选择要注入的依赖。测试类可能包含对SpEL的使用和验证。 6. **Profile**:Spring支持基于环境的配置,可以通过`@Profile`注解定义特定环境下生效的Bean。测试可能...

    学习Spring的一点代码01:如何获取bean?

    首先,Spring容器是管理Bean的中心,它负责Bean的生命周期管理,包括创建、初始化、装配和销毁等过程。容器通过读取配置元数据(XML、Java注解或Java配置类)来决定如何创建Bean。Spring提供了多种方式来获取Bean,...

    25个经典的Spring面试问答.docx

    对于Properties对象,可以使用@Value注解配合SpEL表达式来注入。 13. Spring Bean的自动生成原理是什么? Spring通过BeanDefinition和BeanPostProcessor来自动创建Bean。BeanDefinition包含Bean的配置信息,而...

    spring-context源码

    综上所述,`spring-context`模块是Spring框架的精髓,它通过源码实现了bean的生命周期管理、依赖注入、AOP、事件发布等一系列关键功能,构建了一个灵活且可扩展的企业级应用环境。深入理解`spring-context`源码有助...

    Spring 3源码

    `chapter9` 可能会介绍 SpEL 的使用,例如在配置文件中使用 SpEL 表达式进行属性注入。 8. **事件驱动编程**:Spring 允许应用程序组件通过发布和监听事件进行通信。`chapter12` 可能涵盖这一主题,如 `...

Global site tag (gtag.js) - Google Analytics