Reading configurations from xml file in spring mvc - java

I am developing web application using spring mvc 4 REST API, where I have web.xml file and spring-servlet.xml file.
I am using host/ip and port numbers in between my code, instead I should config in xml file and read it in controller.
That shouldn't overload my application. It shouldn't break the MVC structure/policies.

One of the solutions is to put the configurations in the properties files, and then adopted by the spring xml files. Below is an example:
###Redis Settings###
redis.pool.maxActive=1024
redis.pool.maxIdle=200
redis.pool.maxWait=1000
redis.pool.testOnBorrow=true
redis.ip=redis-server
redis.port=6379
Then:
<!-- Configuration for Properties -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/config/redis.properties</value>
</list>
</property>
</bean>
<!-- Configuration for Redis Client -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxActive" value="${redis.pool.maxActive}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="maxWait" value="${redis.pool.maxWait}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.ip}" />
<property name="port" value="${redis.port}" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
<bean class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory">
</bean>

Related

Hibernate trying to find hibernate.cfg.xml when configured by spring

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>

ActiveMQ Data persistent Issue in Java Spring

I have an application which uses embedded activeMQ 5.11. At the start of the application it creates activemq-data\producerBroker\KahaDB folder at the class path location. I do want to change the location but spring.xml doesn't take a location.
Spring.xml as given,
<bean id="producerBroker" class="org.apache.activemq.broker.SslBrokerService">
<property name="brokerName" value="producerBroker" />
<property name="persistent" value="true" />
<property name="persistenceAdapter" ref="persistenceAdapter"/>
<property name="transportConnectors">
<list>
<bean class="org.apache.activemq.broker.TransportConnector">
<property name="name" value="xxx"></property>
<property name="uri" value="${transportConnectorURIs}"></property>
</bean>
</list>
</property>
<property name="jmsBridgeConnectors">
<list>
<bean class="org.apache.activemq.network.jms.JmsQueueConnector">
<property name="outboundQueueConnectionFactory">
<bean class="org.apache.activemq.ActiveMQSslConnectionFactory">
<property name="brokerURL" value="${brokerURL}" />
<property name="userName" value="${username}" />
<property name="password" value="${password}" />
<property name="trustStore" value="${trust.store.path}" />
<property name="trustStorePassword" value="${trust.store.password}" />
<!-- <property name="keyStore" value="${key.store.path}"/> -->
<!-- <property name="keyStorePassword" value="${key.store.password}"/> -->
</bean>
</property>
<property name="outboundQueueBridges">
<list>
<bean class="org.apache.activemq.network.jms.OutboundQueueBridge">
<constructor-arg value="${screenshotQueueName}" />
</bean>
<bean class="org.apache.activemq.network.jms.OutboundQueueBridge">
<constructor-arg value="${resultXmlQueueName}" />
</bean>
</list>
</property>
</bean>
</list>
</property>
</bean>
<bean id="persistenceAdapter" class="org.apache.activemq.store.kahadaptor.KahaPersistenceAdapter">
<property name="directory" value="E:\test"/>
Current issue is it throws an error as "exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.activemq.store.kahadaptor.KahaPersistenceA
apter] for bean with name 'kahaPersistenceAdapter' defined in class path resource [spring/resultupload/resultupload.xml]; nested exception is java.la
g.ClassNotFoundException: org.apache.activemq.store.kahadaptor.KahaPersistenceAdapter"
Anyone has experience in the directory change in activeMQ 5.11 in java spring?
The destination of the persistence location must be defined at the broker level.
The kahaPersistenceAdapter (which was file based) was removed with version 5.9. You should use the kahaDB.
kahaDB - uses KahaDB an embedded lightweight non-relational database
<broker brokerName="broker" persistent="true" useShutdownHook="false">
<transportConnectors>
<transportConnector uri="tcp://localhost:61616"/>
</transportConnectors>
<persistenceAdapter>
<kahaDB directory="e:/temp" ... />
</persistenceAdapter>
</broker>
all valid attributes: http://activemq.apache.org/schema/core/activemq-core-5.11.0-schema.html#kahaDB

Transactions in Apache Jackrabbit and Spring

I want to run transactions on my Spring webapp which uses Apache Jackrabbit repository. JackRabbit then uses relational database (MySQL/PgSQL) for text data. Binary data are stored to FileSystem.
So far I have this functional configuration of Jackrabbit beans:
<bean id="sessionFactory" class="com.example.MyJcrSessionFactory">
<constructor-arg index="0" ref="repository"/>
<constructor-arg index="1" ref="jcrCredentials"/>
</bean>
<bean id="repository" class="org.apache.jackrabbit.core.RepositoryImpl">
<constructor-arg index="0" ref="config" />
</bean>
<bean id="config" class="org.apache.jackrabbit.core.config.RepositoryConfig" factory-method="create">
<constructor-arg index="0" type="java.io.InputStream" value="classpath:jackrabbit/repository.xml"/>
<constructor-arg index="1" type="java.lang.String" value="/tmp/repository" />
</bean>
<bean id="jcrCredentials" class="javax.jcr.SimpleCredentials">
<constructor-arg index="0" type="java.lang.String" value="..." />
<constructor-arg index="1" type="char[]" value="..." />
</bean>
And rest of Jackrabbit configuration in repository.xml file.
What do I have to do to successfully run transactions on JackRabbit repository? Which technology am I supposed to use?
I'm running Spring 3.1, Jackrabbit 2.3.3 on Tomcat/Glassfish. And I don't want to use obsolete spring modules.
We're using a similar setup (Jackrabbit, Spring, Tomcat) with Jencks as a JCA provider.
Here's an example config:
<bean id="txManagerJencks" class="org.jencks.factory.TransactionManagerFactoryBean" />
<bean id="pooledConnectionManagerJcr" class="org.jencks.factory.ConnectionManagerFactoryBean">
<property name="transactionManager">
<ref local="txManagerJencks" />
</property>
<property name="transaction" value="xa" />
<property name="poolMinSize" value="1"/>
<property name="poolMaxSize" value="5"/>
<property name="connectionMaxIdleMinutes" value="5" />
</bean>
<bean id="repositoryManagedConnectionFactory" class="org.apache.jackrabbit.jca.JCAManagedConnectionFactory" destroy-method="finalize">
<property name="homeDir" value="${jackrabbit.homeDir}/jackrabbit" />
<property name="configFile" value="classpath:repository.xml" />
</bean>
<bean id="repository"
class="org.springframework.jca.support.LocalConnectionFactoryBean" >
<property name="managedConnectionFactory">
<ref local="repositoryManagedConnectionFactory" />
</property>
<property name="connectionManager">
<ref local="pooledConnectionManagerJcr" />
</property>
</bean>

how do i get values from a properties file in quartz scheduler?

I'm using spring 2.0.6 and quartz 1.5.2 in a java based webapp. Want to know how to configure my applicationContext-quartz.xml such that i can read values from a properties file. i.e. I would like to have my file read
<property name="imageFolder" value="${config.imageFolder}" />
<property name="rawImageFolder" value="${config.rawImageFolder}" />
<property name="imageOutputFolder" value="${config.imageOutputFolder}" />
instead of
<property name="imageFolder" value="/path/to/dir1" />
<property name="rawImageFolder" value="/path/to/dir2" />
<property name="imageOutputFolder" value="/path/to/dir3" />
Use the PropertyPlaceholderConfigurer mechanism:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<!-- change this to your property location -->
<value>classpath:quartz.properties</value>
</property>
</bean>

spring jmx authentication

How can i turn on authentication using jmx on spring web app ?
Please take a look here for a solution that is almost working for me:
http://forum.springsource.org/showthread.php?t=73677
The only unresolved issue is how to make JMX client use the same connection to server when authenticating and when doing the secure operation.
<bean id="serverConnector" class="org.springframework.jmx.support.ConnectorServerFactoryBean">
<property name="objectName" value="connector:name=rmi" />
<property name="serviceUrl" value="service:jmx:rmi:///jndi/rmi://:9875/jmxrmi" />
<property name="environment">
<map>
<entry key="jmx.remote.x.password.file" value="C:\Java\jdk1.7.0_79\jre\lib\management\jmxremote.password" />
<entry key="jmx.remote.x.access.file" value="C:\Java\jdk1.7.0_79\jre\lib\management\jmxremote.access" />
</map>
</property>
</bean>
SpringConfig
<bean id="annotationTestMBean" class="com.greenline.appservice.web.bean.AnnotationTestMBean"/>
<!-- Spring JMX 配置 begin -->
<bean id="registry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
<property name="port" value="9875" />
<property name="alwaysCreate" value="true" />
</bean>
<bean id="serverConnector" class="org.springframework.jmx.support.ConnectorServerFactoryBean">
<property name="objectName" value="connector:name=rmi" />
<property name="serviceUrl" value="service:jmx:rmi:///jndi/rmi://192.168.214.63:9875/myconnector" />
<!--jmxmp协议
<property name="objectName" value="connector:name=jmxmp" />
<property name="serviceUrl" value="service:jmx:jmxmp://192.168.214.63:9875" />
-->
<property name="environment">
<!-- the following is only valid when the sun jmx implementation is used -->
<map>
<entry key="jmx.remote.x.password.file" value="C:\Java\jdk1.7.0_79\jre\lib\management\jmxremote.password" />
<entry key="jmx.remote.x.access.file" value="C:\Java\jdk1.7.0_79\jre\lib\management\jmxremote.access" />
</map>
</property>
</bean>
<context:mbean-export registration="replaceExisting"/>
<!-- Spring JMX 配置 end-->
you can see access&password file
JDK_Path\jre\lib\management\jmxremote.password
JDK_Path\jre\lib\management\jmxremote.access
JMX and Spring Part1-3
http://www.javacodegeeks.com/2012/07/jmx-and-spring-part-1.html
oracle-Remote Management Applications (jmx)
http://docs.oracle.com/cd/E19698-01/816-7609/6mdjrf861/index.html

Categories