How to change JdbcTemplate name? - spring

In my application multiple data source have been configured, so need to create jdbcTemplate in a different name. I am getting exception when changing the jdbcTemplate name
ApplicationContext.xml
<bean id="reportsViewTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>
<bean id="edao" class="com.myapp.dao.EmployeeDao">
<property name="jdbcTemplate1" ref="reportsViewTemplate"></property>
</bean>
In My DAO
private JdbcTemplate jdbcTemplate1;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate1 = jdbcTemplate;
}
getting the below exception when trying to change the name
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'edao' defined in class path resource [ApplicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'jdbcTemplate1' of bean class [com.myapp.dao.EmployeeDao]: Bean property 'jdbcTemplate1' is not writable or has an invalid setter method. Did you mean 'jdbcTemplate'?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1568)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1276)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.myapp.main.MainClass.main(MainClass.java:16)
I want to change the name from jdbcTemplate to any other name.How can I achieve that?

it looks like you havnt changed jdbcTemplate property name to jdbcTemplate 1 , at all the places.
Please recheck or share changed xml and java file.

Related

Extends Spring class from non Spring Class

I need to write Spring Component Bean and that need to extends from regular class (from abc.jar).
#Component
public DefClass extends AbcClass{
..
}
This is throwing error AbcClass is a non Spring class, not sure how I can create this as SpringBean.
I tried to defind in my application-Context.xml
<bean id ="abcBean" class ="package.AbcClass" "/>
<bean id ="defClass" class ="package.defClass" parent="abcBean"/>
WEB-INF/classes/applicationCotext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: com.tivoli.pd.jutil.PDException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
It's not spring project. The class is not available (guess the abc.jar is not added to classpath). Check how you build the application, check the abc.jar exists and included in the classpath.

Constructor dependency Injection issue

I am learning DI and new to spring while trying out CI I have written following code and I think I am correct in syntax still it's showing bean creation error. why it is unable to create bean..??
The code is
Constuctor.java
package beans;
public class Constructor {
private String name;
private int age;
private String email;
public void Constructor(String name, int age, String email){
this.name=name;
this.age=age;
this.email=email;
}
public void show()
{
System.out.println("Name = "+name);
System.out.println("Age = "+age);
System.out.println("Email = "+email);
}
}
spring.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<!-- Ordered parameters -->
<beans>
<bean id="t" class="beans.Constructor">
<constructor-arg value="Alok"/>
<constructor-arg value="24"/>
<constructor-arg value="alok#gmail.com"/>
</bean>
</beans>
Const_main.java
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import beans.Constructor;
public class Const_main {
public static void main(String[] args) {
ApplicationContext ap= new ClassPathXmlApplicationContext("resources/spring.xml");
Constructor c = (Constructor)ap.getBean("t");
c.show();
}
}
it's giving the following error
Jun 29, 2017 3:16:45 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#b1a58a3: startup date [Thu Jun 29 15:16:45 IST 2017]; root of context hierarchy
Jun 29, 2017 3:16:45 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [resources/spring.xml]
Jun 29, 2017 3:16:46 PM org.springframework.context.support.ClassPathXmlApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: **Error creating bean with name 't' defined in class path resource [resources/spring.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
Exception in thread "main"** org.springframework.beans.factory.BeanCreationException: Error creating bean with name 't' defined in class path resource [resources/spring.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:240)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at test.Const_main.main(Const_main.java:10)
Try this way
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<!-- Ordered parameters -->
<beans>
<bean id="t" class="beans.Constructor">
<constructor-arg>
<value>Alok</value>
</constructor-arg>
<constructor-arg>
<value>24</value>
</constructor-arg>
<constructor-arg>
<value>alok#gmail.com</value>
</constructor-arg>
</bean>
</beans>
You are trying to use Constructor Dependency Injection without creating any such constructor in your DTO (Constructor.java)
The method you defined above:
public void Constructor(String name, int age, String email){
this.name=name;
this.age=age;
this.email=email;
}
is just a simple method/function not a constructor, try to remove the word void.
Note:
To remove any ambiguities for constructor matching, it is more prefer to use indexes with constructor's parameters like:
<bean id="t" class="beans.Constructor">
<constructor-arg value="Alok" index="0"/>
<constructor-arg value="24" index="1"/>
<constructor-arg value="alok#gmail.com" index="2"/>
</bean>

Error creating bean with name 'sqlSessionFactory' ... Invocation of init method failed; nested exception is java.lang.NullPointerException

I'm trying to integrate spring-mybatis in my application. I'm using spring 4.1.4 and mybatis 3.2.8 java 7 and the server is WebSphere Application Server Liberty. I'm using a spring java configuration class. This error is showing only when I'm deploying the war file, if I'm using Eclipse to deploy the project everything is ok. Deploying in Tomcat 8 is working too.
#Configuration
#MapperScan("x.y.z.mappers")
#PropertySource("classpath:/x/y/z/data_source.properties")
#EnableTransactionManagement
public class DataAccessConfiguration {
#Autowired
private Environment env;
#Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("driver"));
dataSource.setUrl(env.getProperty("url"));
dataSource.setUsername(env.getProperty("user"));
dataSource.setPassword(env.getProperty("password"));
return dataSource;
}
#Bean
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
#Bean
public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setTypeAliasesPackage("x.y.z.portal.model");
return sessionFactory;
}
}
I got this stacktrace
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in a.b.c.DataAccessConfiguration: Invocation of init method failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1127)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1051)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1280)
... 60 more
Caused by: java.lang.NullPointerException
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.readLine(BufferedReader.java:317)
at java.io.BufferedReader.readLine(BufferedReader.java:382)
at org.apache.ibatis.io.DefaultVFS.list(DefaultVFS.java:93)
at org.apache.ibatis.io.VFS.list(VFS.java:193)
at org.apache.ibatis.io.ResolverUtil.find(ResolverUtil.java:216)
at org.apache.ibatis.type.TypeAliasRegistry.registerAliases(TypeAliasRegistry.java:127)
at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:399)
at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:355)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
Instead try this to integrate your application with mybatis. Add below code to spring-dispatcher-servlet.xml.
<context:property-placeholder location="x/y/z/data_source.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>${database.driver}</value></property>
<property name="url"><value>${database.url}</value></property>
<property name="username"><value>${database.username}</value></property>
<property name="password"><value>${database.password}</value></property>
<property name="maxActive"><value>${database.maxactiveconnections}</value></property>
<property name="maxIdle"><value>${database.idleconnections}</value></property>
<property name="initialSize"><value>${database.initialSize}</value></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="/WEB-INF/mybatis-config.xml"/>
</bean>
<bean id="Dao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="mapperInterface" value="com.dao.Dao" />
</bean>

Getting error while dynamically providing input,output path

I have created a multi-file reader and flat file writer (Later, I have to extend it to multifile read to multiple file write, one to one mapping). When i provide the input path through property file, It works all good, but when I try to provide path through jobExecutionContext it starts giving me error. What surprises me is that writer is successfully taking the path from executionContext and writing whatever location i provide but not the reader.
PS: I am using "SingleItemPeekableItemReader" because the data i have in file is multi-line record and has no way to determine when one record ends . the only way to know that record 2 has started is when you have access to next line and you know that its start of new record.
Can someone through some light, What wrong i am doing.
<bean id="itemReader" class="com.varun.reader.AccountDataReader">
<property name="fieldSetReader" ref="PeekableitemReader" />
<property name="headerFieldSetMapper" ref="headerFieldSetMapper" />
<property name="multiReader" ref="multiResourceReader"/>
</bean>
<bean id="PeekableitemReader" class="org.springframework.batch.item.support.SingleItemPeekableItemReader" >
<property name="delegate" ref="multiResourceReader"/>
</bean>
<bean id="multiResourceReader" class=" org.springframework.batch.item.file.MultiResourceItemReader" >
<property name="resources" value="file:${file.input.dir}*.DAT" />
<property name="delegate" ref="fileItemReader" />
</bean>
<bean id="fileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer" ref="accountDataTokenizer"/>
<property name="fieldSetMapper">
<bean class="org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper" />
</property>
</bean>
</property>
<property name="comments" value=""></property>
</bean>
It error out, when i provide the resources in multiresourceReader as below
<property name="resources" value="file:#{JobExecutionContext[input_dir]}*.DAT" />
Error logs as below:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step1': Cannot resolve reference to bean 'itemReader' while setting bean property 'itemReader'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itemReader' defined in class path resource [spring/config/process-config.xml]: Cannot resolve reference to bean 'PeekableitemReader' while setting bean property 'fieldSetReader'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'PeekableitemReader' defined in class path resource [spring/config/process-config.xml]: Cannot resolve reference to bean 'multiResourceReader' while setting bean property 'delegate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multiResourceReader' defined in class path resource [spring/config/process-config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'JobExecutionContext' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:753)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
after making multireader scope as step i am getting error as below.I have tried putting scope on all the other delegate readers as well, but i am getting the same error.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step1': Cannot resolve reference to bean 'itemReader' while setting bean property 'itemReader'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itemReader' defined in class path resource [spring/config/process-config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type [com.sun.proxy.$Proxy11 implementing org.springframework.batch.item.ItemStreamReader,org.springframework.batch.item.ItemStream,org.springframework.aop.scope.ScopedObject,java.io.Serializable,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [org.springframework.batch.item.file.MultiResourceItemReader] for property 'multiReader'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy11 implementing org.springframework.batch.item.ItemStreamReader,org.springframework.batch.item.ItemStream,org.springframework.aop.scope.ScopedObject,java.io.Serializable,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [org.springframework.batch.item.file.MultiResourceItemReader] for property 'multiReader': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:753)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.varun.JobScheduler.main(JobScheduler.java:9)
My custom Reader
public class AccountDataReader implements ItemReader<AccountDataBO> {
private static Log log = LogFactory.getLog(AccountDataReader.class);
private boolean recordFinished;
private AccountDataBO acctObj = new AccountDataBO();
private SingleItemPeekableItemReader<FieldSet> fieldSetReader;
private HeaderFieldSetMapper headerFieldSetMapper;
private MultiResourceItemReader<FieldSet> multiReader;
#Override
public AccountDataBO read() throws Exception {
recordFinished = false;
while (!recordFinished) {
process(fieldSetReader.read());
}
AccountDataBO result = acctObj;
acctObj = new AccountDataBO();
return result;
}
private void process(FieldSet fieldSet) throws Exception {
if (fieldSet == null) {
log.debug("FINISHED Reading");
recordFinished = true;
acctObj=null;
return;
}
String lineId = fieldSet.readString(0);
if (!StringUtils.isEmpty(lineId) && lineId.contains(AccountDataBO.ACCOUNT)) {
log.debug("Starting new Record");
acctObj.setHeader(headerFieldSetMapper.mapFieldSet(fieldSet));
acctObj.setResource(multiReader.getCurrentResource());
}
//Code
}
private void CheckEOFNewAccount() throws Exception {
if ((fieldSetReader.peek() != null
&& ((fieldSetReader.peek().getValues())[0].contains(AccountDataBO.ACCOUNT)))
|| fieldSetReader.peek() == null) {
log.debug("Next Line is Account or EOF Hence returning for current Account");
recordFinished = true;
return;
}
}
public void setFieldSetReader(SingleItemPeekableItemReader<FieldSet> fieldSetReader) {
this.fieldSetReader = fieldSetReader;
}
public void setHeaderFieldSetMapper(HeaderFieldSetMapper headerFieldSetMapper) {
this.headerFieldSetMapper = headerFieldSetMapper;
}
public void setMultiReader(MultiResourceItemReader<FieldSet> multiReader) {
this.multiReader = multiReader;
}
}
In order to inject job parameters into a batch component, the component needs to be step scoped. So in your example, change this:
<bean id="multiResourceReader"
class=" org.springframework.batch.item.file.MultiResourceItemReader" >
To this:
<bean id="multiResourceReader"
class=" org.springframework.batch.item.file.MultiResourceItemReader"
scope="step" >

How do I use the spring jars in my liferay portlet instead of the spring jars in the ROOT\WEB-INF\lib?

I have a portlet that I've created and I'm trying to use spring jpa in it for data retrieval. I'm using Spring version 3.2.14 and the spring jars that I need are deployed in my portlet's WEB-INF/lib folder.
On tomcat start up I keep getting the following exception:
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'packagesToScan' of bean class [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Bean property 'packagesToScan' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1052)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:921)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
... 39 more
If I attach a debugger to Tomcat I can see that it is using spring-beans.jar(version 3.0.7.RELEASE) that is in ROOT/WEB-INF/lib which has a know issue with 'packageToScan' in LocalContainerEntityManagerFactoryBean in my bean definition:
<bean id="reportingEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>org.preptoolkit.exercise.reporting.model</value>
</list>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/>
</bean>
</property>
</bean>
I have tried putting spring-beans-3.2.14.RELEASE.jar in $tomcat_home/lib/ext and liferay still uses the 3.0.7 verion.
Liferay Version: 6.2-ce-ga4
So how do I get Liferay to use spring-beans-3.2.14.RELEASE.jar instead of the one ROOT for my portlet?
(I've been pounding my head on this wall for 3 days now)
UPDATE
Here is the full stacktrace:
13:26:55,702 ERROR [localhost-startStop-1][ContextLoader:206] Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reportingEntityManagerFactory' defined in ServletContext resource [/WEB-INF/classes/persistenceContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'packagesToScan' of bean class [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Bean property 'packagesToScan' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1363)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1085)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:516)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:282)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:204)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
at com.liferay.portal.spring.context.PortletContextLoaderListener.contextInitialized(PortletContextLoaderListener.java:85)
at com.liferay.portal.deploy.hot.SpringHotDeployListener.doInvokeDeploy(SpringHotDeployListener.java:85)
at com.liferay.portal.deploy.hot.SpringHotDeployListener.invokeDeploy(SpringHotDeployListener.java:41)
at com.liferay.portal.deploy.hot.HotDeployImpl.doFireDeployEvent(HotDeployImpl.java:227)
at com.liferay.portal.deploy.hot.HotDeployImpl.fireDeployEvent(HotDeployImpl.java:96)
at com.liferay.portal.kernel.deploy.hot.HotDeployUtil.fireDeployEvent(HotDeployUtil.java:28)
at com.liferay.portal.kernel.servlet.PluginContextListener.fireDeployEvent(PluginContextListener.java:164)
at com.liferay.portal.kernel.servlet.PluginContextListener.doPortalInit(PluginContextListener.java:154)
at com.liferay.portal.kernel.util.BasePortalLifecycle.portalInit(BasePortalLifecycle.java:44)
at com.liferay.portal.kernel.util.PortalLifecycleUtil.register(PortalLifecycleUtil.java:74)
at com.liferay.portal.kernel.util.PortalLifecycleUtil.register(PortalLifecycleUtil.java:58)
at com.liferay.portal.kernel.util.BasePortalLifecycle.registerPortalLifecycle(BasePortalLifecycle.java:54)
at com.liferay.portal.kernel.servlet.PluginContextListener.contextInitialized(PluginContextListener.java:116)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1113)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1671)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'packagesToScan' of bean class [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Bean property 'packagesToScan' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1052)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:921)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
... 39 more
There is a SpringHotDeployListener that tries to initialize Spring (with the library from Liferay) for every portlet app.
To turn that off for your webapp you will have to remove com.liferay.portal.deploy.hot.SpringHotDeployListener in the property hot.deploy.listeners in your portal-ext.properties.
If you use other portlet apps which make use of the Spring environment of Liferay, you will need to create an Ext Plugin and extend SpringHotDeployListener in ext-impl:
public class MySpringHotDeployListener extends SpringHotDeployListener {
protected void doInvokeDeploy(HotDeployEvent hotDeployEvent) throws Exception {
if (!hotDeployEvent.getServletContext().getServletContextName().equals("MyContextName")) {
super.doInvokeDeploy(hotDeployEvent);
}
}
// Same for doInvokeUndeploy
}
Use MySpringHotDeployListener insteand of SpringHotDeployListener in portal-ext.properties then.

Resources