Spring使用util Schema

spring-beans-3.0.xsd是Spring的内核,其它的Schema大多都用于简化某些方面的配置

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 -->
<!-- xmlns:p是导入XML Schema里的p名字空间 -->
<!-- 指定spring配置文件的根元素和Schema 导入p空间和util命名空间的元素 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/util
	http://www.springframework.org/schema/util/spring-util-3.0.xsd">
	
	<!-- 配置chinese实例,其实现类是Chinese -->
	<bean id="chinese" class="tju.chc.app.service.impl.Chinese"
	p:axe-ref="stoneAxe" p:name="孙悟空"/>
	
	<!-- 与上面等效<bean id="chinese" class="tju.chc.app.service.impl.Chinese">
		<property name="axe" ref="stoneAxe"/>
		<property name="name" value="孙悟空"/>
	</bean>
	-->
	<!-- 配置stoneAxe实例,其实现类是StoneAxe -->	
	<bean id="stoneAxe" class="tju.chc.app.service.impl.StoneAxe"/>
	<!-- 配置steelAxe实例,其实现类是SteelAxe -->	
	<bean id="steelAxe" class="tju.chc.app.service.impl.SteelAxe"/>
</beans>

在util Schema提供如下几个元素

1、constant:该标签用于将指定的静态Field暴露成一个Bean实例。使用该标签时可指定如下两个属性:

①id:该属性指定将静态Field定义成名为id的Bean实例

②static-field:该属性指定将哪个类,哪个静态Field暴露出来

2.property-path:该标签用于将指定Bean实例的指定属性暴露成一个Bean实例。使用该标签时可以指定如下两个属性:

①id:该属性指定将属性定义为名为id的Bean实例

②path:该属性指定将哪个Bean实例,哪个属性(支持复合属性)暴露出来

3、list:该标签用于定义一个List Bean,支持使用<value.../>,<ref.../><bean.../>等标签来定义List集合元素。使用该标签支持如下三个属性。

①id:该属性指定定义一个名为id的List Bean实例

②list-class:该实行指定Spring使用哪个List实例类来创建Bean实例

③scope:指定该List Bean实例的作用域

4.set:该标签用于定义一个Set Bean,支持<value.../>,<ref.../><bean.../>等标签来定义Set集合元素。使用该标签支持如下三个属性。

①id:该属性指定定义一个名为id的Set Bean实例

②set-class:该实行指定Spring使用哪个Set实例类来创建Bean实例

③scope:指定该Set Bean实例的作用域

5.map:该标签用于定义一个Map Bean,支持使用<entry.../>来定义Map的key-value对。使用该标签支持如下三个属性

①id:该属性指定定义一个名为id的Map Bean实例

②map-class:该实行指定Spring使用哪个map实例类来创建Bean实例

③scope:指定该map Bean实例的作用域

5.properties:该标签用于加载一份资源文件,并根据加载的资源文件创建一个Properties Bean实例。使用该标签以下属性:

①id:该属性指定定义一个名为id的Properties Bean实例

②location : 指定资文件的位置

③scope:指定该Properties Bean实例的作用域

例子如下

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 -->
<!-- xmlns:p是导入XML Schema里的p名字空间 -->
<!-- 指定spring配置文件的根元素和Schema 导入p空间和util命名空间的元素 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/util
	http://www.springframework.org/schema/util/spring-util-3.0.xsd">
	
	<!-- 配置american实例,其实现类是American -->
	<bean id="american" class="tju.chc.app.service.impl.American"
	p:axe-ref="stoneAxe" p:name="孙悟空" p:age-ref="ame.age" p:schools-ref="ame.schools" 
	p:axes-ref="ame.axes" p:scores-ref="ame.scores"/>
	<!-- 使用util:constant将 指定类的静态Field暴露出来 -->
	<util:constant id="ame.age" static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
	<!-- 使用property-path 将指定Bean的指定属性暴露出来  -->
	<util:property-path id="test" path="american.age"/>
	<!-- 使用properties加载指定的资源 -->
	<!-- <util:properties id="confTest" location="classpath:test_zh_CN.properties"/> -->
	<!-- 使用util:list 定义一个List对象 -->
	<util:list id="ame.schools" list-class="java.util.LinkedList">
		<!-- 每个value ref bean都配置一个List元素 -->
		<value>小学</value>
		<value>中学</value>
		<value>大学</value>
	</util:list>
	<!-- 使用util:set定义一个Set对象 -->	
	<util:set id="ame.axes" set-class="java.util.HashSet">
		<!-- 每个value ref bean都配置一个List元素 -->
		<bean class="tju.chc.app.service.impl.SteelAxe"/>
		<ref local="stoneAxe"/>	
	 </util:set>
	 <!-- 使用util:map定义一个Map对象 -->
	 <util:map id="ame.scores" map-class="java.util.TreeMap">
	 	<entry key="数学" value="87"/>
	 	<entry key="英语" value="87"/>
	 	<entry key="语文" value="800"/>
	 </util:map>
	<!-- 配置stoneAxe实例,其实现类是StoneAxe -->	
	<bean id="stoneAxe" class="tju.chc.app.service.impl.StoneAxe"/>
	<!-- 配置steelAxe实例,其实现类是SteelAxe -->	
	<bean id="steelAxe" class="tju.chc.app.service.impl.SteelAxe"/>
</beans>

American类

/**
 * 
 */
/**
 * @author c
 *
 */
package tju.chc.app.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import tju.chc.app.service.Axe;
import tju.chc.app.service.Person;

public class American implements Person{

	private Axe axe;
	private String name;
	private int age;
	private List schools;
	private Map scores;
	private Set axes;
	//设置注入所需的setter方法
	public void setAxe(Axe axe) {
		this.axe = axe;
	}
	//设置注入所需的setter方法
	public void setName(String name) {
		this.name = name;
	}
	
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public List getSchools() {
		return schools;
	}
	public void setSchools(List schools) {
		this.schools = schools;
	}
	public Map getScores() {
		return scores;
	}
	public void setScores(Map scores) {
		this.scores = scores;
	}
	public Set getAxes() {
		return axes;
	}
	public void setAxes(Set axes) {
		this.axes = axes;
	}
	public void useAxe() {
		//调用axe的chop()方法
		//表明Person对象依赖于axe对象
		System.out.println("我是:"+name+","+axe.chop());
	}
}
测试例子

package qi;

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

import tju.chc.app.service.impl.American;

public class BeanTest_util {
	public static void main(String[] args){
		ApplicationContext cxt = new ClassPathXmlApplicationContext("bean_p_util.xml");
		American ame = cxt.getBean("american", American.class);
		
		ame.useAxe();
		System.out.println(ame.getAge());
		System.out.println(ame.getAxes());
		System.out.println(ame.getSchools());
		System.out.println(ame.getScores());
	}
	
	
}

测试结果

我是:孙悟空,石斧砍柴好慢
8
[tju.chc.app.service.impl.StoneAxe@496396ae, tju.chc.app.service.impl.SteelAxe@e3923e5]
[小学, 中学, 大学]
{数学=87, 英语=87, 语文=800}


除此之外,关于Spring其它常用的简化Schema的

spring-aop-3.0.xsd :用于简化SPring AOP配置的Schema

spring-jee-3.0.xsd  :用于简化SPring的Java EE配置的Schema

spring-jms-3.0.xsd :用于简化spring关于JMS配置的Schema

spring-lang-3.0.xsd :用于简化SPring动态语言配置的Schema

spring-tx-3.0.xsd :用于简化Spring事务配置的Schema

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<?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:util="http://www.springframework.org/schema/util" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 扫描com.xie下的所有spring的注解--> <context:component-scan base-package="com.xie" /> <!-- 把其他的java等的注解也加入到spring容器管理--> <context:annotation-config/> <!-- 配置文件读取--> <util:properties id="dbConfig" location="classpath:Config.properties"/> <!-- 配置数据源--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="#{dbConfig.driver}"/> <property name="url" value="#{dbConfig.url}"/> <property name="username" value="#{dbConfig.username}"/> <property name="password" value="#{dbConfig.password}"/> </bean> <!-- 创建sessionfacto--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="dataSource" ref="dataSource"/> </bean> <!-- mapper由spring接管--> <!-- 扫描出mapper接口--> <mybatis:scan base-package="com.xie.mapper"/> <!-- 使用注解来完成aop--> <aop:aspectj-autoproxy/> <!-- 使用注释来控制事务--> <tx:annotation-driven/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 引入数据源--> <property name="dataSource" ref="dataSource"/> </bean> </beans>
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值