Is there any example of using spring xd with pure java config (no xml).
I created a simple tasklet, jarred the same (whojob.jar) and put it into the lib folder.
The xml config is below. I put this in a file called whojob which is in the modules/job folder and also jarred with my whojob.jar.
When I try to create a job:- (:>job create --name mywhojob --definition "whojob") I get the error :
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.integration.config.TransformerFactoryBean#0': Cannot create inner bean 'org.springframework.xd.dirt.plugins.job.JobLaunchRequestTransformer#0' of type [org.springframework.xd.dirt.plugins.job.JobLaunchRequestTransformer] while setting bean property 'targetObject'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.xd.dirt.plugins.job.JobLaunchRequestTransformer#0' defined in class path resource [META-INF/spring-xd/plugins/job/job-module-beans.xml]: Cannot resolve reference to bean 'jobFactoryBean' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobFactoryBean': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: No Batch Job found in registry for the provided key 'mywhojob.job'.
My xml config is below. If possible I would like to have a pure java solution.
Thanks
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch.xsd">
<batch:job id="whojob">
<batch:step id="whojobStep">
<batch:tasklet ref="whojobXDTasklet" />
</batch:step>
</batch:job>
<bean id="whojobXDTasklet"
class=" com.auction.data.batch.job.WhoTasklet" >
<property name= "who" ref="whoBean"/>
</bean>
<bean id="whoBean"
class="com.auction.data.batch.service.Who" >
</bean>
</beans>
Here is a sample that uses XD job with the batch tasklet defined in an external jar file.
https://github.com/ilayaperumalg/spring-xd-batch-sample
To create jar, from the repo root directory, just run: ./gradlew build
To make it easier, I have also copied the job module "myjob" (which uses the above tasklet) into the repo. You can just copy this 'myjob' into your $XD_HOME/modules/job/.
Give it a try using XD version M6 or the latest master, and let us know if you have any question.
Related
I'm trying to deploy a Spring Service in WSO2 Application Service. I did THIS tutorial and the app works fine on Eclipse, but when I try to deploy it on WSO2 I get this error:
Cannot load Spring beans. Please check the Spring context
configuration file and verify that the defined Spring beans exist in
the .jar file.
I unziped de .jar file and JdbcCustomerDAO class is there with all the others.
Spring context:
<beans xmlns="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-2.5.xsd">
<bean id="customerDAO" class="com.mkyong.customer.dao.impl.JdbcCustomerDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="url" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
</beans>
I also tried to include spring and mysql-connector-java jars on /repository/components/extensions as says HERE.
EDIT:
ERROR {org.wso2.carbon.springservices.ui.SpringServiceMaker} - Cannot
load Spring beans. Please check the Spring context configuration file
and verify that the defined Spring beans exist in the .jar file.
{org.wso2.carbon.springservices.ui.SpringServiceMaker}
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot
find class
[org.springframework.jdbc.datasource.DriverManagerDataSource] for bean
with name 'dataSource' defined in resource loaded through InputStream;
nested exception is java.lang.ClassNotFoundException:
org.springframework.jdbc.datasource.DriverManagerDataSource
Looks like it couldn't find spring-jdbc, so I added the jar to extensions but now I get this error:
ERROR {org.wso2.carbon.springservices.ui.SpringServiceMaker} - Cannot load
Spring beans. Please check the Spring context configuration file and
verify that the defined Spring beans exist in the .jar file.
{org.wso2.carbon.springservices.ui.SpringServiceMaker}
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'dataSource' defined in resource loaded
through InputStream: Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException: Could not
instantiate bean class
[org.springframework.jdbc.datasource.DriverManagerDataSource]:
Constructor threw exception; nested exception is
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
I understand that commons.logging is also missing, but when I try to add it to extensions, WSO2 doesn't start. I get a lot of
Log4j:ERROR Could not instantiate...
I have a Spring + Hibernate project deployed on JBoss 5. It was using Ant earlier and recently I have modified the project structure to use Maven 2. But all the code and config files are the same.
I am using Spring JTA for transaction management in my project. I am able to perfecly buid the project and deploy it on JBoss. But when I try to execute the JUnit tests it gives me the following error:
java.lang.IllegalStateException: No JTA UserTransaction available - specify either 'userTransaction' or 'userTransactionName' or 'transactionManager' or 'transactionManagerName'
at org.springframework.transaction.jta.JtaTransactionManager.checkUserTransactionAndTransactionManager(JtaTransactionManager.java:473)
at org.springframework.transaction.jta.JtaTransactionManager.afterPropertiesSet(JtaTransactionManager.java:413)
The wierd thing is that this was working perfeclty fine when I was using Ant. It started coming after the migration to maven.
This is the entry in spring-jpa-conf.xml file:
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" >
</bean>
And this is the Junit code:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:spring-conf.xml" })
#Transactional(value = "transactionManager")
public abstract class LocalUsersTestBase {
#PersistenceContext(unitName="books-lemf")
protected EntityManager entityManager;
protected void getUsersDetails(List<Users> out) {
..........
}
I also tried to add a property in the config file entry as shown here:
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" >
<property name="userTransactionName" value="java:/TransactionManager"></property>
</bean>
But it gives the error:
Caused by: org.springframework.transaction.TransactionSystemException: JTA UserTransaction is not available at JNDI location [java:/TransactionManager]; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at org.springframework.transaction.jta.JtaTransactionManager.lookupUserTransaction(JtaTransactionManager.java:548)
at org.springframework.transaction.jta.JtaTransactionManager.initUserTransactionAndTransactionManager(JtaTransactionManager.java:425)
at org.springframework.transaction.jta.JtaTransactionManager.afterPropertiesSet(JtaTransactionManager.java:412)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1460)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1400)
... 37 more
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
Please help.
Thanks!!
This is my complete spring config 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:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" >
</bean>
<jee:jndi-lookup id="books-pu"
jndi-name="java:/books-emf"
cache="true"
lookup-on-startup="false"
proxy-interface="javax.persistence.EntityManagerFactory"
/>
<jee:jndi-lookup id="booksDataSource" jndi-name="java:/books-ds"/>
</beans>
Firsty, from spring reference documentation
If you use JTA in a Java EE container then you use a container
DataSource
, obtained through JNDI,
in conjunction with Spring’s
JtaTransactionManager
. This is what the JTA and JNDI lookup version
would look like:
The
JtaTransactionManager
does not need to know about the
DataSource
, or any other specific
resources, because it uses the container’s global transaction management infrastructure
then when you run the tests you need to a environment container managed to get a JTA User Transaction. In addition can you share with us the complete spring configuration
i am running a Karaf container with a number of beans implementing the com.mycompany.foo.IMyBean interface. i refer to them as "child beans". each such "child" bean is registered as a service. i also have a single "parent" bean that rounds up all those "child" services by using osgi:list. everything works just fine in runtime. however, when i run a very simple JUnit scenario, i get the following exception:
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'BeanRefsList': Invocation of init method
failed; nested exception is java.lang.IllegalArgumentException:
Required 'bundleContext' property was not set.
this is the context.xml in my JUnit project:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd
">
<bean id="ChildBean"
class="com.mycompany.foo.ChildBean">
</bean>
<osgi:service id="ChildBeanService" ref="ChildBean" interface="com.mycompany.foo.IMyBean"/>
<osgi:list id="BeanRefsList" interface="com.mycompany.foo.IMyBean"/>
<bean id="ParentBean" class="com.mycompany.foo.ParentBean">
<property name="childBeans" ref="BeanRefsList"/>
</bean>
</beans>
the test class also contains the following annotation entries:
#org.junit.runner.RunWith(SpringJUnit4ClassRunner.class)
#org.springframework.test.context.ContextConfiguration("context.xml")
please let me know what i am doing wrong. thank you for your time!
The error message explains it pretty clearly. The line that says
<osgi:list id="BeanRefsList" interface="com.mycompany.foo.IMyBean"/>
Needs too look more like
<bean id="ParentBean" class="com.mycompany.foo.ParentBean">
<property name="childBeans" ref="BeanRefsList"/>
</bean>
Where the property "bundleContext" gets set properly. I'm not familiar with OSGI, so I don't know what class/object needs to be set here. But, that's what's missing.
I'm not sure what OSGI is looking for that's making it tell you "bundleContext" property is missing, but it sounds like both osgi:list and osgi:service use some kind of bundleContext property.
Have you specified all of the same imported schemas in jUnit that you did for your runtime context?
I am developing a web application using JSF 2.0 ,Spring 3.1.
When I am deploying my application I am getting following error
ERROR [ContextLoader] Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'AllProjectDetailsBean' defined in ServletContext resource [/WEB-INF/springApp-spring.xml]: Could not resolve placeholder 'OutputFilePath'
Application Context 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:aop="http://www.springframework.org/schema/aop"
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 ">
<aop:aspectj-autoproxy />
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
</bean>
<bean id="AllProjectDetailsBean"
class="com.tcs.srl.expertFinder.sections.ProjectDetailsBeanFactory"
factory-method="createInstance">
<constructor-arg index="0" value="${OutputFilePath}" />
</bean>
What I understand from error is when I deploy application the context loads very first time but it fails because it dose not find any property with name OutputFilePath.
That means I have to set system property with the name OutputFilePath before context loads.
Can some tell me how to set system property while deploying jsf application or before context loads.
Or is there any option to remove this error.
Please Help
Thanks
I am working on the spring spring-3.2.2. I have created two java projects in eclipse.
SpringTest
Testclasspath
SpringTest project is having the below beans.xml in which the one bean is defined.
<beans xmlns="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-3.0.xsd">
<bean id="helloWorld" class="com.spring.HelloWorld" init-method="testUpdate" scope="prototype">
<property name="message" value="Hello World!"/>
</bean>
</beans>
I have created the jar springtest.jar of the project SpringTest and it is been added in the classpath of the project Testclasspath. Bean configuration file for the Testclasspath project is talentacquisition.xml and it is importing the beans.xml file of the Springtest project. Please find the below content of talentacquisition.xml
<beans xmlns="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-3.0.xsd">
<import resource="Beans.xml"/>
<bean id="juggler" class="com.springaction.springidol.Juggler" />
</beans>
I am confused with behavior of the import tag in the talentacquisition.xml How it is able to locate the Beans.xml which is present in the jar (springtest.jar) in the classpath and able to load the beans? Why spring is not giving any error ? Don't I have to modify the import tag in the talentacqusition.xml to following
<import resource="classpath:Beans.xml"/>
If import is able to locate the file Beans.xml , then when should we use classpath: and classpath* :?
ResourceLoaders are responsible for how Spring loads the resource. From the reference manual
The location path or paths supplied to an ApplicationContext
constructor are actually resource strings, and in simple form are
treated appropriately to the specific context implementation.
ClassPathXmlApplicationContext treats a simple location path as a
classpath location. You can also use location paths (resource strings)
with special prefixes to force loading of definitions from the
classpath or a URL, regardless of the actual context type.
The ClassPathXmlApplicationContext you're instantiating "treats a simple location path as a classpath location", i.e. it treats "Beans.xml" as "classpath:Beans.xml". Similarly, FileSystemXmlApplicationContext would treat "Beans.xml" as "file:Beans.xml".
Section 6.7 of the manual has more details too.