Spring 学习笔记(二)

1、PropertyPlaceholderConfigurer实例

在实际的项目应用中,常常将数据库的链接配置信息保存在.properties文件中,将数据库访问驱动保存到spring配置文件中,为自动从属性文件中读取保存的数据库配置信息,使用 PropertyPlaceholderConfigurer 类在spring配置文件中设置与数据源配置相对应的属性文件

<bean id="propertyConfigurer"    
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
<!--定义数据配置属性文件为jdbc.properties-->
        <property name="location" value="classpath:jdbc.properties" />  
</bean>
<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--${jdbc.driverClassName} 对应属性文件的属性名称为jdbc.driverClassName-->
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
</bean>
 类似诸如日志文件实例需要从配置文件读取时,也可以采用类似方式 

2、Bean的继承

bean支持继承,甚至可以将父bean定义为抽象类,该抽象bean不允许采用getBean方式实例化,子bean继承父bean时,继承父bean对象的属性,当然也可在子bean中重新定义父bean中指定的属性值,对继承属性进行覆盖。bean的继承相应的spring信息配置如下示意:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="BaseCustomerMalaysia" class="com.yiibai.common.Customer" abstract="true">
		<property name="country" value="Malaysia" />
	</bean>

	<bean id="CustomerBean" parent="BaseCustomerMalaysia">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>
	
</beans>
3、依赖检查

spring可设置对属性的赋值情况进行检查,支持四种级别额属性赋值检查

  • none – 没有依赖检查,这是默认的模式。
  • simple – 如果基本类型(int, long,double…)和集合类型(map, list..)的任何属性都没有设置,UnsatisfiedDependencyException将被抛出。
  • objects – 如果对象类型的任何属性都没有设置,UnsatisfiedDependencyException将被抛出。
  • all – 如果任何类型的任何属性都没有被设置,UnsatisfiedDependencyException将被抛出。

对bean没有明确指定属性检查方式即为:none方式;

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="CustomerBean" class="com.yiibai.common.Customer" 
         dependency-check="simple">

		<property name="person" ref="PersonBean" />
		<property name="action" value="buy" />
	</bean>

	<bean id="PersonBean" class="com.yiibai.common.Person">
		<property name="name" value="yiibai" />
		<property name="address" value="address ABC" />
		<property name="age" value="29" />
	</bean>

</beans>

以上设置bean依赖检查方式为simple;设定至少检查一项属性。也可采用在java代码中设置属性为必须项的方式,进行属性依赖检查,示例代码如下:

public class Customer 
{
	private Person person;
	private int type;
	private String action;
	
	public Person getPerson() {
		return person;
	}
	@Required
	public void setPerson(Person person) {
		this.person = person;
	}
}
简单地套用@Required注解不会强制执行该属性的检查,还需要注册一个RequiredAnnotationBeanPostProcessor以了解在bean配置文件@Required注解。spring配置文件如下示例:

<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-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--在spring中引入context:annotation-config配置(方法一)-->
	<context:annotation-config />
<!--在spring中引入RequiredAnnotationBeanPostProcessor(方法二)-->
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
<bean id="CustomerBean" class="com.yiibai.common.Customer">
  <property name="action" value="buy" />
  <property name="type" value="1" />
</bean>
<bean id="PersonBean" class="com.yiibai.common.Person">
   <property name="name" value="yiibai" />
   <property name="address" value="address ABC" />
   <property name="age" value="29" />
</bean>
</beans>
 当person属性未设置时,触发依赖检查异常 

4、实现InitializingBean和DisposableBean接口

  • 实现 InitializingBean,在 属性设置之后执行afterPropertiesSet()方法。
  • 实现DisposableBean,在 Spring 容器释放该 bean 之后运行 destroy()。即在调用 context.close()之后执行destroy方法
备注:可在配置文件中设置属性初始化以及对象清除方法,替代InitializingBean、DisposableBean接口实现,具体的实现方法是:在bean中分别实现与init-method 和 destroy-method对应的方法,然后在spring配置文件设置init-method 和 destroy-method 属性值,配置文件如下示例:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="customerService" class="com.yiibai.customer.services.CustomerService" 
		init-method="initIt" destroy-method="cleanUp">
   		
		<property name="message" value="i'm property message" />
	</bean>
		







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring是一个开源的Java框架,用于构建企业级应用程序。它提供了一种轻量级的、非侵入式的开发方式,通过依赖注入和面向切面编程等特性,简化了Java应用程序的开发过程。 以下是关于Spring学习的一些笔记: 1. IoC(控制反转):Spring通过IoC容器管理对象的创建和依赖关系的注入。通过配置文件或注解,将对象的创建和依赖关系的维护交给Spring容器来管理,降低了组件之间的耦合度。 2. DI(依赖注入):Spring通过依赖注入将对象之间的依赖关系解耦。通过构造函数、Setter方法或注解,将依赖的对象注入到目标对象中,使得对象之间的关系更加灵活和可维护。 3. AOP(面向切面编程):Spring提供了AOP的支持,可以将与业务逻辑无关的横切关注点(如日志、事务管理等)从业务逻辑中分离出来,提高了代码的可重用性和可维护性。 4. MVC(模型-视图-控制器):Spring提供了一个MVC框架,用于构建Web应用程序。通过DispatcherServlet、Controller、ViewResolver等组件,实现了请求的分发和处理,将业务逻辑和视图展示进行了分离。 5. JDBC和ORM支持:Spring提供了对JDBC和ORM框架(如Hibernate、MyBatis)的集成支持,简化了数据库访问的操作,提高了开发效率。 6. 事务管理:Spring提供了对事务的支持,通过声明式事务管理和编程式事务管理,实现了对数据库事务的控制和管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值