Spring configuration and transaction - java

I played with this numerous times, changing the default target namespace, still it will complain the context undeclared element: Multiple annotations found at this line: - cvc-elt.1: Cannot find the declaration of element 'context:annotation-driven'.
------Configuration File----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:annotation-driven/>
<tx:annotation-driven/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>/WEB-INF/classes/hibernate.cfg.xml</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
...
Also, I added two annotation-driven since the transaction is not working, does tx:annotation-driven use the transactionManager which has it's own session from hibernate?
I used my derived sessionFactory using hibernate3, so only need a transactional before the method to run update queries.
Thanks!

Try to add <?xml version="1.0" encoding="UTF-8"?> and better use java-based config

Related

How do I configure the date format the server returns using CXF JAX-RS and Jackson 2 in XML?

This took me a lot of effort to figure out so I'm going to answer the question below. This answer doesn't use annotations and does not require creating additional classes.
You put this in your spring xml context configuration:
<?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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
...
<bean id="jacksonMapper" class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyyMMdd'T'HHmmss.SSSZ"/>
</bean>
</property>
</bean>
<bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"
p:mapper-ref="jacksonMapper"/>
...
<jaxrs:providers>
<ref bean="jsonProvider"></ref>
</jaxrs:providers>
</jaxrs:server>

Turn on Autowire in Spring

I check my application without
<context:spring-configured/>
and the #Autowire working properly. I don't know how the container can auto inject without
<context:spring-configured/>
Here is my application-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:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-
mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.somepackage" />
<tx:annotation-driven transaction-manager="transactionManager" />
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.tiles3.TilesView</value>
</property>
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.somepackage.converter.CategoryConverter"/>
</set>
</property>
</bean>
</beans>
Autowiring is working because of
context:component-scan
Check out this javadoc
You need to add context schema into your Configuration XML to use #Autowired annotation, like below :
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
For more check the link below :
3.11. Annotation-based configuration
Also, check the difference between context:annotation-config vs context:component-scan , You were getting #Autowiring because of the context:component-scan being used in your context schema.

How to have auto suggest / auto complete for the spring config file?

<?xml version="1.0" encoding="UTF-8"?>
<bean id="reservation" class="com.youtube.Reservation" init-method="init" destroy-method="destroy">
<property name="train" ref="trainobject" />
<property name="bus" ref="busobject" />
</bean>
<bean id="busobject" class="com.youtube.Mode">
<property name="name" value="KPN" />
</bean>
<bean id="trainobject" class="com.youtube.Mode">
<property name="name" value="Shatabdi" />
</bean>
My XML looks like above. I need auto suggest for creating this spring configuration. Can anyone help?
For starters, proper beans tag would be useful.
<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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
Please refer to Spring documentation regarding this topic.

PropertyPlaceholderConfigurer values are not getting resolved for beans in imported context

I have PropertyPlaceholderConfigurer configured in my web application's Spring context which in turn imports few other contexts which are in JARs that expect certain properties to be configured. But for some reason the PropertyPlaceholderConfigurer values ware not available to them and I get error on start up:
java.net.URISyntaxException: Illegalcharacter in path at index 1: ${dax.svc1.endpoint}
Here is what my application context looks like:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" 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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" name="mhpVariables">
<property name="locations">
<list>
<value>classpath:appconfig.properties</value>
</list>
</property>
</bean>
<import resource="classpath:com.test.svc1/childContext.xml"/>
<import resource="classpath:com.test.svc2/child2Context.xml"/>
</beans>
Child context is like this :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
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.0.xsd">
<!-- connection info -->
<bean class="com.test.java.framework.dataaccess.ServiceConnectionInfo" id="ConnectionInfo">
<property name="defaultUri" value="${dax.svc1.endpoint}"/>
<property name="maxTotalConnections" value="500"/>
<property name="maxConnectionsPerHost" value="50"/>
<property name="readTimeout" value="3000"/>
<property name="ConnectionTimeout" value="1000"/>
</bean>
</beans>
I verified the property file is on the classpath and has the property dax.svc1.endpoint. What am I missing here?
You have to put a placeholder bean inside each of the imports; that's the only way I could get it to work as I have a similar setup to what you describe. I also removed the id from the bean to prevent any id conflicts in the container.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="WEB-INF/myconfig.properties" />
</bean>
I will assume that you have all the xml directives... check the encoding of your properties file (also your XML)

XSD Client in Spring

I have an XSD document that I need to communicate with an endpoint (client side only) - is there this functionality built into spring? I have been using JAXB, but was wondering if spring has some sort of wrapper. Thanks.
Finally figured it out - its a matter of changing the MessageFactory on the WebServiceTemplate to use POX vs SOAP. Hope this helps someone!
References:
http://static.springsource.org/spring-ws/sites/1.5/reference/html/client.html http://static.springsource.org/spring-ws/sites/1.5/reference/html/oxm.html
<?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:util="http://www.springframework.org/schema/util"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-1.5.xsd"
default-autowire="no" default-init-method="init" default-destroy-method="destroy">
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="com.cable.comcast.neto.nse.sams.pox"/>
</bean>
<bean id="messageFactory" class="org.springframework.ws.pox.dom.DomPoxMessageFactory"/>
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<constructor-arg ref="messageFactory"/>
<property name="messageSender">
<bean class="org.springframework.ws.transport.http.CommonsHttpMessageSender" />
</property>
<property name="defaultUri" value="http://sams-web.cable.comcast.com/ttsgateway/Inbound"/>
<property name="marshaller" ref="jaxb2Marshaller" />
<property name="unmarshaller" ref="jaxb2Marshaller" />
</bean>
</beans>

Categories

Resources