web.xml中classpathhXmlApplicationContext 和ApplicationContext

CXF和spring整合实现webservice实例 - Java红 - ITeye技术网站
博客分类:
环境:JDK6.0,cxf-2.0.7,spring2.5,tomcat6.0
服务器端:
1.新建Web项目,例如webws,导入cxf-2.0.7/lib下的jar包和spring2.5的包,因为cxf支持spring,因此它自带有sping的一些核心包,为了以后扩展,保险起见都一起导入吧。。
2.新建一个服务接口IHelloWorld.java,该接口是面向客户端,暴露服务:
.
import javax.jws.WebP
import javax.jws.WebS
@WebService
public interface IHelloWorld {
//@WebParam(name="arg0")可有可无,为了增强可读性
public String sayHello(@WebParam(name="arg0")String text);
}
3.再新建该接口的实现类HelloWorldImpl.java:
.
import javax.jws.WebS
@WebService(endpointInterface="cn.com.service.IHelloWorld")
public class HelloWorldImpl implements IHelloWorld {
public String sayHello(String text) {
return "Hello" +
}
4.现在可以进行spring配置了,在eclipse的src文件夹下新建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:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"&
&import resource="classpath:META-INF/cxf/cxf.xml" /&
&import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /&
&import resource="classpath:META-INF/cxf/cxf-servlet.xml" /&
&bean id="hello" class="cn.com.service.HelloWorldImpl"/&
&jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /&
&/beans&
注意:1).&beans&元素里面的命名空间一定要正确,特别要增加xmlns:jaxws="http://cxf.apache.org/jaxws",http://cxf.apache.org/jaxws,http://cxf.apache.org/schemas/jaxws.xsd;
2).cxf.xml,cxf-extension-soap.xml,cxf-servlet.xml三个文件都在cxf-2.0.7.jar中把它们拷贝到META-INF/目录下。
5.配置web.xml:
&?xml version="1.0" encoding="UTF-8"?&
&web-app version="2.5" xmlns="/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="/xml/ns/javaee
/xml/ns/javaee/web-app_2_5.xsd"&
&context-param&
&param-name&contextConfigLocation&/param-name&
&param-value&classpath:applicationContext.xml&/param-value&
&/context-param&
&listener&
&listener-class&org.springframework.web.context.ContextLoaderListener&/listener-class&
&/listener&
&servlet-name&CXFServlet&/servlet-name&
&display-name&CXF Servlet&/display-name&
&servlet-class&org.apache.cxf.transport.servlet.CXFServlet&/servlet-class&
&load-on-startup&1&/load-on-startup&
&/servlet&
&servlet-mapping&
&servlet-name&CXFServlet&/servlet-name&
&url-pattern&/*&/url-pattern&
&/servlet-mapping&
&welcome-file-list&
&welcome-file&index.jsp&/welcome-file&
&/welcome-file-list&
&/web-app&
到此服务器端基本上告一段落,可以将应用部署到tomcat,启动并访问http://localhost:8080/webws/HelloWorld?wsdl,如果能正确显示xml文件则说明部署成功。
1.新建一个java项目,例如wsclient,也导入cxf-2.0.7和spring包
2.新建客户程序(如果熟悉cxf可以运用cxf的wsdl2java命令直接根据wsdl生成),如Test.java:
package pro.webws.
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-client.xml");
IHelloWorld client = (IHelloWorld) ctx.getBean("client");
String result = client.sayHello("你好!");
System.out.println(result);
}
根据代码,我们知道还需要新建一个IHelloWorld接口,这是服务器端暴露的服务,我们还需要在客户端再新建一个以利用之,代码和服务器端一致即可,不再赘述。
现在可以配置客户端的spring文件了,新建spring-client.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:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schema/jaxws.xsd"&
&bean id="client" class="pro.webws.client.IHelloWorld" factory-bean="clientFactory"
factory-method="create" /&
&bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"&
&property name="serviceClass" value="pro.webws.client.IHelloWorld" /&
&property name="address" value="http://localhost:8080/webws/HelloWorld" /&
&/beans&
注意:&property name="address" value="http://localhost:8080/webws/HelloWorld" /&中的/HelloWorld要与服务器端applicationContext.xml中的&jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /&的address属性对应。
现在服务器和客户端都已完成,启动tomcat,运行客户程序,将输出结果:
Hello你好!
浏览 22304
tsinglongwu
浏览: 95429 次
来自: 深圳
楼主,我用楼主写的实例运行时,在tomcat启动时就报异常了: ...
遇到问题就是 服务器端和客户端接口包名没写成一样的,,改过之后 ...
谢谢啊啊啊
但是如果类名前两个字母都是大写,则返回的bean名也是大写,即 ...
已经解决时项目的大小写问题,弄了半天才解决。。。
刚开始我的项 ...spring注解注入:&context:component-scan&详解 - 为程序员服务
为程序员服务
spring注解注入:&context:component-scan&详解
spring从2.5版本开始支持注解注入,注解注入可以省去很多的xml配置工作。由于注解是写入java代码中的,所以注解注入会失去一定的灵活性,我们要根据需要来选择是否启用注解注入。
我们首先看一个注解注入的实际例子,然后再详细介绍context:component-scan的使用。
如果你已经在用spring mvc的注解配置,那么你一定已经在使用注解注入了,本文不会涉及到spring mvc,我们用一个简单的例子来说明问题。
本例中我们会定义如下类:
PersonService类,给上层提供Person相关操作
PersonDao类,给PersonService类提供DAO方法
Person类,定义Person相关属性,是一个POJO
App类,入口类,调用注解注入的PersonService类
PersonService类实现如下:
package cn.outofmemory.
import org.springframework.beans.factory.annotation.A
import org.springframework.stereotype.S
public class PersonService {
@Autowired
private PersonDao personD
public Person getPerson(int id) {
return personDao.selectPersonById(id);
在Service类上使用了@Service注解修饰,在它的私有字段PersonDao上面有@Autowired注解修饰。@Service告诉spring容器,这是一个Service类,默认情况会自动加载它到spring容器里。而@Autowired注解告诉spring,这个字段是需要自动注入的。
PersonDao类:
package cn.outofmemory.
import org.springframework.context.annotation.S
import org.springframework.stereotype.R
@Scope("singleton")
@Repository
public class PersonDao {
public Person selectPersonById(int id) {
Person p = new Person();
p.setId(id);
p.setName("Person name");
在PersonDao类上面有两个注解,分别为@Scope和@Repository,前者指定此spring bean的scope是单例,你也可以根据需要将此bean指定为prototype,@Repository注解指定此类是一个容器类,是DA层类的实现。这个类我们只是简单的定义了一个selectPersonById方法,该方法的实现也是一个假的实现,只是声明了一个Person的新实例,然后设置了属性,返回他,在实际应用中DA层的类肯定是要从数据库或者其他存储中取数据的。
Person类:
package cn.outofmemory.
public class Person {
public int getId() {
public void setId(int id) {
public String getName() {
public void setName(String name) {
this.name =
Person类是一个POJO。
package cn.outofmemory.
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
* Hello spring!
public class App
public static void main( String[] args )
ApplicationContext appContext = new ClassPathXmlApplicationContext("/spring.xml");
PersonService service = appContext.getBean(PersonService.class);
Person p = service.getPerson(1);
System.out.println(p.getName());
在App类的main方法中,我们初始化了ApplicationContext,然后从中得到我们注解注入的PersonService类,然后调用此对象的getPerson方法,并输出返回结果的name属性。
注解注入也必须在spring的配置文件中做配置,我们看下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: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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"&
&context:component-scan base-package="cn.outofmemory.spring" use-default-filters="false"&
&context:include-filter type="regex" expression="cn\.outofmemory\.spring\.[^.]+(Dao|Service)"/&
&/context:component-scan&
这个配置文件中必须声明xmlns:context 这个xml命名空间,在schemaLocation中需要指定schema:
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
这个文件中beans根节点下只有一个context:component-scan节点,此节点有两个属性base-package属性告诉spring要扫描的包,use-default-filters="false"表示不要使用默认的过滤器,此处的默认过滤器,会扫描包含Service,Component,Repository,Controller注解修饰的类,而此处我们处于示例的目的,故意将use-default-filters属性设置成了false。
context:component-scan节点允许有两个子节点&context:include-filter&和&context:exclude-filter&。filter标签的type和表达式说明如下:
Filter Type
Examples Expression
Description
annotation
org.example.SomeAnnotation
符合SomeAnnoation的target class
assignable
org.example.SomeClass
指定class或interface的全名
org.example..*Service+
AspectJ語法
org\.example\.Default.*
Regelar Expression
org.example.MyTypeFilter
Spring3新增自訂Type,實作org.springframework.core.type.TypeFilter
在我们的示例中,将filter的type设置成了正则表达式,regex,注意在正则里面.表示所有字符,而\.才表示真正的.字符。我们的正则表示以Dao或者Service结束的类。
我们也可以使用annotaion来限定,如下:
&?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"&
&context:component-scan base-package="cn.outofmemory.spring" use-default-filters="false"&
&context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/&
&context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/&
&/context:component-scan&
这里我们指定的include-filter的type是annotation,expression则是注解类的全名。
另外context:conponent-scan节点还有&context:exclude-filter&可以用来指定要排除的类,其用法和include-filter一致。
最后我们要看下输出的结果了,运行App类,输出:
21:14:18 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1cac6db: startup date [Sun May 18 21:14:18 CST 2014]; root of context hierarchy
21:14:18 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring.xml]
21:14:18 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1fcf790: defining beans [personDao,personService,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy
Person name
前几行都是spring输出的一些调试信息,最后一行是我们自己程序的输出。
本文源码下载:
推荐阅读:
相关聚客文章苟有恒,何必三更起五更眠;最无益,只怕一日曝十日寒.
posts - 238, comments - 314, trackbacks - 0, articles - 16
阅读排行榜
一、简单的用ApplicationContext做测试的话,获得Spring中定义的Bean实例(对象).可以用:
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");RegisterDAO registerDAO = (RegisterDAO)ac.getBean("RegisterDAO");
如果是两个以上:ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","dao.xml"});
或者用通配符:ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/*.xml");
二、ClassPathXmlApplicationContext[只能读放在web-info/classes目录下的配置文件]和FileSystemXmlApplicationContext的区别
classpath:前缀是不需要的,默认就是指项目的classpath路径下面;如果要使用绝对路径,需要加上file:前缀表示这是绝对路径;
对于FileSystemXmlApplicationContext:默认表示的是两种:
1.没有盘符的是项目工作路径,即项目的根目录;2.有盘符表示的是文件绝对路径.
如果要使用classpath路径,需要前缀classpath:
public class HelloClient {
& protected static final Log log = LogFactory.getLog(HelloClient.class);
& public static void main(String[] args) {&&& // Resource resource = new ClassPathResource("appcontext.xml");&&& // BeanFactory factory = new XmlBeanFactory(resource);
&&& // 用classpath路径&&& // ApplicationContext factory = new ClassPathXmlApplicationContext("classpath:appcontext.xml");&&& // ApplicationContext factory = new ClassPathXmlApplicationContext("appcontext.xml");
&&& // ClassPathXmlApplicationContext使用了file前缀是可以使用绝对路径的&&& // ApplicationContext factory = new ClassPathXmlApplicationContext("file:F:/workspace/example/src/appcontext.xml");
&&& // 用文件系统的路径,默认指项目的根路径&&& // ApplicationContext factory = new FileSystemXmlApplicationContext("src/appcontext.xml");& & // ApplicationContext factory = new FileSystemXmlApplicationContext("webRoot/WEB-INF/appcontext.xml");
&&& // 使用了classpath:前缀,这样,FileSystemXmlApplicationContext也能够读取classpath下的相对路径&&& // ApplicationContext factory = new FileSystemXmlApplicationContext("classpath:appcontext.xml");&&& // ApplicationContext factory = new FileSystemXmlApplicationContext("file:F:/workspace/example/src/appcontext.xml");
&&& // 不加file前缀&&& ApplicationContext factory = new FileSystemXmlApplicationContext("F:/workspace/example/src/appcontext.xml");&&& IHelloWorld hw = (IHelloWorld)factory.getBean("helloworldbean");&&& (hw.getContent("luoshifei"));& }}
名称: ?4C.ESL | .↗Evon 口号: 遇到新问题?先要寻找一个方案乄而不是创造一个方案こmail:如何用Spring 3.1的Environment和Profile简化工作 - ImportNew
由于项目很紧,我每周写一篇技术博客的目标在12月份恐怕难以完成。我的周末一共只有4天,一个漂亮的家庭工作环境,当然这是为了方便做些其他的事情,而不是为了整天泡在Hack News上。现在已经是中午了,我还有2个小时完成这篇博客。马上开始!
我编写或设计的软件通常需要部署在不同的环境,也需要使用不同的部署配置。这些部署配置大致可以分为以下几类:
Java企业级容器(包括JBoss、WebLogic、Tomcat、Glassfish等)
独立运行的Java应用程序
GUI应用程序
本文暂不对GUI应用程序进行讨论,留到后续再说。对于其他的类型(容器、独立应用程序和测试框架)来说,代码往往是相同的。因此,在设计或者编码这类软件的时候,这一点非常关键。对于每种场景来说,我所写的代码需要完美运行在每一种场景中。
这是程序合格并且健壮的关键!这里的问题是,有一些资源的配置是与代码的运行平台息息相关的。在我编写单元测试的时候,我无法(其实我能,这么说是为了让你领会文章的主题)获得绑定到JNDI树上的数据源。而在容器中,我只需要遍历JNDI树,询问是否可以获取数据源即可。
另外,想Spring这样的框架也是鼓励这样的开发模式的,至少控制反转挺受欢迎。不同于直接用代码配置数据库和队列,Spring框架是在运行时注入这些对象的,生活依然美好。
你以为我的博客会把网上到处都是控制反转的示例代码再罗列一遍么?当然不是。看下面的代码:
public class BusinessClazz implements SomethingReallyImportant {
private DataSource dataS
public void setDataSource(DataSource dataSource) {
this.dataSource = dataS
数据源是通过运行时注入获得的,BusinessClazz对数据源本身一无所知。我不是世上最聪明的人,但也肯定不是最笨的人。我是说,我曾经读过像《J2ee Development Without Ejb》和《Expert one-on-one J2EE design and development》这类的书,并且自以为已经理解了其中的内容。但这些都是给妹子们看的,不是么?开个玩笑。我在Spring中间了一个数据源实例,并注入到BusinessClazz实例中。
&bean name=&myBusinessClazz&&
&property name=&dataSource& ref=&dataSource&/&
现在,我的可以完全不考虑数据源的具体实现,仍能在所有的部署环境中运行。但对于数据源呢,应该如何配置才能保证“一次配置,到处运行”?我们将注重对数据源的配置做说明,这个例子也是用于那些因运行环境或运行时不同而需要做修改的组件。
一般情况下,数据源需要两部分配置信息。第一部分是关于数据库位于何处,以及如何连接该数据库的信息。这里需要的信息包括主机名、款口端口号、服务名等。
第二部分是关于如何展示描述配置信息的。下面是一些可选方案。在其中,我建立了一个到数据库的连接,
&bean id=&dataSource& destroy-method=&close&
class=&mons.dbcp.BasicDataSource&&
&property name=&driverClassName& value=&org.hsqldb.jdbcDriver&/&
&property name=&url& value=&jdbc:hsqldb:hsql://localhost&/&
&property name=&username& value=&sa&/&
&property name=&password& value=&&/&
也可以使用Apache的commons-dbcp库创建一个连接池,
&bean id=&dataSource&
class=&mons.dbcp.BasicDataSource& destroy-method=&close&&
&property name=&driverClassName& value=&net.sourceforge.jtds.jdbc.Driver&/&
&property name=&url& value=&jdbc:myserver&/&
&property name=&username& value=&username&/&
&property name=&password& value=&password&/&
&property name=&initialSize& value=&2&/&
&property name=&maxActive& value=&5&/&
&property name=&maxIdle& value=&2&/&
还可以使用容器来管理连接,再从容器中获取连接,
&jee:jndi-lookup id=&dataSource& jndi-name=&java:mydatasource&/&
当然,还有其他许多方法可以配置数据源,但为了说明问题,上面的示例已经够用了。Spring框架的类可以用来处理随运行环境而变化的属性,使用简单,功能强大。
现在的问题是,如何在不同的部署环境中无缝切换,并选择不同类型的数据源呢?
本文所说的问题并不起最近才有的,在过去的6、7年中,许多部署团队想出了各种不同的方法来解决它。每个团队都针对其特定的问题,采取了适合自身条件的方法来解决,他们会在代码中加入一些自适应的判断来简化问题。
这里,希望代码可以“一次编写,到处运行”的最大驱动力之一,就是可以开发人员编写用于测试生还 生产环境代码的单元测试。
对这个问题的一个典型的示例方案是将数据源定义在不同上下文文件中,这些文件需要按照统一的命名约定来命名,以免造成混乱。例如,假设我们有3个操作模式,所以需要建3个不同的上下文文件,
datasources-containerContext.xml
datasources-pooledConnection.xml
datasources-singleConnection.xml
接下来就是平台相关的代码了,会在你的应用程序初始化Spring的时候执行。它通过Ant式的通配符跨越部署的类路径来查找上下文文件。
ClassPathXmlApplicationContext containerContext =
new ClassPathXmlApplicationContext(&**/**-containerContext.xml&);
ClassPathXmlApplicationContext nonContainerContext =
new ClassPathXmlApplicationContext(&**/**-pooledContext.xml&);
ClassPathXmlApplicationContext testingContextContext =
new ClassPathXmlApplicationContext(&**/**-singleContext.xml&);
好了,在刚刚过去的几个小时里,你已经建立一个可以在多种环境中运行的系统,所编写的代码也充分利用这个特点。这个办法你已经用了好几年了,但是,这个办法的主要缺点是,每个团队、每个项目都会有自己的方式来解决前面提到的问题。
使用Spring Profile
Spring 3.1为这个问题提供了一个解决方案(如果你还没有为自己的项目升级Spring版本,嗯,你麻烦大了)。
Spring在容器中引入Environment和Profile的概念。每个应用程序上下文都有一个都可以访Environment对象。
ClassPathXmlApplicationContext classPathXmlApplicationContext =
new ClassPathXmlApplicationContext();
ConfigurableEnvironment configurableEnvironment =
classPathXmlApplicationContext.getEnvironment();
每种运行环境都有很多活动Profile类可供使用。大多数讲解Spring Profile的例子都是在开发模式或生产模式下。对于不同运行环境问题来说,我的解决方案是使用使用多个Profile来适应不同运行时。这个解决方案的优势是你可以自行决定如何使用Profile。
默认星空情况下,你所创建的Bean在载入容器中后是没有Profile对象的。下面看一个例子。假设下面是我的应用程序中,数据源实例的定义。
&?xml version=&1.0& encoding=&UTF-8&?&
&beans xmlns=&…&&
&bean id=&dataSource& destroy-method=&close&
class=&mons.dbcp.BasicDataSource&&
&property name=&driverClassName& value=&org.hsqldb.jdbcDriver&/&
&property name=&url& value=&jdbc:hsqldb:hsql://localhost&/&
&property name=&username& value=&sa&/&
&property name=&password& value=&&/&
在Spring 3.0中,增加了一个新的容器类 ,可以作为ClassPathXmlApplicationContext和FileSystemXmlApplicationContext之外的另一个选择。
GenericXmlApplicationContext类的特点是可以通过Setter方法完成所有的配置,而无需依靠笨重的构造器去完成配置。记住,在初始化容器的准备工作完成后,需要调用refresh()方法完成实际的初始化工作。
下面的代码展示了如何使用GenericXmlApplicationContext类初始化容器:
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.getEnvironment().setActiveProfiles(&standalone&);
ctx.load(&*Context.xml&);
ctx.refresh();
这里,我将活动Profile设置为“standalone”。在这个工程里,我希望代码既可以作为“standalone”运行在应用程序容器之外,还可以作为“container”运行在容器中。这里,我可以设置多个Profile,例如,下面的代码设置了Profile为“standalone”与“activemq”。
ctx.getEnvironment().setActiveProfiles(&standalone&, &activemq&);
虽然做了上面的配置,实际上并不会对当前的配置上下文产生影响,因为还没有配置Profile实例。所以,修改配置上下文为:
&?xml version=&1.0& encoding=&UTF-8&?&
&beans xmlns=&…& profile=&standalone&&
&bean id=&dataSource& destroy-method=&close&
class=&mons.dbcp.BasicDataSource&&
&property name=&driverClassName& value=&org.hsqldb.jdbcDriver&/&
&property name=&url& value=&jdbc:hsqldb:hsql://localhost&/&
&property name=&username& value=&sa&/&
&property name=&password& value=&&/&
只有当活动Profile设置为“standalone”时,才会实例化这个Bean。Profile是Bean的属性,而不是一个实例对象,因此,你无法配置单独的Bean来选择Profile。在较早的Spring版本中,这会导致产生多个文件,而Ant的通配符无法在运行时找到正确的配置文件。
在Spring 3.1中,&beans/&标签可以嵌套在&beans/&标签内。现在,我们重新编写一下数据源配置文件:
&?xml version=&1.0& encoding=&UTF-8&?&
&beans xmlns=&…&&
&beans profile=&standalone&&
&bean id=&dataSource&&
&property name=&driverClassName& value=&org.hsqldb.jdbcDriver&/&
&property name=&url& value=&jdbc:hsqldb:hsql://localhost&/&
&property name=&username& value=&sa&/&
&property name=&password& value=&&/&
&beans profile=&container&&
&jee:jndi-lookup id=&dataSource& jndi-name=&java:mydatasource&/&
这样,就可以通过下面的代码快速切换Profile:
ctx.getEnvironment().setActiveProfiles(&container&);
另一种切换Profile的方法是在运行时作为系统参数传入:
-Dspring.profiles.active=&standalone&
此外,也可以作为Ear/War的初始化参数传入:
&servlet-name&dispatcher&/servlet-name&
&servlet-class&
org.springframework.web.servlet.DispatcherServlet
&/servlet-class&
&init-param&
&param-name&spring.profiles.active&/param-name&
&param-value&production&/param-value&
&/init-param&
&/servlet&
阅读本文对你有什么帮助?
希望本文能够使你对Spring 3.1的新特性有所了解,这个新特性已经在Spring的3个主修订版中跳票了。
英文原文:,翻译: -
译文链接:
【如需转载,请在正文中标注并保留原文链接、译文链接和译者等信息,谢谢合作!】
关于作者:
码农一枚,热爱技术的技术菜鸟
很好的文章,get
关于ImportNew
ImportNew 专注于 Java 技术分享。于日 11:11正式上线。是的,这是一个很特别的时刻 :)
ImportNew 由两个 Java 关键字 import 和 new 组成,意指:Java 开发者学习新知识的网站。 import 可认为是学习和吸收, new 则可认为是新知识、新技术圈子和新朋友……
新浪微博:
推荐微信号
反馈建议:@
广告与商务合作QQ:
– 好的话题、有启发的回复、值得信赖的圈子
– 写了文章?看干货?去头条!
– 为IT单身男女服务的征婚传播平台
– 优秀的工具资源导航
– 活跃 & 专业的翻译小组
– 国内外的精选博客文章
– UI,网页,交互和用户体验
– JavaScript, HTML5, CSS
– 专注Android技术分享
– 专注iOS技术分享
– 专注Java技术分享
– 专注Python技术分享
& 2016 ImportNew

我要回帖

更多关于 build.xml classpath 的文章

 

随机推荐