Tomcat startup takes huge time with my Spring application - java

I have Spring project which produces 42MB war file. Whenever I deploy it in a server it is taking more than an hour with tomcat 7. Even in development environment it is consuming more than 20 mins in the local environment if we make any changes in it.
I suspect I have a major mistake.
<?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:jpa="http://www.springframework.org/schema/data/jpa"
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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.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.xsd">
<context:component-scan base-package="com.abc.xyz.server.filters" />
<context:component-scan base-package="com.abc.xyz.server.utils" />
<context:component-scan base-package="com.abc.xyz.server.application.Bean" />
<context:component-scan
base-package="com.abc.xyz.server.application.controllers" />
<context:component-scan
base-package="com.abc.xyz.server.application.services" />
<jpa:repositories
base-package="com.abc.xyz.server.application.repositories" />
<mvc:annotation-driven>
<mvc:message-converters>
<!-- Use the HibernateAware mapper instead of the default -->
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean
class="com.abc.xyz.server.utils.HibernateAwareObjectMapper" />
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- <bean id="jdbcEmployeeDAO" class="com.abc.xyz.server.application.model.dao.JDBCEmployeeDAOImpl">
</bean> -->
<bean id="jdbcProperty"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/application.properties</value>
</property>
</bean>
<bean id="abstractDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
p:username="${jdbc.username}" p:password="${jdbc.password}">
<property name="initialSize" value="0" />
<property name="maxIdle" value="1" />
<property name="minIdle" value="0" />
<property name="validationQuery" value="SELECT 1" />
<property name="testOnBorrow" value="true" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="180000" />
<property name="numTestsPerEvictionRun" value="100" />
<property name="minEvictableIdleTimeMillis" value="120000" />
<property name="removeAbandonedTimeout" value="60" />
<property name="logAbandoned" value="true" />
<property name="maxActive" value="30" />
<property name="maxWait" value="3000" />
<property name="removeAbandoned" value="true" />
</bean>
<bean id="concreteDataSourceOne" parent="abstractDataSource"
p:url="${jdbc.databaseurlOne}" />
<bean id="concreteDataSourceTwo" parent="abstractDataSource"
p:url="${jdbc.databaseurlTwo}" />
<bean id="concreteDataSourceDev" parent="abstractDataSource"
p:url="${jdbc.databaseurlDev}" />
<bean id="concreteDataSourceGlace" parent="abstractDataSource"
p:url="${jdbc.databaseurlGlace}" />
<bean id="dataSource"
class="com.abc.xyz.server.datasource.TennantAwareDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="database2" value-ref="concreteDataSourceTwo" />
<entry key="database1" value-ref="concreteDataSourceOne" />
<entry key="dev" value-ref="concreteDataSourceDev" />
<entry key="glace" value-ref="concreteDataSourceGlace" />
</map>
</property>
<property name="defaultTargetDataSource" ref="concreteDataSourceGlace" />
</bean>
<bean id="objectmapper" class="com.fasterxml.jackson.databind.ObjectMapper"></bean>
<bean id="EntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:persistenceUnitName="PersistenceUnit" p:dataSource-ref="dataSource">
<!-- THIS IS WHERE THE MODELS ARE -->
<property name="packagesToScan"
value="com.abc.xyz.server.application.models" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:showSql="${hibernate.show_sql}" p:generateDdl="false"
p:databasePlatform="${hibernate.dialect}" />
</property>
</bean>
<!-- Ecache related stuff -->
<!-- <cache:annotation-driven /> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cache-manager-ref="ehcache"/> <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:ehcache.xml" p:shared="true"/> -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="EntityManagerFactory" />
</bean>
<tx:annotation-driven />
<bean id="sessionMap" class="com.abc.xyz.server.utils.SessionMap"
scope="session">
<!-- this next element effects the proxying of the surrounding bean -->
<aop:scoped-proxy />
</bean>
<!-- Configuration Bean -->
<mvc:default-servlet-handler />
<mvc:interceptors>
<bean class="com.abc.xyz.server.utils.EMRRequestInterceptor" />
</mvc:interceptors>
<!-- Swagger --><!--
<context:property-placeholder location="classpath:/swagger.properties" />
<bean id="springSwaggerConfig"
class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" /> -->
<!-- File Upload --> <!--
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> -->
I also get GC messages like this in the console.
[GC [PSYoungGen: 605920K->128K(660480K)] 1237105K->631313K(2058752K),
0.0141920 secs] [Times: user=0.03 sys=0.00, real=0.02 secs]

Related

How to make Spring JMS pick destination queue name from annotation #JmsListener

Any help will be greatly appreciated. I am trying to create a replacement for MDB with spring JMSListener. I wanted the destination name to be passed as a annotation, but i noticed that org.springframework.jms.listener.DefaultMessageListenerContainer container config has destinationName or destination as mandatory. Am i missing something ? How to make use of destination resolver to use queue name from #JmsListener annotation?
#Component
public class InitStRspnLstnr {
#JmsListener(destination = "${xyz.company.MQ.INITST.RSPN.FADS.Q}")
public void onMessage(Message msg) throws JMSException {
System.out.println("**********************************************"+msg);
}}
Below is my Xml pasted, I commented the container for now, since spring was trying to connect to dummy queue instead of the one in annotation..
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cxf="http://cxf.apache.org/core"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
">
<import resource="classpath*:com/xyz/svc/component.xml"/>
<mvc:annotation-driven enable-matrix-variables="true"/>
<context:component-scan base-package="com.xyz.css.rplr.initst"></context:component-scan>
<!-- WebSphere MQ Connection setup start -->
<bean id="mqIdsConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
<property name="hostName"><value>${xyz.company.MQ.HOST.NM}</value></property>
<property name="port"><value>${xyz.company.MQ.PORT}</value></property>
<property name="queueManager"><value>${xyz.company.MQ.QMNGR}</value></property>
<property name="channel"><value>${xyz.company.MQ.CHANNEL}</value></property>
<property name="CCSID"><value>819</value></property>
<property name="transportType">
<util:constant static-field="com.ibm.mq.jms.JMSC.MQJMS_TP_CLIENT_MQ_TCPIP" />
</property>
</bean>
<bean id="jmsQueueIdsConnectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory">
<ref bean="mqIdsConnectionFactory" />
</property>
</bean>
<bean id="jmsDestinationResolver"
class="org.springframework.jms.support.destination.DynamicDestinationResolver">
</bean>
<bean id="jmsQueueIdsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref bean="jmsQueueIdsConnectionFactory" />
</property>
<property name="destinationResolver">
<ref bean="jmsDestinationResolver" />
</property> <!--
<property name="defaultDestinationName">
<value>${xyz.company.MQ.INITST.Q}</value>
</property>-->
<property name="pubSubDomain">
<value>false</value>
</property>
<property name="receiveTimeout">
<value>20000</value>
</property>
</bean>
<!-- <bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory">
<ref bean="jmsQueueIdsConnectionFactory" />
</property>
<property name="destinationResolver">
<ref bean="jmsDestinationResolver" />
</property>
<property name="destinationName" value="dummyQueue"/>
<property name="concurrency" value="3-10" />
</bean> -->
<!-- WebSphere MQ Connection setup end -->
</beans>
Added working version below. defaultDestinationName is not needed in xml , if its set up in code via annotation.. ${company.project.MQ.module.Q} and other values are loaded from properties file, it can be hard coded too.
#Component
public class SampleLstner{
#JmsListener(concurrency = "1", destination = "${company.project.MQ.module.Q}", containerFactory = "jmsListenerContainerFactory")
public void onMessage(Message msg) throws JMSException {}
}
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:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cxf="http://cxf.apache.org/core"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
">
<import resource="classpath*:com/company/svc/component.xml"/>
<mvc:annotation-driven enable-matrix-variables="true"/>
<context:component-scan base-package="com.company.test.main.module"></context:component-scan>
<!-- WebSphere MQ Connection setup start -->
<bean id="mqIdsConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
<property name="hostName"><value>${company.project.MQ.HOST.NM}</value></property>
<property name="port"><value>${company.project.MQ.PORT}</value></property>
<property name="queueManager"><value>${company.project.MQ.QMNGR}</value></property>
<property name="channel"><value>${company.project.MQ.CHANNEL}</value></property>
<property name="CCSID"><value>819</value></property>
<property name="transportType"><value>1</value></property>
</bean>
<bean id="jmsQueueIdsConnectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory">
<ref bean="mqIdsConnectionFactory" />
</property>
</bean>
<bean id="jmsDestinationResolver"
class="org.springframework.jms.support.destination.DynamicDestinationResolver">
</bean>
<bean id="jmsQueueIdsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref bean="jmsQueueIdsConnectionFactory" />
</property>
<property name="destinationResolver">
<ref bean="jmsDestinationResolver" />
</property> <!--
<property name="defaultDestinationName">
<value>${company.project.MQ.module.Q}</value>
</property>-->
<property name="pubSubDomain">
<value>false</value>
</property>
<property name="receiveTimeout">
<value>20000</value>
</property>
</bean>
<bean id="jmsListenerContainerFactory" class="org.springframework.jms.config.DefaultJmsListenerContainerFactory">
<property name="connectionFactory">
<ref bean="jmsQueueIdsConnectionFactory" />
</property>
<property name="destinationResolver">
<ref bean="jmsDestinationResolver" />
</property>
<property name="concurrency" value="1" />
</bean>
<!-- WebSphere MQ Connection setup end -->

Valid XML document must have a root tag

I find stackoverflow with it, and find a post in Android, but I don't think this is fit me.
Selector for button error "Valid XML document must have a root tag"
My snapshot is below:
And my code in spring_datasource.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task" xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="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/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!--资源载入器 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean>
<!--数据连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${connection.url}" />
<property name="username" value="${connection.username}" />
<property name="password" value="${connection.password}" />
<property name="initialSize" value="${connection.initialSize}" />
<property name="minIdle" value="${connection.minIdle}" />
<property name="maxActive" value="${connection.maxActive}" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 基于注解的事物管理 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 当在web.xml 中DispatcherServlet使用<url-pattern>/</url-pattern> 映射时,
能映射静态资源(当Spring Web MVC框架没有处理请求对应的控制器时(如一些静态资源),
转交给默认的Servlet来响应静态文件,否则报404找不到资源错误,)。-->
<mvc:default-servlet-handler />
<!-- spring和hibernate整合 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">
${hibernate.show_sql}
</prop>
<prop key="hibernate.format_sql">
${hibernate.format_sql}
</prop>
<prop key="hibernate.hbm2ddl.auto">
${hibernate.hbm2ddl.auto}
</prop>
<prop key="hibernate.jdbc.batch_size">
${hibernate.jdbc.batch_size}
</prop>
<prop key="hibernate.autoReconnect">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.ldl.vision86.common.basis.entity</value>
<value>com.ldl.vision86.business.entity</value>
</list>
</property>
</bean>
<!-- 定时器配置
task:executor/#pool-size:可以指定执行线程池的初始大小、最大大小
task:executor/#queue-capacity:等待执行的任务队列的容量
task:executor/#rejection-policy:当等待队已满时的策略,分为丢弃、由任务执行器直接运行等方式
-->
<task:scheduler id="scheduler" pool-size="10" />
<task:executor id="executor" keep-alive="3600" pool-size="100-200"
queue-capacity="500" rejection-policy="CALLER_RUNS" />
<task:annotation-driven executor="executor" scheduler="scheduler" />
</beans>
I don't know where is the issue.
Your XML is no problem.
Try to Click the Create Spring facet.

How to enable logging in Spring Junit test?

How to enable logging in Spring Junit test? I think web.xml is not found when I do tests. Need to tell somehow to Junit where is web.xml with log configuration exists so logs will start working and I could bebug springs configuration errors.
My test class:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration("test-context.xml")
public class DSLCapacityTest {
private MockHttpServletRequest request;
private MockHttpServletResponse response;
#Autowired private DSLCapacity dc;
private AnnotationMethodHandlerAdapter adapter;
#Before
public void setUp() throws Exception {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
response.setOutputStreamAccessAllowed(true);
adapter = new AnnotationMethodHandlerAdapter();
}
#Test
#Repeat(2)
public void testGetIndex() throws Exception {
request.setRequestURI("/dashboard/dsl-capacity/");
request.setMethod("GET");
ModelAndView mv = adapter.handle(request, response, dc);
assertSame("Incorrect message", "dashboard/dsl-capacity/index", mv.getViewName());
}
And my test-context.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:sec="http://www.springframework.org/schema/security"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.2.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd">
<bean id="portalAuthenticator" class="uk.co.powergroup.portal.security.Authenticator">
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:ignoreUnresolvablePlaceholders="false">
<property name="location">
<value>file:/etc/portal-frontend/config.properties</value>
</property>
</bean>
<sec:authentication-manager>
<sec:authentication-provider ref='portalAuthenticator'/>
</sec:authentication-manager>
<sec:http pattern="/favicon.ico" security="none"/>
<sec:http pattern="/static/**" security="none"/>
<sec:http pattern="/logged-out" security="none"/>
<sec:http pattern="/login" security="none"/>
<sec:http pattern="/**/nologin*" security="none"/>
<sec:http auto-config="false" use-expressions="true">
<sec:csrf disabled="true"/>
<sec:intercept-url pattern="/forgot-password" access="isAnonymous()"/>
<sec:intercept-url pattern="/**" access="isAuthenticated()" />
<sec:logout logout-url="/logout" logout-success-url="/logged-out"/>
<sec:form-login login-page="/login" login-processing-url="/login-security-check" authentication-failure-url="/login" />
<sec:custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" />
<sec:session-management session-authentication-strategy-ref="sas"/>
<sec:custom-filter ref="userValidator" after="FILTER_SECURITY_INTERCEPTOR" />
</sec:http>
<sec:global-method-security pre-post-annotations="enabled" secured-annotations="enabled">
<sec:expression-handler ref="expressionHandler"/>
</sec:global-method-security>
<bean id="userValidator" class="uk.co.powergroup.portal.security.UserValidator">
</bean>
<bean id="expressionHandler" class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
<property name="permissionEvaluator" ref="myPermissionEvaluator"/>
</bean>
<bean id="myPermissionEvaluator" class="uk.co.powergroup.portal.security.PermissionChecker"/>
<bean id="concurrencyFilter" class="org.springframework.security.web.session.ConcurrentSessionFilter">
<constructor-arg name="sessionRegistry" ref="sessionRegistry" />
<constructor-arg name="expiredUrl" value="/login" />
</bean>
<bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl"/>
<bean id="sas" class="org.springframework.security.web.authentication.session.ConcurrentSessionControlAuthenticationStrategy">
<constructor-arg name="sessionRegistry" ref="sessionRegistry" />
<property name="maximumSessions" value="-1"/>
</bean>
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="PORTAL2DB"/>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://10.10.10.249:3306/portal"/>
<property name="username" value="portal"/>
<property name="password" value="twink13s"/>
</bean>
<bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.spring.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>${activemq.uri}</value>
</property>
<property name="password">
<value>${activemq.password}</value>
</property>
<property name="userName">
<value>${activemq.user}</value>
</property>
</bean>
</property>
<!-- Make this 2.5 minutes in case for some reason the JMS tester job stops (sends every minute so default of 30 seconds too short) -->
<property name="idleTimeout" value="150000"/>
</bean>
<!-- Spring JMS Template -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory"/>
<property name="receiveTimeout" value="1000"/>
<property name="sessionTransacted" value="false"/>
</bean>
<bean id="jmsMessageListener" class="uk.co.powergroup.portal.jms.ReplyHandler"/>
<jms:listener-container container-type="default" connection-factory="jmsFactory" acknowledge="auto"
concurrency="2-5">
<jms:listener destination="${activemq.replyQueue}" ref="jmsMessageListener"/>
</jms:listener-container>
<bean id="threadPool" class="uk.co.powergroup.portal.helper.ThreadPool"/>
<bean name="retryTransactionsFilterBean" class="uk.co.powergroup.portal.helper.RetryFilter"/>
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="uk.co.powergroup.portal.converters.PermissionConverter"/>
<bean class="uk.co.powergroup.portal.converters.RoleConverter"/>
<bean class="uk.co.powergroup.portal.converters.SuiteConverter"/>
<bean class="uk.co.powergroup.portal.converters.DataCentreConverter"/>
<bean class="uk.co.powergroup.portal.converters.DataCentreLocationConverter"/>
<bean class="uk.co.powergroup.portal.converters.CompanyConverter"/>
<bean class="uk.co.powergroup.portal.converters.HardwareConverter"/>
<bean class="uk.co.powergroup.portal.converters.RackConverter"/>
<bean class="uk.co.powergroup.portal.converters.EquipmentConverter"/>
<bean class="uk.co.powergroup.portal.converters.PowerDistributionUnitTypeConverter"/>
<bean class="uk.co.powergroup.portal.converters.TimeZoneDOConverter"/>
<bean class="uk.co.powergroup.portal.converters.DslLnsBearerTypeConverter"/>
<bean class="uk.co.powergroup.portal.converters.DslLnsLocationConverter"/>
<bean class="uk.co.powergroup.portal.converters.DslLnsOrganisationConverter"/>
<bean class="uk.co.powergroup.portal.converters.DslLnsConverter"/>
<bean class="uk.co.powergroup.portal.converters.BigDecimalConverter"/>
<bean class="uk.co.powergroup.portal.converters.PstnNumberDDIConverter"/>
</set>
</property>
</bean>
<context:component-scan base-package="uk.co.powergroup.portal.dao.impl"
name-generator="uk.co.powergroup.portal.misc.BeanNameGeneratorImpl"/>
You can do configuration for log4j.properties in your test case file so it will create log for you.
#BeforeClass
public static void init() {
PropertyConfigurator.configure("src/configuration/log4j.properties");
}
Java web containers mainly used web.xml file and also web.xml file not belongs to Spring context configuration.

Spring+Maven Property File not loading

I am trying to access a property file containing db configurations in a Maven + Spring project.
I get following error:
Cannot load JDBC driver class '${db_driver}'
My Property file is placed in src/resources folder.
Below is the tag to load property files:
<bean id="dbPropertyReader"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="locations">
<value>classpath:${appenv.deployment}.properties</value>
</property>
</bean>
Following tag uses properties loaded:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="url" value="${db_url}" />
<property name="driverClassName" value="${db_driver}" />
<property name="username" value="${db_username}" />
<property name="password" value="${db_password}" />
</bean>
Below are contents of properties file:
#JDBC Properties
db_driver=com.mysql.jdbc.Driver
db_url=jdbc\:mysql\://hostname\:3306/xxx_dbxxx?useUnicode\=true
db_username=abcdefgh
db_password=ijklmnopq
db_removeabadoned=true
db_initialsize=1
db_maxactive=2
${appenv.deployment} is a VMArgument set as follows:
-Dappenv.deployment=development
I have checked, this value is getting populated properly.
I am getting following line in logs:
Found key 'appenv.deployment' in [systemProperties] with type [String] and value 'development'
Then after this I am also getting following:
Loading properties file from class path resource [development.properties]
But some how, the values are not getting loaded.
Spring-Datasource.xml
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.5.xsd">
<bean id="dbPropertyReader"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="locations">
<value>classpath:${appenv.deployment}.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="url" value="${db_url}" />
<property name="driverClassName" value="${db_driver}" />
<property name="username" value="${db_username}" />
<property name="password" value="${db_password}" />
<property name="initialSize" value="${db_initialsize}" />
<property name="maxActive" value="${db_maxactive}" />
</bean>
<bean id="firstConfigDataFromDB" class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg type="javax.sql.DataSource" ref="dataSource" />
<constructor-arg index="1" value="tablename1" />
<constructor-arg index="2" value="propertyname2" />
<constructor-arg index="3" value="propertyvalue2" />
</bean>
<bean id="firstConfigDataFromDBFactory"
class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
<constructor-arg ref="firstConfigDataFromDB" />
</bean>
<!-- DB Properties Initialization -->
<bean id="firstConfigurationPlaceHolder"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2" />
<property name="ignoreUnresolvablePlaceholders" value="false"/>
<property name="properties" ref="firstConfigDataFromDBFactory" />
</bean>
<bean id="secondConfigurationFromDB"
class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg type="javax.sql.DataSource" ref="dataSource" />
<constructor-arg index="1" value="tablename2" />
<constructor-arg index="2" value="propertyname2" />
<constructor-arg index="3" value="propertyvalue2" />
</bean>
<bean id="secondConfigurationFromDBFactory"
class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
<constructor-arg ref="secondConfigurationFromDB" />
</bean>
<!--
Error Map Initialization
Subtype of org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
-->
<bean id="secondConfigurationPlaceHolder"
class="com.application.SecondConfigurationPlaceHolder">
<property name="order" value="3" />
<property name="ignoreUnresolvablePlaceholders" value="false"/>
<property name="properties" ref="secondConfigurationFromDBFactory" />
</bean>
</beans>
Generic.xml
<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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
>
<!-- Enable annotation scanning -->
<context:annotation-config/>
<!-- Initialise connection to Database -->
<import resource="Spring-Datasource.xml"/>
<!-- Initialize mail connection -->
<import resource="Spring-Mail.xml"/>
<!-- Inject database connection to DAO -->
<import resource="Spring-DAO.xml"/>
<!-- Other Beans Below -->
</beans>
applicationContext.xml
<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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
>
<import resource="generic.xml" />
<bean id="applicationBean" class="com.application.ApplicationBean" scope="singleton" >
<property .. />
<property .. />
</bean>
</beans>
I am loading applicationContext.xml with following statement in code:
`appContext = new ClassPathXmlApplicationContext("applicationContext.xml");`
applicationContext.xml imports generic.xml.
generic.xml imports Spring-DataSource.xml.
have you added this to your application context file
<context:property-placeholder location="Path for .properties file"/>
add this line before the beans

Spring project how do I access JBoss JNDI Datasources

Below is my current database.xml file for my Spring Project. Can someone please tell me what would have to be changed so I can use a JBoss JNDI datasource in it.. I want to do this so I don't need the config files with the database user and password and url in it.
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<!--
Last changed: $LastChangedDate: 2012-11-19 08:53:13 -0500 (Mon, 19 Nov 2012) $
#author $Author: johnathan.smith#uftwf.org $
#version $Revision: 829 $
-->
<context:property-placeholder location="classpath:app.properties" />
<context:component-scan base-package="org.uftwf" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- these are C3P0 properties -->
<property name="acquireIncrement" value="${database.c3p0.acquireIncrement}" />
<property name="minPoolSize" value="${database.c3p0.minPoolSize}" />
<property name="maxPoolSize" value="${database.c3p0.maxPoolSize}" />
<property name="maxIdleTime" value="${database.c3p0.maxIdleTime}" />
<property name="maxIdleTimeExcessConnections" value="${database.c3p0.maxIdleTimeExcessConnections}" />
<property name="numHelperThreads" value="${database.c3p0.numHelperThreads}" />
<property name="unreturnedConnectionTimeout" value="${database.c3p0.unreturnedConnectionTimeout}" />
<property name="idleConnectionTestPeriod" value="300" />
<property name="driverClass" value="${database.driver}" />
<property name="jdbcUrl" value="${database.url}" />
<property name="user" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>org.uftwf.enrollment.model.Contact</value>
<value>org.uftwf.enrollment.model.Enrollment</value>
<value>org.uftwf.enrollment.model.Member</value>
<value>org.uftwf.enrollment.model.Profile</value>
<value>org.uftwf.enrollment.model.School</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
<prop key="format_sql">${format_sql}</prop>
</props>
</property>
</bean>
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
I assume you can configure DataSource in JBoss. Notice that you have to define its JNDI name in application server configuration. Once you have the name, simply replace your dataSource bean with:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/some-name"/>
</bean>
or shortcut:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/some-name" expected-type="javax.sql.DataSource" />

Categories