I have a Spring Web Application with Maven 3 and Datasources/Beans configured for local development on a h2 Database. For Testing there exists a JBoss AS 7.1 Server.
Is it possible to override a Bean / DataSource in a special JBoss XML-File, so that for local (IDE) matters a DriverManagerDataSource is used (see first code posting), and a JndiObjectFactoryBean (second one), if my Application is deployed on a JBoss?
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
<!-- And so on -->">
<!--GENERAL-->
<context:annotation-config/>
<context:component-scan base-package="com.mysuperapp"/>
<!--DATASOURCES-->
<bean id="activitiDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:file:h2\activiti;MVCC=TRUE"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="hibernateDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:file:h2\hibernate;AUTO_SERVER=TRUE"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<!--TXMANAGEMENT-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="mysuperappPersistenceUnit"/>
<property name="dataSource" ref="hibernateDataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="packagesToScan" value="com.mysuperapp.model"/>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<entry key="hibernate.hbm2ddl.auto" value="update"/>
</map>
</property>
</bean>
<!-- And so on -->
custom applicationContext.xml datasources for JBoss
<bean id="activitiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:jboss/datasources/activitiDataSource"/>
</bean>
<bean id="hibernateDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:jboss/datasources/hibernateDataSource"/>
</bean>
Any help is greatly appreciated.
Your application creates the database connection on its own as i can see on your config.
Instead of doing this you could just define the datasource on the app-server and obtain it by a jndi lookup. So you can configure the database connection for each system separately.
<!-- DataSource-LookUp -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton">
<property name="jndiName" value="java:jboss/datasources/MyDataSourceDS" />
<property name="resourceRef" value="true" />
</bean>
Your JBoss has a file <jboss_home>/standalone/configuration/standalone.xml which is used if you don't provide a different one.
For testing purposes you could try and make a copy of that file in the same directory, e.g. naming it test.xml and add your datasource definition there. Then use it by starting JBoss with standalone.bat --server-config=test or standalone.bat -c test.
I managed to do this with a SVN patch, which is added prior to the automatic deployment. It changes the DataSource definitions.
Pay attention when creating the patch with windows and applying under linux with /usr/bin/patch (CR+LF problem, apply dos2unix filename to both patch and files to be patched).
Related
I have configured Hibernate(5.2.6) via spring(4.3.5) But when I run my Junit(4.12) tests it gives out an error saying that it could not locate hibernate.cfg.xml file. Can anyone help me out?
After I run the Junit test, tables are created in database but data is not added, I can see the sql queries being run as I have enables show_sql
did you specify hibernate configured xml file name in web.xml.?
if not specify name of xml file in web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:hibernateContext.xml</param-value>
</context-param>
How is applicationContext is configured for spring and hibernate integration?
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:properties/jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configLocation">
<value>classpath:hibernate/hibernate.cfg.xml</value>
</property>
</bean>
i'm using Apache Camel and i try to load datasource properties from this file
config.properties:
url = my_url
user = user_name
password = user_pass
this is dataSource (blueprint.xml):
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
<property name="URL" value="my_url"/>
<property name="user" value="user_name"/>
<property name="password" value="user_pass"/>
</bean>
How can i read values from config.properties and insert them into dataSource properties ?
You talk about blueprint.xml, and camel, so I assume you are in an osgi container like Karaf/ServiceMix, and you are using Aries Blueprint.
Then you can use the cm namespace and a property-placeholder. If you use camel and want your properties to be dynamically reloaded, then you can use too an update strategy reload, which start/stop the blueprint container when the configuration change. This will load the configuration with pid "datasource" (ie, in karaf, the file etc/datasource.cfg) :
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.2.0"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.2.0">
<cm:cm-properties id="myProps" persistent-id="datasource" update-strategy="reload"/>
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
<property name="URL" value="${url}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
</blueprint>
If you want to use your configuration file without using ConfigurationAdmin or dynamically reload your bundle, then you can use the ext namespace :
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.2.0">
<ext:property-placeholder>
<ext:location>file:config.properties</ext:location>
</ext:property-placeholder>
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
<property name="URL" value="${url}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
</blueprint>
According to code I assume you use probably spring as container. General solution in spring is to use PropertyPlaceHolder, your configuration will look like this:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>config.properties</value>
</property>
</bean>
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
<property name="URL" value="${jdbc.myUrl}"/>
<property name="user" value="${jdbc.user_name}"/>
<property name="password" value="${jdbc.user_pass}"/>
</bean>
Please check the example for details.
I've got the following pom.xml surefire config:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<printSummary>true</printSummary>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<includes>
<include>**/test/resources/META-INF/spring/test-database.properties</include>
<include>**/src/test/java/*.java</include>
</includes>
<excludes>
<exclude>**/*_Roo_*</exclude>
<exclude>**/database-*.properties</exclude>
</excludes>
</configuration>
</plugin>
And the following applicationContext.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- FOR UNIT TESTING ONLY -->
<!--
This will automatically locate any and all property files you have
within your classpath, provided they fall under the META-INF/spring
directory. The located property files are parsed and their values can
then be used within application context files in the form of
${propertyKey}.
-->
<context:property-placeholder location="classpath*:META-INF/spring/test-database.properties"/>
<bean id="externalProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="ignoreResourceNotFound"><value>true</value></property>
<property name="locations">
<list>
<value>classpath*:META-INF/conf/configuration.properties</value>
</list>
</property>
</bean>
<!--
Turn on AspectJ #Configurable support. As a result, any time you
instantiate an object, Spring will attempt to perform dependency
injection on that object. This occurs for instantiation via the "new"
keyword, as well as via reflection. This is possible because AspectJ
is used to "weave" Roo-based applications at compile time. In effect
this feature allows dependency injection of any object at all in your
system, which is a very useful feature (without #Configurable you'd
only be able to dependency inject objects acquired from Spring or
subsequently presented to a specific Spring dependency injection
method). Roo applications use this useful feature in a number of
areas, such as #PersistenceContext injection into entities.
-->
<context:spring-configured/>
<!--
This declaration will cause Spring to locate every #Component,
#Repository and #Service in your application. In practical terms this
allows you to write a POJO and then simply annotate the new POJO as an
#Service and Spring will automatically detect, instantiate and
dependency inject your service at startup time. Importantly, you can
then also have your new service injected into any other class that
requires it simply by declaring a field for your service inside the
relying class and Spring will inject it. Note that two exclude filters
are declared. The first ensures that Spring doesn't spend time
introspecting Roo-specific ITD aspects. The second ensures Roo doesn't
instantiate your #Controller classes, as these should be instantiated
by a web tier application context. Refer to web.xml for more details
about the web tier application context setup services.
Furthermore, this turns on #Autowired, #PostConstruct etc support. These
annotations allow you to use common Spring and Java Enterprise Edition
annotations in your classes without needing to do any special configuration.
The most commonly used annotation is #Autowired, which instructs Spring to
dependency inject an object into your class.
-->
<context:component-scan base-package="com.mms.pone.portal">
<context:exclude-filter expression=".*_Roo_.*" type="regex"/>
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<!--<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="${database.driverClassName}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="1800000"/>
<property name="numTestsPerEvictionRun" value="3"/>
<property name="minEvictableIdleTimeMillis" value="1800000"/>
<property name="validationQuery" value="SELECT 1"/>
<property name="initialSize" value="5" />
<property name="maxActive" value="25" />
</bean> -->
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="${database.driverClassName}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="1800000"/>
<property name="numTestsPerEvictionRun" value="3"/>
<property name="minEvictableIdleTimeMillis" value="1800000"/>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="classpath*:META-INF/test-persistence.xml" />
</bean>
</beans>
Everything seems right, but when I run mvn clean install or mvn test, the file compiles, shows no errors and shows the summary Tests Run: 0.
Any help would be greatly welcomed.
I finally found this link, discovered the issue to be double property declarations, both in my persistence.xml as well as in my applicationContext.xml. Once those were cleaned out and had my files looking:
applicationContext.xml:
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="${database.driverClassName}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<property name="testWhileIdle" value="false"/>
<property name="timeBetweenEvictionRunsMillis" value="1800000"/>
<property name="numTestsPerEvictionRun" value="3"/>
<property name="maxActive" value="1" />
<property name="minEvictableIdleTimeMillis" value="1800000"/>
</bean>
persistence.xml
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
Everything ran just fine.
I am using an XML configuration file that is loaded into my Java application using ApplicationContext.
The XML configuration file resolves its properties by reading from several property files using PropertyPlaceholderConfigurer.
I want to make each property file optional. I thought that this is done by setting ignoreUnresolsvablePlaceholders to true, however I am getting the following exception when I run the application (db-default.properties exists but db.properties does not):
Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [db.properties] cannot be opened because it does not exist
This is what my XML configuration looks like:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="placeholder-configurer-1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
<bean id="placeholder-configurer-2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="location" value="classpath:/db-default.properties"/>
</bean>
<bean id="placeholder-configurer-3" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="3"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:/db.properties</value>
</list>
</property>
</bean>
<bean id="MyDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
What do I need to do to make db.properties an optional property file?
<property name="ignoreResourceNotFound" value="true"/>
spring blog article
api docs
This should work too:
<context:property-placeholder ignore-resource-not-found="true" location="classpath:your.properties" ... />
I've a problem with the Flyway schema upgrader for Spring. Following code exists in my servlet.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/database"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
<bean id="flyway" class="com.googlecode.flyway.core.Flyway" init-method="migrate" depends-on="dataSource">
<property name="dataSource" ref="dataSource"/>
</bean>
But how to set the migration script directory or rather whats the default directory?
Everything is explained in the excellent documentation:
If you want to alter the default directory (which is db/migration as shown on the picture above), I believe this should work (see: Flyway.setBaseDir()):
<bean id="flyway" class="com.googlecode.flyway.core.Flyway" init-method="migrate" depends-on="dataSource">
<property name="dataSource" ref="dataSource"/>
<property name="baseDir" value="my/migrations/path"/>
</bean>