Inject JAXBContext into spring - spring

I am trying to inject a JAXBContext into spring application context, by:
<bean id="jaxbContext" class="javax.xml.bind.JAXBContext" factory-method="newInstance">
<constructor-arg type="java.lang.Class" value="com.package.MyClassName"/>
</bean>
It throws an exception:
No matching factory method found: factory method 'newInstance'
And I also try :
<bean id="jaxbContext" class="javax.xml.bind.JAXBContext" factory-method="newInstance">
<constructor-arg type="java.lang.String" value="com.package"/>
</bean>
And It throws an an exception:
javax.xml.bind.JAXBException: "com.package" doesnt contain ObjectFactory.class or jaxb.index
I did put a jaxb.index file inside the package "com.package" and has a single line "MyClassName" in the file.

#Tomasz's answer is the solution I'd recommend, but if you want to stick with JAXBContext, then the reason your first example failed is that the static getInstance() method on JAXBContext doesn't take a single Class argument, it takes a vararg list of them. So you need to inject a list, not a single class:
<bean id="jaxbContext" class="javax.xml.bind.JAXBContext" factory-method="newInstance">
<constructor-arg value-type="java.lang.Class">
<list>
<value>com.package.MyClassName</value>
</list>
</constructor-arg>
</bean>

Have you tried Spring OXM? The last line is important, namespaces are for reference only:
<?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:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-1.5.xsd">
<oxm:jaxb2-marshaller id="marshaller" contextPath="com.package"/>
</beans>
See 8.4. XML Schema-based Configuration. Yu'll also need spring-oxm on your classpath.

this will resolve the problem for jaxb.index file or ObjectFactory problem in spring env. provide the value of the package where the classes are their which generate the xml
enter code here`
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="packagesToScan" >
<value>com.adarsh.spring.integration.entities</value>
</property>
</bean>`

Related

deployerConfigContext.xml has no effect

I tried to get a CAS-server up and running using https://github.com/apereo/cas-overlay-template for a side project (I'm a student), but I have never used maven or spring before.
I'm trying to hook my own IPersonAttributeDao into the CAS-server. However, when I put my deployerConfigContext.xml under src/main/webapp/WEB-INF/, nothing actually changed when I repackaged (using the build script) and deployed to tomcat8. (Yes, I did restart tomcat).
Below the contents of my deployerConfigContent.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<bean id="authenticationManager" class="org.jasig.cas.authentication.AuthenticationManagerImpl">
<property name="credentialsToPrincipalResolvers">
<list>
<bean id="primaryPrincipalResolver"
class="org.jasig.cas.authentication.principal.PersonDirectoryPrincipalResolver">
<property name="attributeRepository" ref="attributeRepository"/>
</bean>
<bean class="org.jasig.cas.authentication.principal.UsernamePasswordCredentialsToPrincipalResolver">
<property name="attributeRepository" ref="attributeRepository"/>
</bean>
<bean class="org.jasig.cas.authentication.principal.HttpBasedServiceCredentialsToPrincipalResolver">
<property name="attributeRepository" ref="attributeRepository"/>
</bean>
</list>
</property>
<property name="authenticationHandlers">
<list>
<bean id="primaryAuthenticationHandler"
class="org.jasig.cas.authentication.AcceptUsersAuthenticationHandler">
<property name="users">
<map>
<!-- Login stays the default casuser:Mellon, no idea why -->
<entry key="test" value="1234"/>
</map>
</property>
</bean>
<!-- DO NOT EVER PUT THIS BEAN IN PRODUCTION!!! -->
<bean class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler"/>
</list>
</property>
</bean>
<!-- <bean id="attributeRepository" class="class.i.am.trying.to.hook.in">
</bean>-->
<!-- This doesn't seem to work -->
<bean id="attributeRepository"
class="org.jasig.services.persondir.support.StubPersonAttributeDao">
<property name="backingMap">
<map>
<entry key="uid" value="uid" />
<entry key="eduPersonAffiliation" value="eduPersonAffiliation" />
<entry key="groupMembership" value="groupMembership" />
</map>
</property>
</bean>
</beans>
What am I missing?
UPDATE:
I reread the documentation (https://apereo.github.io/cas/5.0.x/), and found that deployerConfigContext.xml was supposed to be in resources, and not in webapp/WEB-INF as I found on the internet. I moved it, and now I'm finally getting errors in the log (which means the file is being read).
Error below:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.jasig.cas.authentication.PolicyBasedAuthenticationManager] for bean with name 'authenticationManager' defined in class path resource [deployerConfigContext.xml]
I found a reference to PolicyBasedAuthenticationManager in the docs, but trying that gave the same error (with the other classname ofcourse).
Bean classes of enabled beans must be deployed in bean archives.
A library jar, EJB jar, application client jar or rar archive is a
bean archive if it has a file named beans.xml in the META-INF
directory. The WEB-INF/classes directory of a war is a bean archive if
there is a file named beans.xml in the WEB-INF directory of the war. A
directory in the JVM classpath is a bean archive if it has a file
named beans.xml in the META-INF directory.
One suggestion, if you are using Spring, you can also set #Annotation to define beans.

Spring Beans getting initialised twice for Entity Manager

I am having a bean defined as below
<bean id="batchManagementService" class="com.amdocs.dc.sprint.batch.service.BatchManagementServiceImpl" autowire-candidate="false">
<property name="repository" ref="batchPersistenceRepository" />
<property name="timeService" ref="batchTimeService" />
<property name="validator" ref="batchValidator" />
</bean>
I am refering this bean in two places in different modules.
However it is getting initialised with two differnt values.
with the JDK proxy which performs correct DB transactions
with the implementor class directly "BatchManagementServiceImpl" as mentioned above.
IN the second case the Entity manager is returning null causing all DB transactions to fail.
The implementor class contains transactional methods.
Any help on this will be appreciated..
Not quite clear from the description for me, but it sounds like your applicationContext is misconfigured within the modules.
Maybe you try an approach like:
MODULE_A:
-applicationContextA.xml
MODULE_B (which should use MODULE_A's beans )
-applicationContextB.xml
MODULE_C (which should use MODULE_B and MODULE_A )
-applicationContextC.xml
The WRONG approach is:
applicationContextA.xml:
<beans xmlns="http://www.springframework.org/schema/beans">
...
<bean id="batchManagementService"...>
applicationContextB.xml:
<beans xmlns="http://www.springframework.org/schema/beans">
<import resource="applicationContextA.xml"/>
<bean id=anotherBean...
applicationContextC.xml:
<beans xmlns="http://www.springframework.org/schema/beans">
<import resource="applicationContextB.xml"/>
<bean id=anotherBean...
In this case, when applicationContextC is created, batchManagementService will be instantiated twice.
Try this one:
applicationContextA.xml:
<beans xmlns="http://www.springframework.org/schema/beans">
...
<bean id="batchManagementService"...>
applicationContextB.xml:
<beans xmlns="http://www.springframework.org/schema/beans">
<!--No import here! -->
<!--<import resource="applicationContextA.xml"/>-->
<bean id=anotherBean...
applicationContextC.xml:
<beans xmlns="http://www.springframework.org/schema/beans">
<!-- No import here -->
<!--<import resource="applicationContextB.xml"/>-->
<bean id=anotherBean...
applicationContextFull.xml
<beans xmlns="http://www.springframework.org/schema/beans">
<import resource="applicationContextA.xml"/>
<import resource="applicationContextB.xml"/>
<import resource="applicationContextC.xml"/>
</beans>
, and try to boot application context using applicationContextFull.xml.
Hope this helps.

How can I use Spring Batch Admin with Spring 3.2 and #Schedule annotation?

I integrated Spring Batch Admin into my app, which uses Spring 3.2.
Now I try to annotate a method with #Scheduled and activate this with <task:annotation-driven/>.
When I launch the webapp I get this exception:
Caused by: java.lang.IllegalStateException: #Scheduled method 'removeInactiveExecutions'
found on bean target class 'SimpleJobService', but not found in any interface(s) for bean
JDK proxy. Either pull the method up to an interface or switch to subclass (CGLIB) proxies
by setting proxy-target-class/proxyTargetClass attribute to 'true'
The SimpleJobService of Spring Batch Admin uses this annotation on a method.
In Spring 3.2. it seems, that there is no need to put cglib into the classpath and spring-asm is obsolete, too. I excluded the spring-asm dependency from spring-batch-integration.
Where can I set proxy-target-class=true (I already tried it on <tx:annotation-config> and <aop:config>?
How can I use #Scheduled in my application?
Add execution-context.xml in META-INF\spring\batch\override, set proxy of SimpleJobServiceFactoryBean to target class
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" 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.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">
<!-- Original jobRepository missing read ${batch.isolationlevel} -->
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"
p:dataSource-ref="dataSource" p:transactionManager-ref="transactionManager" p:isolationLevelForCreate = "${batch.isolationlevel}"/>
<!-- Original jobService conflicted with #EnableScheduling -->
<bean id="jobService"
class="org.springframework.batch.admin.service.SimpleJobServiceFactoryBean">
<aop:scoped-proxy proxy-target-class="true" />
<property name="jobRepository" ref="jobRepository" />
<property name="jobLauncher" ref="jobLauncher" />
<property name="jobLocator" ref="jobRegistry" />
<property name="dataSource" ref="dataSource" />
<property name="jobExplorer" ref="jobExplorer" />
<property name="transactionManager" ref="transactionManager" />
</bean>
</beans>

Spring property reference is not working

<bean name="readerService" class="com.mayank.example1.ReaderService"/>
<property name="reader" ref="fileReader" />
</bean>
<bean name="fileReader" class="com.mayank.example1.FileReader">
<constructor-arg value="resources/myfile.txt" />
</bean>
Reder service take reader as argument in its constructor
Reader is Interface.
FileReader is class that implement Reader
In spring It is not taking property reader and throwing exception:
Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 15 in XML document from class path resource [reader-beans.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'property'. One of '{"http://www.springframework.org/schema/beans":import, "http://www.springframework.org/schema/beans":alias, "http://www.springframework.org/schema/beans":bean, WC[##other:"http://www.springframework.org/schema/beans"]}' is expected
It looks like you are closing the bean tag too early (note the /> at the end, shouldn't this be just >?):
<bean name="readerService" class="com.mayank.example1.ReaderService"/>
<property name="reader" ref="fileReader" />
</bean>
Make sure you have the required xml namespaces bean and context provided at the top of your configuration file. My example uses version 3.1 of Spring you may need to adjust for the version of Spring you are using.
Also notice the adjustment to the readerService bean tag which was being closed too early.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-3.1.xsd">
<bean name="readerService" class="com.mayank.example1.ReaderService">
<property name="reader" ref="fileReader" />
</bean>
<bean name="fileReader" class="com.mayank.example1.FileReader">
<constructor-arg value="resources/myfile.txt" />
</bean>
</beans>

spring mvc i18n: If key not in properties file, how to set default locale?

suppose if i don't have a key in my one of the properties file i get a exception like :
javax.servlet.ServletException: javax.servlet.jsp.JspTagException: No message found under code 'label.cancel' for locale 'en'.
suppose if it is not available in my messages_ch_CN.properties is there any way that if it is not present in that file it should check in messages_en_En file.
or is there any work around any one has implemented.
For example you have: 2 language
messages_ch_CN.properties /*in the property file lang=Chinese*/
messages_en_EN.properties /*in the property file lang=English*/
messages.properties /*in the property file lang=English default*/
messages.properties this is default property, which always contains every key used throughout your application.
and NAME-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.example.controller"/>
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:com/example/i18n/messages"/>
<property name="fallbackToSystemLocale" value="false"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"/>
</bean>
</mvc:interceptors>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
</beans>
<property name="fallbackToSystemLocale" value="false"/> - says that, if you don't have property messages_ch_CN.properties or messages_en_EN.properties or in one of those property there is no necessary key, for example: title, spring will use messages.properties as default
if <property name="fallbackToSystemLocale" value="true"/> - spring uses user locale of deployment environment, but it not good because user's deployment environment, can different from the languages that we have provided. If user's locale different from the languages that we have provided, spring uses messages.properties.
One workaround would be to subclass the messageSource you're using (ie ReloadableResourceBundleMessageSource) and override it's resolveCode() so that:
it looks for a message for specified code and locale (by calling super.resolveCode(code, locale)).
if it doesn't find it, then it looks for one with default locale (by calling super.resolveCode(code, defaultLocale)).
Then use that newly created class as messageSource.
Always provide a default messages file (without locale specification), which always contains every key used throughout your application. It is not possible to look automatically for another locale than the chosen one without subclassing one of the ResourceBundleimplementations.

Resources