Java 如何利用bean:profile将SpringXML从3.0转换为3.1
我在3.0中有一个xml,如下所示:Java 如何利用bean:profile将SpringXML从3.0转换为3.1,java,spring,spring-mvc,spring-3,Java,Spring,Spring Mvc,Spring 3,我在3.0中有一个xml,如下所示: <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" v
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.internal.url}" />
<property name="username" value="${jdbc.internal.username}" />
<property name="password" value="${jdbc.internal.password}"/>
</bean>
我会遇到如下错误:
Invalid content was found starting with element 'bean'. One of '{"http://www.springframework.org/schema/beans":beans}'
问题
我如何使用bean:profile
,以便仅当活动概要文件为dev
更新
我的定义是:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">
我也遇到了同样的问题:我从未成功地将
嵌套在另一个
中,即使spring beans.xsd
是正确的
我的(部分)解决方案是从
<beans profile="dev, qualif"
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.xsd">
</beans>
必须将所有嵌套的
声明放在配置文件的最后。这就是XML模式的定义方式,您必须遵守这一点
另见
- :
spring-beans-3.1.xsd
已更新为允许此嵌套,但仅允许文件中最后一个元素
这是设计的
从:
spring-beans-3.1.xsd已更新为允许这种嵌套,但被限制为只允许文件中最后一个元素。这将有助于提供灵活性,而不会导致XML文件混乱。虽然此增强是为bean定义概要文件服务开发的,但嵌套元素通常是有用的。假设您在给定文件中有一个子集bean,它应该被标记为lazy init=“true”。您可以声明一个嵌套元素,而不是标记每个bean,其中的所有bean都将继承该默认值。文件中其他地方定义的bean将保持lazy init=“false”的正常默认值。这适用于元素的所有default-*属性,例如default lazy init、default init method、default destroy method等等
谢谢这解决了我的问题。但是,现在在启动应用程序时,我遇到了一些异常情况,例如:
无法自动连线字段
,因为我有@Autowired
。我已经在web.xml中设置了活动配置文件。也许我会为此提出一个新问题……也谢谢;)我在文档中遗漏了这一行。@Tomasz我创建了一个单独的问题:
<beans profile="dev, qualif"
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.xsd">
</beans>