我需要在我的spring应用程序中设置不同的配置文件,我在其中使用hibernate来允许我从不同的环境(Dev / Test / PRD)切换。 你有下面的 我遇到的问题是,一旦我创建了所有属性文件,类并设置了设置。应用程序崩溃。 下面是dev属性文件:
profile.name=dev.profiles
# Database Properties
db.driverClass=com.mysql.jdbc.Driver
db.connectionURL=jdbc:mysql://localhost:3306/granojo_db
db.username=root
# db.password=dev_pss
然后是开发课:
@Component
public class DevEnv implements GenericEnv {
private String envName = "dev";
@Value("${profile.name}")
private String profileName;
public String getEnvName() {
return envName;
}
public void setEnvName(String envName) {
this.envName = envName;
}
public String getProfileName() {
return profileName;
}
public void setProfileName(String profileName) {
this.profileName = profileName;
}
@Override
public String toString() {
return "DevEnv [envName=" + envName + ", profileName=" + profileName
+ "]";
}
}
这是我的spring-config.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"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config />
<context:component-scan base-package="com.granojo.controller" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<mvc:annotation-driven />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/granojo_db" />
<property name="username" value="root" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.granojo.model.Category</value>
<value>com.granojo.model.Role</value>
<value>com.granojo.model.Status</value>
<value>com.granojo.model.Subcategory</value>
<value>com.granojo.model.User</value>
<value>com.granojo.model.Video</value>
<value>com.granojo.model.Menu</value>
<value>com.granojo.model.Watches</value>
<value>com.granojo.model.Image</value>
<value>com.granojo.model.Program</value>
<value>com.granojo.model.Seasson</value>
<value>com.granojo.model.Advertisement</value>
<value>com.granojo.model.Sponsor</value>
<value>com.granojo.model.Content</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="categoryService" class="com.granojo.services.CategoryServiceImpl"></bean>
<bean id="categoryDao" class="com.granojo.dao.CategoryDaoImpl"></bean>
<bean id="subcategoryService" class="com.granojo.services.SubcategoryServiceImpl"></bean>
<bean id="subcategoryDao" class="com.granojo.dao.SubcategoryDaoImpl"></bean>
<bean id="videoService" class="com.granojo.services.VideoServiceImpl"></bean>
<bean id="videoDao" class="com.granojo.dao.VideoDaoImpl"></bean>
<bean id="menuService" class="com.granojo.services.MenuServiceImpl"></bean>
<bean id="menuDao" class="com.granojo.dao.MenuDaoImpl"></bean>
<bean id="programService" class="com.granojo.services.ProgramServiceImpl"></bean>
<bean id="programDao" class="com.granojo.dao.ProgramDaoImpl"></bean>
<bean id="seassonService" class="com.granojo.services.SeassonServiceImpl"></bean>
<bean id="seassonDao" class="com.granojo.dao.SeassonDaoImpl"></bean>
<bean id="userService" class="com.granojo.services.UserServiceImpl"></bean>
<bean id="userDao" class="com.granojo.dao.UserDaoImpl"></bean>
<bean id="advertisementService" class="com.granojo.services.AdvertisementServiceImpl"></bean>
<bean id="advertisementDao" class="com.granojo.dao.AdvertisementDaoImpl"></bean>
<bean id="roleService" class="com.granojo.services.RoleServiceImpl"></bean>
<bean id="roleDao" class="com.granojo.dao.RoleDaoImpl"></bean>
<bean id="sponsorService" class="com.granojo.services.SponsorServiceImpl"></bean>
<bean id="sponsorDao" class="com.granojo.dao.SponsorDaoImpl"></bean>
<bean id="watchesDao" class="com.granojo.dao.WatchesDaoImpl"></bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<property name="maxUploadSize" value="9999990000000"/>
</bean>
<bean id="jwtTokenAuthFilter" class="com.granojo.security.JWTTokenAuthFilter" />
</beans>
当我删除dataSource bean时(因为现在我希望它是动态的)并添加了以下内容:
<beans profile="dev">
<!-- allows for ${} replacement in the spring xml configuration from the
application-default.properties, application-dev files on the classpath -->
<context:property-placeholder
location="classpath:properties/application-default.properties, classpath:properties/application-dev.properties"
ignore-unresolvable="true" />
<!-- scans for annotated classes in the com.env.dev package -->
<context:component-scan base-package="com.granojo.conf.DevEnv" />
</beans>
<beans profile="test">
<!-- allows for ${} replacement in the spring xml configuration from the
application-default.properties, application-test files on the classpath -->
<context:property-placeholder
location="classpath:properties/application-default.properties, classpath:properties/application-test.properties"
ignore-unresolvable="true" />
<!-- scans for annotated classes in the com.env.test package -->
<context:component-scan base-package="com.granojo.conf.TestEnv" />
</beans>
<beans profile="prod">
<!-- allows for ${} replacement in the spring xml configuration from the
application-default.properties, application-prod files on the classpath -->
<context:property-placeholder
location="classpath:properties/application-default.properties, classpath:properties/application-prod.properties"
ignore-unresolvable="true" />
<!-- scans for annotated classes in the com.env.prod package -->
<context:component-scan base-package="com.granojo.conf.ProdEnv" />
</beans>
应用程序崩溃,因为在尝试定义带注释的类时它没有找到dataSoruce bean。
我的问题: 1 - 我该如何解决这个问题?我想使用不同的配置文件而不破坏任何东西:) 2 - 如何使用maven为测试和生产构建应用程序?我应该使用特定命令还是应该在pom中包含一些内容。
更新:
我将以下内容添加到mu pom.xml
<profiles>
<profile>
<id>test</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources/properties/default</directory>
</resource>
<resource>
<directory>src/main/resources/properties/test</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<id>dev</id>
<build>
<resources>
<resource>
<directory>src/main/resources/properties/default</directory>
</resource>
<resource>
<directory>src/main/resources/properties/dev</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
有了这个,我可以选择使用哪个配置文件...但我没有在spring-config.xml中进行任何更改,因为如果我在那里取消它会破坏与hibernate的连接。因此,应用程序继续使用我在该xml中的配置而不是配置文件中的配置。
总而言之,我需要删除spring-config.xml中的数据源信息以及有关sessionFactory和annotatedClasses的信息...因为它们都是相关的。所以我需要找到一种方法来替换所有这些,这样我就可以从我的pom.xml中获取信息