java - Trying to set up different profiles in Spring & Hibernate -


i need set different profiles in spring app in i'm using hibernate allow me switch different environments (dev/test/prd). below have issue have once create properties files, classes , set settings. application crash. below have dev properties file:

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 

and dev class:

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

this working spring-config.xml (without change)

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

when remove datasource bean(because want dynamic) , added following:

<beans profile="dev">         <!-- allows ${} replacement in spring xml configuration              application-default.properties, application-dev files on classpath -->         <context:property-placeholder             location="classpath:properties/application-default.properties, classpath:properties/application-dev.properties"             ignore-unresolvable="true" />          <!-- scans annotated classes in com.env.dev package -->         <context:component-scan base-package="com.granojo.conf.devenv" />     </beans>      <beans profile="test">         <!-- allows ${} replacement in spring xml configuration              application-default.properties, application-test files on classpath -->         <context:property-placeholder             location="classpath:properties/application-default.properties, classpath:properties/application-test.properties"             ignore-unresolvable="true" />          <!-- scans annotated classes in com.env.test package -->         <context:component-scan base-package="com.granojo.conf.testenv" />     </beans>      <beans profile="prod">         <!-- allows ${} replacement in spring xml configuration              application-default.properties, application-prod files on classpath -->         <context:property-placeholder             location="classpath:properties/application-default.properties, classpath:properties/application-prod.properties"             ignore-unresolvable="true" />          <!-- scans annotated classes in com.env.prod package -->         <context:component-scan base-package="com.granojo.conf.prodenv" />     </beans> 

the application crash because doesn't find datasoruce bean when trying define annotated classes.

my questions: 1 - how can fix this? want use different profiles without breaking :) 2 - how can build app test , production using maven? should use specific command or should include in pom.

update:

i add following 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> 

and this, can choose profile use... didn't make change in spring-config.xml because if take there break connection hibernate. application keep using config have in xml instead 1 in profile.

so in conclusion, need remove datasource information in spring-config.xml , information sessionfactory , annotatedclasses... because related. need find way replace can take information pom.xml

please check answer !!

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

you can use maven profiling using.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -