Spring 3.0 load proprrties file and set #Scheduled cron - java

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>

Related

Parameter 0 of constructor in type required a bean of type 'org.hibernate.SessionFactory' that could not be found

I had two projects:
Project A which is in charge of the view of a website (Spring 3 + JSF + JSP)
Project B which is in charge of the logic of that website (Spring 3)
Both of them work just fine in the current version. However, we wanted to migrate project A (developed using JSF and JSP) to Spring Boot.
I started a Spring Starter Project using STS4 and as soon as I try to run the app, this error pops out:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
oct 27, 2021 10:14:12 AM org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter report
GRAVE:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in {CLASS} required a bean of type 'org.hibernate.SessionFactory' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.hibernate.SessionFactory' in your configuration.
The thing is that {CLASS} is in project B, which has its own configuration file that works just fine with the old project A. This is the file that loads that sessionFactory bean in project B:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="hibernateProperties">
<bean class="org.foo.bar.PropiedadesEntorno">
<constructor-arg>
<ref bean="entorno" />
</constructor-arg>
</bean>
</property>
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>
</beans>
And the config file of the new project:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
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">
<bean name="entorno" class="java.lang.Integer">
<constructor-arg type="int" value="1" />
</bean>
<context:annotation-config />
<context:component-scan base-package="org.foo.bar" />
<import resource="classpath*:META-INF/spring/config.xml"/>
</beans>
The class that loads that SessionFactory is this one:
#Repository
public class ContratoDao extends BaseDao implements IContratoDao {
#Autowired
public ContratoDao(SessionFactory sessionFactory) {
super(sessionFactory);
}
...
}
I tried several solutions all around here but i can't get them to work as I can't change project B.
Thanks in advance for any help.
You can get Session from EntityManager. Inject EntityManager
#Autowired
private EntityManager entityManager;
private Session getSession() {
return entityManager.unwrap(Session.class);
}
and use getSession()
in ContratoDao. It may help you to solve this issue.

bean definition is not loading into spring context

I defined the beans definition in country-sg.xml file which I placed in the src/main/resources
Created the root-context.xml and imported above file as , I added root-context file web.xml file.
country-sg.xml file is not loading into context.
Can anyone help how to load this configuration file.
Thanks in advance.
root-context.xml file :
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.kachakayala" />
<mvc:annotation-driven />
<import resource="classpath*: /country-sg.xml"/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
country-sg.xml file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="sg" class="com.kachakayala.services.CountrySg"></bean>
</beans>
In order to add the Bean sg in the application context, you have to scan the the configuration class country-sg.xml first. You can achieve this by adding the annotation #ComponentScan("package-of-your-config.xml") at your main class definition header.

Failed to read schema document http://www.springframework.org/schema/beans/spring-context.xsd

I was getting the following error message while i was working with Spring with IntelliJ:
Failed to read schema document
'http://www.springframework.org/schema/beans/spring-context.xsd',
because 1) could not find the document; 2) the document could not be
read; 3) the root element of the document is not .
Help on this.
I was using wrong url in xsi:schemaLocation
Instead if using:
http://www.springframework.org/schema/beans/spring-context.xsd
I should have to used:
http://www.springframework.org/schema/context/spring-context.xsd
That solved my problem.
I crossed checked this with firing url into browser where within beans no spring-context.xsd was available.
Hope some of you can help with this.
#Indrajeet : The proper spring.xml or application-context.xml format :
<?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"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.demo" />
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

Spring #Async. XML configuration

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?

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"/>

Categories

Resources