gpt4 book ai didi

java - 无需 Maven 配置文件即可工作,有错误

转载 作者:行者123 更新时间:2023-12-01 18:12:53 24 4
gpt4 key购买 nike

这是我的问题

这是我在 pom.xml 中的配置文件:

<profiles>
<profile>
<id>dev</id>
<activation>
<property>
<name>environment</name>
<value>dev</value>
</property>
</activation>
</profile>

<profile>
<id>prod</id>
<activation>
<property>
<name>environment</name>
<value>prod</value>
</property>
</activation>
</profile>
</profiles>

当我使用这个 context.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/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jd="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:sws="http://www.springframework.org/schema/web-services"
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.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3-0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<context:component-scan base-package="net.xxx.xxx.dao" />

<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@xxx.xxx.xxx.xxx:1521:essmh" />
<property name="username" value="*****" />
<property name="password" value="*****" />
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="persistenceUnitName" value="carfleetPersistenceUnit" />
<property name="dataSource" ref="dataSource" />
</bean>

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven />
</beans>

但我希望能够根据我是处于生产状态还是处于开发状态来更改我的dataSource

这就是我现在得到的:

 <beans profile="dev">
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@xxx.xxx.xxx.xxx:1521:dev" />
<property name="username" value="*****" />
<property name="password" value="*****" />
</bean>
</beans>

<beans profile="prod">
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@xxx.xxx.xxx.xxx:1522:prod" />
<property name="username" value="*****" />
<property name="password" value="*****" />
</bean>
</beans>

<beans profile="dev, prod">

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="persistenceUnitName" value="carfleetPersistenceUnit" />
<property name="dataSource" ref="dataSource" />
</bean>

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven />

</beans>

我使用mvn -e -P prod help:active-profiles检查配置文件prod在每个包中是否处于 Activity 状态,而且确实如此。但最后,它不起作用,我收到错误:

由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:未定义类型为 [javax.persistence.EntityManagerFactory] ​​的唯一 bean:预期为单个 bean,但发现为 0。

肯定存在错误,因为这是从“应用程序正在运行”到“应用程序有错误”的唯一更改。

<小时/>

编辑

我现在明白我混淆了 Spring 配置文件和 Maven 配置文件,但我仍然想链接它们两个。因此,为此,我将属性放入我的 pom.xml > 配置文件中:

<profiles>
<profile>
<id>dev</id>
<properties>
<environment>dev</environment>
</properties>
</profile>

<profile>
<id>prod</id>
<properties>
<environment>prod</environment>
</properties>
</profile>
</profiles>

我在 web.xml 中添加:

<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>${environment}</param-value>
</context-param>

当我为 devprod 硬编码 ${environment} 时,它工作得很好。但是当我希望能够从 Maven 切换时: clean -Pprod package tomcat7:redeploy我遇到以下问题:

IllegalArgumentException : Could not resolve placeholder 'environment' in string value "${environment}`

最佳答案

您正在混合 Maven 配置文件和 Spring 配置文件!这些是不同的事情。您可以使用

spring.profiles.active

启用您想要的配置文件。

关于java - 无需 Maven 配置文件即可工作,有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31612216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com