Spring : 基于XML Schema 的配置 (二)

[本教程翻译自Spring 官方文档,并有适当增删]

续上一篇:

<util:list/>

以前如果要装配一个集合(List),你要这样写:

<!-- creates a java.util.List instance with values loaded from the supplied sourceList -->
<bean id="emails" class="org.springframework.beans.factory.config.ListFactoryBean">
    <property name="sourceList">
        <list>
            <value>[email protected]</value>
            <value>[email protected]</value>
            <value>[email protected]</value>
            <value>[email protected]</value>
        </list>
    </property>
</bean>

现在你可以:

<!-- creates a java.util.List instance with the supplied values -->
<util:list id="emails">
    <value>[email protected]</value>
    <value>[email protected]</value>
    <value>[email protected]</value>
    <value>[email protected]</value>
</util:list>

你还可以去自定义该List 的实现,如 <util:list id="emails" list-class="java.util.LinkedList">

(设置ID是为了使集合复用)

对map,set的用法类似这里不介绍。

需要说明的是,<list>和<set>都能装配java.util.Collection的任何实现或数组。

引用其他bean,

<list>
   <ref bean="..." />
<list>

list的成员还包括:<value><bean><null/>

对于map,

<map>
    <entry key="GUITAR" value="..." />
    <entry key="CYMBAL" ref-value="..." />
</map>
  • jee模式

jee标签是为了处理和JavaEE相关的配置,如查询JNDI对象和定义EJB的引用。

当然,使用之前你得添加命名空间。

<?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:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"> <!-- bean definitions here -->

</beans>

下面的例子是使用jndi定义的数据源,

之前你要这样写:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/MyDataSource"/>
</bean>
<bean id="userDao" class="com.foo.JdbcUserDao">
    <!-- Spring will do the cast automatically (as usual) -->
    <property name="dataSource" ref="dataSource"/>
</bean>

现在,你可以:

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyDataSource"/>

<bean id="userDao" class="com.foo.JdbcUserDao">
    <!-- Spring will do the cast automatically (as usual) -->
    <property name="dataSource" ref="dataSource"/>
</bean>

下面是个更复杂的配置:

<jee:jndi-lookup id="simple"
        jndi-name="jdbc/MyDataSource"
        cache="true"
        resource-ref="true"
        lookup-on-startup="false"
        expected-type="com.myapp.DefaultFoo"
        proxy-interface="com.myapp.Foo"/>

时间: 2024-08-27 19:41:46

Spring : 基于XML Schema 的配置 (二)的相关文章

Spring : 基于XML Schema的配置(一)

[本教程翻译自Spring官方文档,并有适当增删] (是针对Spring 4.0.6 Release版本的) 基于XML Schema的配置在Spring 2.0开始被引入,并在2.5和3.0版本得到增强和扩展. 转向基于XML Schema的动机是使得Spring XML配置更简单.传统的基于 <bean/>的方法是很好,但它的通用特性带来了很大的配置开销. 从Spring 依赖注入容器的观点来看,一切都是bean.这对Spring 容器是个好消息,因为如果一切都是bean,那么一对象都能以

Spring 框架的概述以及Spring中基于XML的IOC配置

Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器.框架.一站式 优势: 方便解耦:做到编译期不依赖,运行期才依赖 AOP的支持 声明式事务的支持 方便程序的测试 方便整合各种框架 降低JavaEE API的使用难度 Spring源码很厉害 解耦: 耦合包括:类之间的和方法之间的 解决的思路: 在创建对象的时候用反射来创建,而不是new 读取配置文件

Spring基于XML方式的使用

一.IoC配置 IoC的配置是通过Spring的xml文件的bean标签进行的. 1.bean标签介绍 bean标签一般是在xml文件进行配置的,xml文件一般样式如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.or

基于XML的AOP配置

创建spring的配置文件并导入约束 此处要导入aop的约束 <?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:aop="http://

spring在xml文件中配置bean的三种方法

一.最常见,也是缺省,是调用spring的缺省工厂类 spring缺省工厂类:org.springframework.beans.factory.support.DefaultListableBeanFactory使用其静态方法preInstantiateSingletons() 配置文件中最普通最基本的定义一个普通bean<bean id="DvdTypeDAOBean" class="com.machome.dvd.impl.DvdTypeDAO" >

spring基于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:aop="http://www.springframework.org/schema/

Spring中基于Java的容器配置(二)

使用@Configuration注解 @Configuration注解是一个类级别的注解,表明该对象是用来指定Bean的定义的.@Configuration注解的类通过@Bean注解的方法来声明Bean.通过调用注解了@Bean方法的返回的Bean可以用来构建Bean之间的相互依赖关系,可以通过前文来了解其基本概念. 注入inter-bean依赖 当@Bean方法依赖于其他的Bean的时候,可以通过在另一个方法中调用即可. @Configuration public class AppConfi

基于Spring可扩展Schema自定义配置(2)

本章主要实现配置支持,注解扫描等功能.为本次教程的核心 命名空间支持 要实现命名空间支持,需要继承自NamespaceHandlerSupport. package com.codestd.spring.cxf.config.schema; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; import com.codestd.spring.cxf.config.EndpointBeanProcessor;

Spring 基于xml配置方式的事务

参考前面的声明式事务的例子:http://www.cnblogs.com/caoyc/p/5632198.html 我们做了相应的修改.在dao中和service中的各个类中,去掉所有注解标签.然后为为每个字段提供一个setXxx()方法 最后就是配置applicationContext.xml文件了.内容如下: 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http:/