2016-07-28 8 views
0

Spring環境で異なる環境(Dev/Test/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 

そして、DEVクラス:これは私の作業スプリング-config.xmlには(そのまま)

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

ある

<?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> 

注釈付きクラスを定義しようとするときにdataSource Beanが見つからないため、アプリケーションがクラッシュします。

私の質問: 1 - これはどのように修正できますか?私は何も壊さずに別のプロファイルを使用したい:) 2 - 私はテスト用と生産用のビルドを行うためにアプリをビルドすることはできますか?特定のコマンドを使用すべきか、または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ではなく、xmlの設定を使用し続けます。

結論として、私はspring-config.xmlのデータソース情報と、sessionFactoryとannotatedClassesに関する情報を削除する必要があります。それらはすべて関連しているからです。だから私はすべてを置き換える方法を見つける必要があるので、私はpom.xmlから情報を得ることができます

+0

XML構成と注釈付き構成を混合しています。エラーを見つけるのは簡単ではありません。 – dit

+0

ええ、私は知っている:(新しく間違えて...どうすればいいですか? – Faabass

答えて

0

この回答を確認してください!!

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

あなたは使用してプロファイリングを行うためにMavenを使用することができます。

+0

私はそれを試して、私が探しているものに見えます...しかし、私はこれに従うと、私は休止状態の接続を私のsprint-context.xmlと私はそれを行うときにsessionFactoryが定義されておらず、クラスのautowiredが動作していないため、サーバを起動するときに失敗する...どのようにして機能に影響を与えずにこれをすべて削除できますか? – Faabass

+0

投稿できますか?あなたの新しい変更? –

+0

完了しました!お返事ありがとうございます! – Faabass

関連する問題