Spring bean configuration list<map> - java

Im trying to create a bean configuration file for my
SettingsDto class:
public class SettingsDto {
private String administratorEmail;
private String gatekeeperAddress;
private String webRtcUrl;
private String userGuideUrl;
private String vmrFaqUrl;
private String lyncGuideUrl;
List<Map<String, String>> supportPhones;
List<String> audioCalls;
But Im having issues with how Im doing the supportPhones (List<Map<String, String>>)
<?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"
xmlns:context="http://www.springframework.org/schema/context"
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/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<context:component-scan base-package="sandbox.spring" />
<bean id="SettingsBean" class="com.dto.SettingsDto">
<property name="administratorEmail" value="v#.com" />
<property name="gatekeeperAddress" value="192.168.1.0" />
<property name="webRtcUrl" value="https://web.com" />
<property name="userGuideUrl"
value="url" />
<property name="vmrFaqUrl"
value="url" />
<property name="lyncGuideUrl"
value="url" />
<property name="audioCalls">
<util:list>
<value>+1 (000)000-0000</value>
<value>(000)000-0000</value>
</util:list>
</property>
<property name="supportPhones">
<util:list>
<ref bean="supportPhonesMapping1" />
<ref bean="supportPhonesMapping2" />
</util:list>
</property>
<util:map id="supportPhonesMapping1">
<entry key="name" value="North America" />
<entry key="phone" value-ref="+1 111-1111" />
</util:map>
<util:map id="supportPhonesMapping2">
<entry key="name" value="+11 111-1111" />
<entry key="phone" value-ref="International" />
</util:map>
</bean>
</beans>
The error I get is
Invalid content was found starting with element 'util:map'. No child
element is expected at this point

You cannot set value to util:map that way. value can only take a string. Define a util:map separately, then use ref to reference it.
For example:
<util:map id="myMap">
<entry key="name" value="..." />
</util:map>
<util:list>
<ref bean="myMap"/>
</util:list>
Also, value-ref should reference another bean - currently you have it as a string literal value, which you can simply use value for.

Maybe it's your beans schema. You have it slightly wrong. It should be:
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

so in the end there were a few issues.
first off my declaration was wrong
<entry key="phone" value-ref="International" />
needed to be
<entry key="phone" value="International" />
(no "-ref")
I also changed the xml to define my list and map outside of the bean then referenced it in the bean
<property name="supportPhones" ref="supportPhoneList" />

Related

How to map the marshalled object values(reader) to hibernate entity object(writer) in spring batch

I am new to Spring Batch, even after an extensive search also I didn't get any clue on how to map the XML marshalled object value to hibernate entity object. To be specific, I think it follows the same approach as FieldSetMapper but not sure how to get the MarshallObject instead of FlatFieldSet. Please help.
FYI: I should use Hibernate only and not JdbcTemplate or preparedstatementsetter etc.
The batch config file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<import resource="//context.xml" />
<!-- <context:component-scan base-package="com.politico.batchhandle.marshallbatch" /> -->
<!-- parallel job processing -->
<job id="feedprocesser" xmlns="http://www.springframework.org/schema/batch">
<!-- <split id="split1"> <flow> -->
<step id="stepfeedprocess">
<!-- <tasklet task-executor="taskExecutor" throttle-limit="10"> -->
<tasklet>
<chunk reader="xmlmultiReader" writer="dataWriter" processor="dataProcessor"
commit-interval="10" />
</tasklet>
</step>
<!-- </flow> </split> -->
</job>
<!-- <bean id="testReader" class="com.politico.batchhandle.marshallbatch.DataReader"
/> -->
<bean id="dataProcessor" class="com.politico.batchhandle.marshallbatch.DataProcessor" />
<bean id="dataWriter" class="com.politico.batchhandle.marshallbatch.DataWriter" />
<!-- <bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor"
/> -->
<bean id="xmlmultiReader" class="org.springframework.batch.item.xml.StaxEventItemReader">
<property name="fragmentRootElementName" value="crs-bill-summary" />
<property name="resource" value="classpath:xmlfiles\\Bill_Digest.xml" />
<property name="unmarshaller" ref="reportUnmarshaller" />
<property name="fieldSetMapper" ref="dataFieldSetMapper" />
</bean>
<bean id="reportUnmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.politico.jaxbconvert.readerTO.CrsBillSummary</value>
</list>
</property>
<property name="mappingLocation" value="classpath:/userMapping.xml" />
</bean>
<bean id="dataFieldSetMapper" class="com.politico.batchhandle.dao.DataFieldSetMapper">
</bean>
</beans>
I assumed xml marshall object also can be mapped using FieldSetMapper, here is the corresponding code:
public class DataFieldSetMapper implements FieldSetMapper<CrsBillSummary>{
#Override
public CrsBillSummary mapFieldSet(FieldSet fieldset) throws BindException {
// TODO Auto-generated method stub
return null;
}
}
I am sure I am using the wrong interface for fieldsetmapping , so please help me with the right approach..
Please add this one to your XML
<bean id="reportUnmarshaller"
class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="aliases">
<util:map id="aliases">
<entry key="trade"
value="XXX.CrsBillSummary" />
<entry key="property1" value="java.math.BigDecimal" />
<entry key="property2" value="java.lang.String" />
</util:map>
</property>
</bean>
Note: CrsBillSummary in your case, I hope you should use Pojo class not Entity one. And then you can convert to Entity at Writer layer then persist it to Database. It is best practice I recommend use it to avoid mistakes which unattended objects persist to Database.

It seems that JDBCTemplate ignores SET CONSTRAINT ALL DEFERRED

I am having this Spring 4.0 service that copies tables from one schema to another. I am switching between Datasources programmatically by updating the Datasource in an abstract class from which all DAOs inherit.
public abstract class GenericDao implements SystemChangedListener {
private static final Logger logger = Logger.getLogger(GenericDao.class);
private NamedParameterJdbcTemplate jdbcTemplate;
/**
* Initializing the bean with the definition data source through #Autowired
* #param definitionDataSource as instance of #DataSource
*/
#Autowired
private void setDataSource(DataSource definitionDataSource) {
this.jdbcTemplate = new NamedParameterJdbcTemplate(definitionDataSource);
}
public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate(){
return this.jdbcTemplate;
}
#Override
public void updateDataSource(DataSource dataSource) {
this.setDataSource(dataSource);
}
Whenever I need to switch to anothe DataSource I call this method. It all works fine so far, except for the cases when I have a parent-child tables in a foreign key relationship with foreign key defined as deferrable. I get 'ORA-02292: integrity constraint - child record' found whenever the application tries to delete first the parent table.
This happens in a service by calling the methods like this:
logger.info("Switching to target system: " + repDTO.getSourceSystem());
systemService.switchSystem(targetSys);
logger.info("Cleaning up data in target table: " + repDTO.getTableToReplicate());
// deleting the data
managedTableService.cleanData(repDTO.getTableToReplicate());
logger.info("Importing data in target table: " + repDTO.getTableToReplicate());
// importing the data
managedTableService.importData(inserts);
Methods are executed in a transaction so when something goes wrong I return to the clean state. I also use Hibernate for collecting meta information about my tables, before copying them, but for this I use a different connection.
I tried to call SET CONSTRAINT ALL DEFERRED before the cleaning method, but it doesn't work. I even tried a batch update - SET CONSTRAINT ALL DEFERRED + DELETE FROM TABLE, still no success. What am I doing wrong? Any help is appreciated. Thank you!
PS. I use Oracle 11 as a DB. Here's my spring datasource configuration
<?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:util="http://www.springframework.org/schema/util"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!-- Scans within the base package of the application for #Components to configure as beans -->
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/db.properties" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:packagesToScan="de.telekom.cldb.admin"
p:dataSource-ref="dataSource"
p:jpaPropertyMap-ref="jpaPropertyMap"
p:jpaVendorAdapter-ref="hibernateVendor" />
<bean id="hibernateVendor" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="${db.dialect}" />
</bean>
<!-- system 'definition' data source -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="${db.driver}"
p:url="${db.url}"
p:username="${db.username}"
p:password="${db.password}" />
<!--
p:maxActive="${dbcp.maxActive}"
p:maxIdle="${dbcp.maxIdle}"
p:maxWait="${dbcp.maxWait}"/>
-->
<util:map id="jpaPropertyMap">
<entry key="generateDdl" value="false"/>
<entry key="hibernate.hbm2ddl.auto" value="validate"/>
<entry key="hibernate.dialect" value="${db.dialect}"/>
<entry key="hibernate.default_schema" value="${db.schema}"/>
<entry key="hibernate.format_sql" value="false"/>
<entry key="hibernate.show_sql" value="true"/>
<entry key="hibernate.connection.SetBigStringTryClob" value="true"/>
<entry key="hibernate.c3p0.min_size" value="5" />
<entry key="hibernate.c3p0.max_size" value="20" />
<entry key="hibernate.c3p0.timeout" value="300" />
<entry key="hibernate.c3p0.max_statements" value="50" />
<entry key="hibernate.c3p0.idle_test_period" value="3000" />
</util:map>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
For switching between DataSources progammatically I use org.apache.commons.dbcp.BasicDataSource
######## UPDATE
Hibernate DEBUG log

JPA packagesToScan not causing package-info.java annotations to be scanned

I am having trouble configuring JPA global type mappings via the package-info.java file, which looks like this:
#TypeDefs({
#TypeDef(
typeClass = MyCustomUserType.class,
defaultForType = MyType.class
)
})
package my.entity.package;
import...
My spring configuration file looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:spring-configured />
<context:annotation-config />
<context:component-scan base-package="my.package"/>
<context:property-placeholder location="classpath:/my.properties" />
<bean id="testDataSource" class="org.apache.commons.dbcp.BasicDataSource"
p:driverClassName="com.ibm.db2.jcc.DB2Driver"
p:url="jdbc:db2://${database.host}:50000/${database.dbname}:currentSchema=I0071DBA;"
p:username="${database.username}"
p:password="${database.password}"
p:initialSize="5"
p:maxActive="10">
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="testDataSource" />
<property name="persistenceProviderClass" value="org.hibernate.jpa.HibernatePersistenceProvider"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect"/>
<entry key="hibernate.default_schema" value="MySchema"/>
<entry key="hibernate.cache.use_query_cache" value="false"/>
<entry key="hibernate.cache.use_second_level_cache" value="false"/>
</map>
</property>
<property name="packagesToScan" value="my.entity.package"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
I believe the packagesToScan property is being used, because all my entity classes are configured into the EntityManagerFactory. Only the package-info.java file is not being used. Any suggestions for configuring the package-info.java file into the EntityManagerFactory?

Get BeanCreationException when try to add Jackson Library

I have a simple Hello World example that passes a Map to Camel and displays the values to the console via Log4J. I want to expand this example to render this map in JSON by adding the Jackson library to my Camel applicationContext.xml
First I tried adding the following XML tags to my applicationContext.xml (as specified at
http://camel.apache.org/json.html under "Using JSON in Spring DSL")
<camel:dataFormats>
<camel:json id="jack" library="Jackson"/>
</camel:dataFormats>
But when I add this to my applicationContext.xml, and run my Java code I get the following XmlBeanDefinitionStoreException message:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'dataFormats'. One of '{"http://camel.apache.org/schema/
spring":route}' is expected.
Moving these tags inside or outside of my camelContext yields the same error (just a longer list of URLs when inside the camelContext).
Is there something else I need to specify in my ApplicationContext.xml?
Here is my current applicationContext.xml:
UPDATED: The following xml now works. Had to move the location of the dataFormats XML tags.
<?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:camel="http://camel.apache.org/schema/spring"
xmlns:context="http://www.springframework.org/schema/context"
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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean
class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<context:component-scan base-package="sample" />
<context:annotation-config />
<camel:camelContext id="HelloWorldContext">
<camel:dataFormats>
<camel:json id="jack" library="Jackson"/>
</camel:dataFormats>
<camel:route>
<camel:from
uri="timer://hello.world.request.timer?fixedRate=true&period=10000" />
<camel:to uri="log:hello.world.request?level=INFO?showAll=true" />
<camel:bean ref="helloWorld" />
<camel:to uri="log:hello.world.response?level=INFO?showAll=true" />
</camel:route>
</camel:camelContext>
<bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="configuration" ref="jmsConfig" />
</bean>
<bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="transacted" value="false" />
<property name="concurrentConsumers" value="1" />
</bean>
<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="vm://localhost" />
<property name="redeliveryPolicy" ref="redeliveryPolicy" />
<property name="prefetchPolicy" ref="prefetchPolicy" />
</bean>
<bean id="prefetchPolicy" class="org.apache.activemq.ActiveMQPrefetchPolicy">
<property name="queuePrefetch" value="5" />
</bean>
<bean id="redeliveryPolicy" class="org.apache.activemq.RedeliveryPolicy">
<property name="maximumRedeliveries" value="1" />
<property name="backOffMultiplier" value="2" />
<property name="initialRedeliveryDelay" value="2000" />
<property name="useExponentialBackOff" value="true" />
</bean>
</beans>
The dateFormats and json elements are part of the camel namespace. You need to specify that
<camel:dataFormats>
<camel:json id="jack" library="Jackson"/>
</camel:dataFormats>

Defining enum maps in Spring beans

I am trying to define an enum map in my Spring beans xml, and I want it to populate in the xml, however when I try to define it like this
<bean class = "java.util.EnumMap">
<constructor-arg>
<util:map key-type="org.itemlist.products.stockitem">
<entry key="stockitem.SOAP">100</entry>
</util:map>
</constructor-arg>
UPDATE
Here is my beans configuration
<?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: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-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean class = "java.util.EnumMap">
<constructor-arg>
<util:map key-type="org.itemlist.products.stockitem">
<entry key="stockitem.SOAP">100</entry>
</util:map>
</constructor-arg>
</bean>
</beans>
When I add a value inside entry, this is now the error
cvc-complex-type.2.3: Element 'entry' cannot have character [children], because the type's content type is element-only.
Have you define those schema in the header:
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
And the namespace:
xmlns:util="http://www.springframework.org/schema/util"
You can define it like this :
<bean id="springBean" class="a.b.c.d.MyEnum">
<constructor-arg>
<util:map key-type="a.b.c.d.MyEnum" value-type="aa.b.c.d.Mya.b.c.d.MyVal">
<entry>
<key><value type="a.b.c.d.MyEnum">ENUM-VAL1</value></key>
<ref bean="myValBean1" />
</entry>
<entry>
<key><value type="a.b.c.d.MyEnum">ENUM-VAL1</value></key>
<ref bean="myValBean1" />
</entry>
</util:map>
</constructor-arg>
</bean>
That error message means that the entry element must be empty, i.e. contain no text or other elements. The syntax you want is:
<entry key="stockitem.SOAP" value="100"/>
The entry element also lets you pass a reference to another bean as the value, e.g.:
<entry key="stockitem.SOAP" value-ref="myOtherBean"/>
(which is of no use in your situation, I just mention it for completeness)

Categories