Spring remove printout Could not load properties from URL - java

I'm building a gradle plugin in which I want to give users the ability to override some properties from an external file.
I'm using the Spring propertyPlaceholderConfigurer in order to set a few destinations where users can place their files, This works as expected.
My problem is that when running my plugin Spring prints out to the console: "Could not load properties from URL [...]"
Does anyone have any idea how I would go about squelching this message?
I've tried overriding logback and sl4j to no avail.
My Spring application-context xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"
default-lazy-init="true">
<!-- Activates scanning of #Autowired -->
<context:annotation-config/>
<!-- Activates scanning of #Repository and #Service -->
<context:component-scan base-package="com.example.test"/>
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/conf/plugin.properties</value>
<value>file:${user.home}/conf/plugin.properties</value>
<value>file:${user.home}/conf/plugin-override.properties</value>
</list>
</property>
<property name="placeholderPrefix" value="${"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="systemPropertiesMode" value="2"/>
</bean>

If you want to implement custom logic to set locations you can extend PropertyPlaceholderConfigurer like
class CustomPropertyPlaceholderConfigurer extends org.springframework.beans.factory.config.PropertyPlaceholderConfigurer {
public void setFileName(String fileName) throws FileNotFoundException {
File file = findPropertyFile(fileName); // implement property file loading
super.setLocation(new FileSystemResource(file));
}
and use it like
<bean id="propertyPlaceholderConfigurer" class="package.CustomPropertyPlaceholderConfigurer ">
<property name="placeholderPrefix" value="${"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="systemPropertiesMode" value="2"/>
</bean>

As far as I understand there are no such files.
Try to add configs below to the propertyPlaceholderConfigurer
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="ignoreResourceNotFound" value="true" />

I found that the message was originating in PropertiesLoaderSupport
at the loadProperties method:
if (this.ignoreResourceNotFound) {
if (logger.isWarnEnabled()) {
logger.warn("Could not load properties from " + location + ": " + ex.getMessage());
}
}
So what I ended up doing is set the logger level in my gradle task to Error as follows:
task.doFirst {
logger.context.level = LogLevel.ERROR
logging.captureStandardOutput LogLevel.ERROR
}

Related

Spring MVC with JPA: Not able to instantiate JpaRepository from a jar

I am trying to import a standard spring project P1 into a web project P2 and I am implementing the web aspect of P1 in P2.
I have imported all resources of P1 in P2 as P1.jar
Have explicitly imported the application context file as well using <import-resource> which happens successfully.
But the JpaRepositories does not get autowired in P2. It doesn't seem to be in the context of P2.
Can anyone suggest what I might be missing here.
UPDATE: 25Nov2016
P1-ApplicationContext.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:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.11.xsd">
<tx:annotation-driven proxy-target-class="true"/>
<context:component-scan base-package="com.home.p1.blog" />
<jpa:repositories base-package="com.home.p1.blog.repo" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
<property name="url"
value="jdbc:derby://localhost:1527/MyDerby" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="WorkUp" />
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan" value="com.home.p1.blog"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.use_sql_comments">false</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="authorDAO" class="com.home.p1.blog.repo.AuthorDAO">
</bean>
<bean id="authorService" class="com.home.p1.blog.service.AuthorServiceImpl"></bean>
<bean id="blogService" class="com.home.p1.blog.service.BlogServiceImpl"></bean>
</beans>
P2-ApplicationContext.xml (its actually named: rest-servlet.xml to hold the RestController scan package)
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.11.xsd">
<import resource="classpath:com.home.p1.blog.src/src/main/resources/P1-ApplicationContext.xml"/> <!-- this loads find -->
<context:component-scan base-package="com.home.p2.blog.controller" /> <!-- this will load the RestController -->
<!-- <context:component-scan base-package="com.home.**" /> -->
<!-- THIS GIVES SOME WIERD ERROR <jpa:repositories base-package="com.home.*" /> -->
<mvc:annotation-driven/>
<!-- <bean id="blogService" class="com.oracle.blog.service.BlogServiceImpl"></bean> -->
</beans>
UPDATE: 28Nov2016
Upon including <jpa:repositories base-package> configuration in P2's application context, I no longer get the wierd error which I was getting earlier.
Also, enabling a successful import of xml config in Spring bean support, everything else is falling appropriately into place. Only the JpaRepositories referred in P1 are not reflecting in P2's context.
I dont think what you are trying to do is possible. You need to enable repository scan in your p2 and mention the packages in that scan.
If your p1.jar is in the classpath of p2 then you only have enable repository scan in your p2,
<jpa:repositories base-package="com.home.p1.blog.repo" />
RESOLVED: Please note that to import the JpaRepositories from P1.jar in P2, we don't have to mention <jpa:repositories> tag in P2's config xml.
Just ensure that we are appropriately importing the P1's config.xml.
Also Eclipse users need to add Xml / Java config in Spring Beans Support tab as well as enable support for import element.

class path resource [target/jdbc.properties] cannot be opened because it does not exist

I am making a simple spring application. I already programed this on my other computer and decided to do it again from scratch to really commit it into memory. However my properties file for some reason is not in the class path. I right clicked the properties file and selected "copy qualified name" in eclipse to get the files path. However it is saying the file does not exsist and I find this very weird because I looked at my working example and it is in the same folder "target" and works fine.
When I click "copy qualified name" I get /DatabaseSpringframework/target/jdbc.properties I took away /DatabaseSpringframework/ and left the target/jdbc.properties and still the same error.
Here is my xml file...
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<context:component-scan base-package="com.learntoprogram"></context:component-
scan>
<context:property-placeholder location="target/jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="username" value="${jdbc.username}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driver}"></property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
</bean>
</beans>
Change location="target/jdbc.properties" to location="classpath:jdbc.properties".
That finds the property file in the classpath.

PersistenceUnitInfo [appName] has transactionType JTA, but does not have a jtaDataSource defined

I am new to WebLogic and fairly new to Hibernate / Spring applications, as my primary language is C# and my primary servers have always been Windows Servers, so please forgive any simple errors I may have.
I am having trouble deploying to our WebLogic 10.3.4 server. It works locally on my WebLogic instance, but not on the remote server.
I am using Hibernate 4.2.8 for persistence and Spring MVC 4.0 for my web application framework. The error I am receiving is:
Failed to load webapp: 'ncms2_May20.war'
Message icon - Error Substituted for missing class Exception [EclipseLink-28010] (Eclipse Persistence Services - 2.1.2.v20101206-r8635) - org.eclipse.persistence.exceptions.EntityManagerSetupException Exception Description: PersistenceUnitInfo ncms2 has transactionType JTA, but does not have a jtaDataSource defined.
I am using a Spring annotation based Hibernate configuration file.
package mil.navy.navsupbsc.utilities;
import java.util.Properties;
import javax.sql.DataSource;
import com.app.AuditInterceptor;
import org.hibernate.SessionFactory;
import org.hibernate.dialect.Oracle10gDialect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
#Configuration
public class HibernateConfiguration {
#Value("#{dataSource}")
private DataSource dataSource;
#Bean
public LocalSessionFactoryBean sessionFactoryBean() {
Properties props = new Properties();
props.put("hibernate.dialect", Oracle10gDialect.class.getName());
props.put("hibernate.format_sql", "true");
props.put("hibernate.hbm2ddl.auto", "update");
props.put("hibernate.show_sql", "true");
props.put("hibernate.format_sql", "true");
props.put("hibernate.use_sql_comments", "true");
LocalSessionFactoryBean bean = new LocalSessionFactoryBean();
bean.setEntityInterceptor(new AuditInterceptor());
bean.setPackagesToScan(new String[] { "com.app.entity" });
bean.setHibernateProperties(props);
bean.setDataSource(this.dataSource);
return bean;
}
#Bean
public HibernateTransactionManager transactionManager() {
return new HibernateTransactionManager(sessionFactoryBean().getObject());
}
}
My Spring Servlet 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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.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.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}"
p:username="${jdbc.username}" p:password="${jdbc.password}" />
<mvc:annotation-driven />
<tx:annotation-driven transaction-manager="transactionManager" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:component-scan base-package="com.app" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
</beans>
Could any of you assist me in getting this to work? I appreciate any and all help. Thank you!
Update
I did the following to switch it to JTA:
added a JtaTransactionManager in the Hibernate java configuration file rather than using the HibernateTransactionManager.
set the JtaTransactionManager property in the LocalSessionFactoryBean
added a jdni lookup in the spring-servlet file
added a resource reference in both the web.xml and weblogic.xml files
removed the data source in spring-servlet and removed the reference to it in the hibernate java configuration file
created a data source on the server
Still not working 100% though. Will keep this updated.
UPDATE: Here's a helpful resource that I've been using (http://spring.io/blog/2011/08/15/configuring-spring-and-jta-without-full-java-ee/)
You haven't posted your persistence.xml JPA configuration file but it may contain something like:
<persistence-unit name="..." transaction-type="JTA">
<jta-data-source>java:/DefaultDS</jta-data-source>
When deploying on application servers you should benefit from their own JTA data sources and transaction manager support, so locating the jtaDataSource through JNDI should be your first option.
Your data source is configured with Spring:
org.springframework.jdbc.datasource.DriverManagerDataSource
and that's not really a production ready data source implementation.
So, try to configure your Spring application context to make use of the WebLogic transaction management support.
I used the information given in the answer by #Vlad Mihalcea to improve my code; however, it was not the reason it was not working. One of #Vlad's comments that suggested looking for a persistence.xml file in the META-INF folder clued me into the answer.
Even though I was using the Spring configuration file for Hibernate, I had an old persistence.xml file under the META-INF folder. I wasn't using it, but WebLogic was picking it up automatically. Since I wasn't using it, I didn't have a data source specified in the persistence.xml file. WebLogic automatically assumes any persistence unit is JTA if not specified as RESOURCE LOCAL. I deleted that persistence.xml file, and it worked.
That said, I also fixed up my code to use the JTA data source as #Vlad Mihalcea suggested. I moved all my configuration code to the Spring-servlet.xml file to simplify the configuration. I'm sure it could be translated into the programmatic Spring configuration without too much trouble.
In the meantime, here is a working JTA transaction based Spring / Hibernate configuration file.
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.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.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="packagesToScan" value="mil.navy.navsupbsc.entity" />
<property name="dataSource" ref="NCS"/>
<property name="jtaTransactionManager" ref="transactionManager" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.driverClassName">oracle.jdbc.OracleDriver</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
</props>
</property>
</bean>
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:component-scan base-package="mil.navy.navsupbsc" />
<tx:annotation-driven transaction-manager="transactionManager" />
<jee:jndi-lookup id="NCS" jndi-name="NCS" resource-ref="false">
</jee:jndi-lookup>
<tx:jta-transaction-manager id="transactionManager" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
</beans>
Note that I also needed to add references in the web.xml and WebLogic.xml files to the JNDI data source I created on the web server (called NCS in my application).

include database.properties from spring project into web.xml

I have a database project which is using spring. For that I have two important files in src/META-INF/spring/(database project)
The first one is the cmn-dao-spring.xml . The other one is the database.properties.
In my tomcat webapp project I am able to load with that code all the needed context-files:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/META-INF/spring/*.xml
</param-value>
</context-param>
The problem is that the database.properties is not loaded.
If I change the xml to this:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/META-INF/spring/*
</param-value>
</context-param>
I get the Exception:
Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog.
because the properties is no valid xml.
The startup of my tomcat fails.
How can I include the database.properties from my cmn-dao project in my webapp?
EDIT
That is my cmn-dao.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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<tx:annotation-driven />
<context:annotation-config />
<!-- DATABASE CONFIGURATION -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>META-INF/spring/database.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<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>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- BEAN DEFINITIONS -->
<bean id="scoreMapper" class="de.bc.qz.dao.mapper.ScoreMapper"
autowire="byName">
<constructor-arg value="s." />
</bean>
<bean id="scoreExtractor" class="de.bc.qz.dao.extractor.ScoreExtractor"
autowire="byName">
</bean>
<bean id="questionMapper" class="de.bc.qz.dao.mapper.QuestionMapper"
autowire="byName">
<constructor-arg value="q." />
</bean>
<bean id="complaintMapper" class="de.bc.qz.dao.mapper.ComplaintMapper"
autowire="byName">
<constructor-arg value="c." />
</bean>
<bean id="scoreDao" class="de.bc.qz.dao.ScoreDao" autowire="byName">
<property name="dataSource" ref="dataSource" />
<property name="LAUSFT">
<value>
SELECT * FROM (
SELECT s.*, #rank
:= #rank + 1 rank
FROM
quiz.score s, (SELECT #rank := 0) init
ORDER BY points DESC
) s
WHERE rank BETWEEN ? AND ?
ORDER BY rank;
</value>
</property>
<property name="LUS">
<value>
SELECT id
FROM quiz.score
WHERE username = ? AND uuid = ?;
</value>
</property>
</bean>
<bean id="complaintDao" class="de.bc.qz.dao.ComplaintDao"
autowire="byName">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="questionDao" class="de.bc.qz.dao.QuestionDao" autowire="byName">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
The JUnit's for cmn-dao works absolut correct and the placeholder works too.
Inside tomcat project I have added the related projects via Deployment Assembly.
Thx for your help
Stefan
use spring util to read properties file.
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<context:annotation-config />
<util:properties id="db_config" location="classpath:db_config.properties"></util:properties>
</beans>
user spring new '#value' annotation to get the property file key=value i.e
example : db_config.properties contains
db_user_name = uttesh
db_password = password
to get "db.user.name" property value use below code
#Value("#{db_config[db_user_name]}")
private String dbUsername;
contextCongifLocation is for spring configuration files only.
use this in your spring config (xml) file for loading properties:
<bean id="props" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:META-INF/spring/database.properties"/>
</bean>
or
<context:property-placeholder location="classpath:META-INF/spring/database.properties" />
Try to use the util namespace instead, it allows to load several property files and separate them into property groups:
<util:properties id="application" location="classpath:application.properties"/>
To use the properties:
<bean id="postgresDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="#{application.driverClassName}"/>
<property name="url" value="#{application.url}"/>
<property name="username" value="#{application.username}"/>
<property name="password" value="#{application.password}"/>
</bean>
This is the properties file example:
driverClassName=org.postgresql.Driver
url=jdbc:postgresql://localhost:5432/somedatabase
username=dummy
password=dummy
Change your context file to something like that :
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/META-INF/spring/*.properties
</param-value>
</context-param>
Use contextConfigLocation in web.xml to read your main spring config xml file.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/META-INF/spring/spring-app-context.xml
</param-value>
</context-param>
import cmn-dao.xml in spring-app-context.xml
<import resource="classpath:/META-INF/spring/cmn-dao.xml" />
Now use PropertyPlaceholderConfigurer to read database.properties in cmn-dao.xml
<bean id="props" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:META-INF/spring/database.properties"/>
</bean>
This is how you load property files into your spring context:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
</beans>

Getting the present working directory using spring

This is the code to get the present working directory of a java application at runtime.
String currentWorkingDirectory = System.getProperty("user.dir")+System.getProperty("file.separator");
Is there any way by which this can be configured using the spring-context xml.
For ex:
<bean id="csvReportGenerator" class="some.path.CSVReportGenerator">
<constructor-arg name="outputFileName" value="${currentWorkingDirectory}/${reportOutputFileGeneric}"/>
</bean>
Yes, you can do it using Spring expressions. See section 6.4.1 of this article
<property name="userDir" value="#{ systemProperties['user.dir'] }"/>
<property name="fileSep" value="#{ systemProperties['file.separator'] }"/>
You can simply use classpath: or can use ./ if you are deploying in an unix environment(which usually is). Say, classpath:sample.properties or ./sample.properties
In spring-context.xml You can use
1) classpath:filename.properties or
2) ./filename.properties
3) file:./
For current dir of context-xml, ./ should work, but for working dir, file:./ works fine.
eg.
<?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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:annotation-config />
<bean id="properties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="singleton" value="true" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:/shaharma.properties</value>
<value>./shaharma-custom.properties</value>
</list>
</property>
</bean>
</beans>

Categories