2016-07-28 59 views
0

不同的配置文件,我需要設置不同的配置文件在我的春天應用程序中,我使用休眠,讓我從不同的環境(開發/測試/ PRD)切換。 下面你有 我的問題是,一旦我創建了我所有的屬性文件,類和設置設置。應用程序崩潰。 下面你有dev的屬性文件:試圖建立在Spring和Hibernate

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 
       + "]"; 
    } 
} 

這是我工作的彈簧-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> 

當我刪除數據源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中包含某些內容。

UPDATE:

我添加以下畝的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是因爲如果我在那裏使用它,會破壞與休眠的連接。所以應用程序繼續使用我在xml中的配置,而不是配置文件中的配置。

所以最後,我需要刪除我的spring-config.xml中的數據源信息以及關於sessionFactory和annotatedClasses的信息......因爲它們都是相關的。所以,我需要找到一種方法來替代所有的,所以我可以從我的pom.xml以信息

+0

您正在將XML配置與註釋配置混合使用。不容易找到錯誤。 – dit

+0

是的,我知道:(newbwy錯誤..我怎麼可以用一種方法做到這一點? – Faabass

回答

0

請檢查這個答案!

https://stackoverflow.com/a/8511414/4287445

您可以使用Maven用做分析。

+0

我試過了,我期待的是我尋找的東西......但是如果我遵循這一點,我需要刪除休眠連接我的sprint-context.xml,當我這樣做時,sessionFactory沒有被定義,並且類的自動裝配不起作用,從而在啓動服務器時產生失敗......我怎樣才能在不影響功能的情況下移除所有這些? – Faabass

+0

你的新的變化? –

+0

完成!感謝您檢閱! – Faabass