Spring #Async. XML configuration - java

I need to configure one asynchronous method in my project. I trying to it it this way:
This is my spring config:
<?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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
"
>
<context:annotation-config/>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<task:executor id="asyncExecutor" pool-size="5" />
<task:annotation-driven executor="asyncExecutor" />
and the method:
#Async
public Future<Boolean> async() throws InterruptedException {
TimeUnit.SECONDS.sleep(20);
return new AsyncResult<>(true);
}
When I run test, I get an Exception:
nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Only one AsyncAnnotationBeanPostProcessor may exist within the context.
If I delete <task:annotation-driven executor="asyncExecutor" /> then method doesn't start asynchronously.
What wrong with my config?

Related

Can I define SpringDataJpa repository without <context:annotation-config/>

I would like to know why I am getting this error Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: EntityPathResolver must not be null! when I am not having <context:annotation-config/>. From my understanding the <context:annotation-config/> should only enable annotations like #Autowire, #Resource etc.
This 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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/data/jpa
https://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<import resource="data-source-context.xml"/>
<!-- <context:annotation-config/>-->
<jpa:repositories
base-package="com.znamenacek.repository.springdatajpa"
entity-manager-factory-ref="emf"
transaction-manager-ref="transactionManager"
/>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="emf"
/>
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:jpaVendorAdapter-ref="jpaVendorAdapter"
p:packagesToScan="com.znamenacek.entity"
p:jpaProperties-ref="hibernateProperties"
/>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
<util:properties id="hibernateProperties"
location="hibernate.properties"
/>
</beans>
Could you please explain me where is the problem and how can I get rid off the <context:annotation-config/>?

Spring 3.0 load proprrties file and set #Scheduled cron

I want to execute a method every hr which will do my job.
I am using SPRING 3.0 (please keep in mind) schedule cron below is my code. But it is giving the below error.
Error : "Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'scheduling.job.cron'"
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:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util"
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/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd ">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:test.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
Java Class
#Scheduled(cron = "${scheduling.job.cron}")
public void testScheule()
{
logger.info("Schedule Call" + new Date());
}
Properties file (which is present in src/main/resource/test.properties) contain below line
scheduling.job.cron=0 0/1 * * * ?
Can somebody please help me to get out of this error and work sucessfully.
Thanks in advance.
Be sure you have correctly configured your application.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:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util"
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.xsd
http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<task:annotation-driven />
<util:properties id="testProps" location="test.properties" />
<context:property-placeholder properties-ref="testProps" />
<bean id="yourBeanClassHere" class="com.howtodoinjava.service.YourBeanClassImplHere"></bean>
</beans>
Have a look at this link for a better understanding http://howtodoinjava.com/spring/spring-core/4-ways-to-schedule-tasks-in-spring-3-scheduled-example/
What you are trying to achieve is not possible in Spring 3.0.
This feature is available ,starting from spring 3.0.1.
Even if you are able to resolve the property scheduling.job.cron from test.properties file, you will receive below exception:-
java.lang.IllegalArgumentException: cron expression must consist of 6 fields (found 1 in ${scheduling.job.cron})
In Spring 3.0, only possible way to do is:-
#Service (value="testService")
public class TestService {
// ..
public void testScheule() { .. }
}
Now you can define in xml like below:-
<context:component-scan base-package="com.some.package" />
<task:annotation-driven />
<util:properties id="applicationProps" location="test.properties" />
<task:scheduled-tasks>
<task:scheduled ref="testService " method="testScheule" cron="#{applicationProps['scheduling.job.cron']}" />
</task:scheduled-tasks>

cvc-elt.1: Cannot find the declaration of element 'beans'

I am trying to get a bean object to authenticate the user login functionality with Spring Security:
ApplicationContext context = new ClassPathXmlApplicationContext(
"com/humandevice/drive/fx/util/applicationContext.xml");
authenticationManager = (AuthenticationManager) context
.getBean("authenticationManager");
My applicationContext.xml is below:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="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.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframework.org/schema/context">
<context:component-scan base-package="com.humandevice.drive.fx">
<context:include-filter type="regex"
expression="com.humandevice.drive.fx.*" />
</context:component-scan>
<bean id="LoginController" alias="loginController" class="controller.LoginController">
<property name="authenticationManager" ref="authenticationManager" />
<property name="applicationContext" ref="applicationContext" />
</bean>
<bean id="applicationContext" alias="applicationContext"
class="org.springframework.context.ApplicationContext;">
</bean>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userService">
<password-encoder ref="bCryptPasswordEncoder" />
</authentication-provider>
</authentication-manager>
</beans>
but I get this exception:
Caused by: org.xml.sax.SAXParseException; lineNumber: 9; columnNumber: 64; cvc-elt.1: Cannot find the declaration of element 'beans'.
I am having difficulty understanding the issue.
Update
I have made some changes to my XML as such:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<import resource="/context-service.xml" />
<import resource="/context-repository.xml" />
<context:component-scan base-package="com.humandevice.drive.fx"></context:component-scan>
<authentication-manager>
<authentication-provider user-service-ref="com.humandevice.drive.service.user.IUserService">
<password-encoder ref="bCryptPasswordEncoder" />
</authentication-provider>
</authentication-manager>
</beans:beans>
I now receive this exception:
lineNumber: 11; columnNumber: 44; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'import'.
Your default namespace is http://www.springframework.org/schema/security and you configured xmlns:beans="http://www.springframework.org/schema/beans"this means you have to add the prefix beans: to all the tag form http://www.springframework.org/schema/beans so your XML should be as follows.
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="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.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframework.org/schema/context">
<context:component-scan base-package="com.humandevice.drive.fx">
<context:include-filter type="regex"
expression="com.humandevice.drive.fx.*" />
</context:component-scan>
<beans:bean id="LoginController" alias="loginController" class="controller.LoginController">
<beans:property name="authenticationManager" ref="authenticationManager" />
<beans:property name="applicationContext" ref="applicationContext" />
</beans:bean>
<beans:bean id="applicationContext" alias="applicationContext"
class="org.springframework.context.ApplicationContext;">
</beans:bean>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userService">
<password-encoder ref="bCryptPasswordEncoder" />
</authentication-provider>
</authentication-manager>
</beans:beans>
This code will help you.
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
classpath:/org/springframework/beans/factory/xml/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
classpath:/org/springframework/context/config/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
classpath:/org/springframework/aop/config/spring-aop-3.0.xsd
">
</beans>
For me I just did cut and paste and save of XML files in same place and it worked for me !!
I had similar issue and I want to give another example of possible causes.
Original .xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="https://www.springframework.org/schema/mvc"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:beans="https://www.springframework.org/schema/beans"
xmlns:context="https://www.springframework.org/schema/context" xmlns:tx="https://www.springframework.org/schema/tx"
xsi:schemaLocation="https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
https://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
working .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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
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-4.0.xsd">
Solutions:
use xmlns="http://www.springframework.org/schema/beans" as base xmlns. remove beans: prefix
change https to http
For me somehow the changes to the applicationContext.xml were not updated in the classpath. So I manually deleted the applicationContext.xml file from classpath and re-build the application which solved my problem
I'm completely unsure why this wierd kind of behavior. I too faced the same exception and followed what Karthikeyan Vaithilingam has advised. But still the issue is not resolved. So I have reverted the changes what ever I have done and saved the file. Viola!! Exception gone, no errors now.
I got same error while trying to create executable jar. I had
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:context.xml");
instead of (notice the * after classpath)
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:context.xml");

SpringBeanAutowiringInterceptor Not Injecting Spring Beans to Message Driven Bean : NullPointerException

I have gone through most resources and tried to the following mechanism to get the Spring Bean to my MDB.
#MessageDriven(name = "FileMDB")
#Interceptors(SpringBeanAutowiringInterceptor.class)
public class FileMessageBean implements MessageListener {
#Autowired
private IContextLoader contextLoader;
#Override
public final void onMessage(final Message message) {
}
}
My beanRefContext.xml is in the classpath of a JAR File Which is a dependency of a WAR 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"
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">
<bean
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg value="classpath:channel-integration-context.xml" />
</bean>
</beans>
The channel-integration-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Activates various annotations to be detected in bean classes: Spring's
#Required and #Autowired, as well as JSR 250's #Resource. -->
<!-- <context:annotation-config /> -->
<!-- AOP Annotation -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- File Adapter -->
<import resource="classpath:channel-dao-beans.xml" />
<!-- JMS Beans -->
<import resource="classpath:jms-beans.xml" />
<!-- Data JNDI Beans -->
<import resource="classpath:datasource-jndi-beans.xml" />
<context:component-scan base-package="com.fdc.channelintegration" />
<bean id="contextLoader" class="com.fdc.channelintegration.util.SpringContextLoader">
</bean>
</beans>
I am using Websphere 8.5 and my MDB is triggering properly. Still the contextLoader is not injected to the MDB and I am getting a NullPointerException.
Please help me out.
Didn't you forget to put #Cmponent or #Service annotation on your IContextLoader class so spring annotation processor could find it?
<context:annotation-config /> is commented out. Return it back to your context file and remove #Interceptors(SpringBeanAutowiringInterceptor.class)
Remove defenition of the bean from the context file:
<bean id="contextLoader" class="com.fdc.channelintegration.util.SpringContextLoader"/>

Errors of the Cineasts examples of Spring data neo4j

I imported the cineast maven project to eclipse, but I face a configuration problem...
cvc-complex-type.4: Attribute 'base-package' must appear on element 'neo4j:config'
Below are the configuration files which have this error.
ApplicationContext.xml
<?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"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
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.xsd
http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config/>
<context:component-scan base-package="org.neo4j.cineasts">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:spring-configured/>
<neo4j:config storeDirectory="target/data/graph.db"/>
<neo4j:repositories base-package="org.neo4j.cineasts.repository"/>
<!--neo4j:config graphDatabaseService="graphDatabaseService"/>
<bean id="graphDatabaseService" class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
<constructor-arg value="http://localhost:7474"/>
</bean-->
<bean class="org.neo4j.cineasts.movieimport.MovieDbApiClient">
<constructor-arg value="926d2a79e82920b62f03b1cb57e532e6"/>
</bean>
<bean class="org.neo4j.cineasts.movieimport.MovieDbLocalStorage">
<constructor-arg value="data/json"/>
</bean>
<tx:annotation-driven mode="proxy"/>
</beans>
movie-test-context.xml
<?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"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
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.xsd
http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config/>
<context:spring-configured/>
<context:component-scan base-package="org.neo4j.cineasts"/>
<neo4j:config graphDatabaseService="graphDatabaseService"/>
<neo4j:repositories base-package="org.neo4j.cineasts.repository"/>
<bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase" destroy-method="shutdown"/>
<bean class="org.neo4j.cineasts.movieimport.MovieDbApiClient">
<constructor-arg value="926d2a79e82920b62f03b1cb57e532e6"/>
</bean>
<bean class="org.neo4j.cineasts.movieimport.MovieDbLocalStorage">
<constructor-arg value="data/json"/>
</bean>
<tx:annotation-driven mode="proxy"/>
</beans>
Have a look at http://blog.neo4j.org/2014/03/spring-data-neo4j-progress-update-sdn-3.html
This is actually a duplicate question from earlier on Stack, but, long and short is that if you're using the latest SDN (and I'm pretty sure you are), then you need to include the "base-package" attribute in both the neo4j:config and neo4j:repositories elements.
In the link above, Michael Hunger explains this and you can clearly see what the values for those attributes should be.
HTH

Categories

Resources