Loading Schema in Spring XML - spring

I'm trying to load an XSD file as a Schema instance in my application context XML file. We're building it in java code, but I'd like to be injecting it.
Here's what the user class looks like if that'll clarify things.
class XmlBuilder {
...
Schema schema; // set by spring
public String createXml(Object param1) {
// create xml
Validator validator = schema.newValidator();
try {
validator.validate(documentSS);
} catch (SAXException e) {
// log and convert to a proper exception
}
return xml;
}
}
The schema is in the app's classpath, I'm just having trouble loading it and creating the schema object. I can get this far
<bean id="schemaFactory" class="javax.xml.validation.SchemaFactory" factory-method="newInstance">
<constructor-arg value="http://www.w3.org/2001/XMLSchema"/>
</bean>
<bean id="xmlBuilder" class="XmlBuilder">
<property name="schema">
<bean factory-bean="schemaFactory" factory-method="newSchema">
<!-- missing bit -->
</bean>
</property>
</bean>
But I'm stuck on loading the file and passing it into the factory method.

I ended up using the ResourceSource class in 'org.springframework.ws:spring-xml'
<property name="schema">
<bean class="org.springframework.xml.transform.ResourceSource">
<constructor-arg value="classpath:/schema.xsd"/>
</bean>
</property>

Related

Converting bean from spring to spring boot

My problem statement is as follows :
I want to connect to db and from a table get certain values and assign those to a bean. In current spring project, it is done as follows :
The class com.some.DbPropertyPlaceholderConfigurer is in a jar file.
<bean id="propertyConfigurer"
class="com.some.DbPropertyPlaceholderConfigurer">
<property name="dataSourceName" value="dataSource" />
<property name="locations">
<list>
<value>classpath:resources/context.properties</value>
</list>
</property>
</bean>
<bean id="someObject" class="com.some.beans.SomeObject">
<property name="someprop" value="${prefix.someprop}" />
<property name="someprop1" value="${prefix.someprop1}" />
<property name="someprop2" value="${prefix.someprop2}" />
....
</bean>
DbPropertyPlaceholderConfigurer is in a jar file, it reads from a db table.
The Db table has key value columns.
The key contains "prefix.someprop" and it has some value.
Now, I am working on converting spring beans into spring boot beans using annotations.
Following is the bean in spring.
I came up with following for spring boot but bean it is not getting initialized.
I am not sure how to map properties "dataSourceName" and "locations"
#Configuration
public class AppConfig {
#Bean
public DbPropertyPlaceholderConfigurer propertyConfigurer() {
return new DbPropertyPlaceholderConfigurer();
}
#Bean
public SomeObject someObject() {
return new SomeObject();
}
}
It throws following exception :
[localhost-startStop-1] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.lang.NullPointerException
at com.some.configuration.DbPropertyPlaceholderConfigurer.processProperties(DbPropertyPlaceholderConfigurer.java:130)
Also if it gets initialzed, how will I actually use it ?

Reading message from properties file in spring4

I am facing an issue in reading the properties file in spring mvc4. To read messages I have added following in spring-servlet.xml file located under WEB-INF folder.
<context:component-scan base-package="com.test.restful.producer" />
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="location">
<value>classpath:application.properties</value>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
In my controller class,
#Value("${MSG}")
private String msg;
i am getting msg as null. Please helpout me how to load properties file. My appilcation.properties file is available in classpath only.
I am using Spring-4.0.5
Thank you
You can try this xml for creating properties bean.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:propertyFile.properties" name="propertiesBean"/>
Or go for non xml version as below
#PropertySource("classpath:propertyFile.properties")
public class AppConfig {
#Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}

Groovy and spring under same transaction

I'm struggling with legacy code. I'm creating unit tests so I've decided to use groovy to fill database with required legacy data. Normally in my code I using ibatis for persistence. I'd like to rollback test in the end. Problem is that when I create row via groovy then I use it's id to create row via ibatis I get constraint violation exception - parent key not found.
When I use groovy to persist parent and than create child based on parents id it works perfectly fine.
Also I can't use #Transactional because of problems with XML parser (legacy code FTW :/ )
#ContextConfiguration(locations = [ "../dao/impl/ibatis/spring-data-context-config.xml", "classpath:/pl/com/betacom/treq/dao-context.xml"])
#RunWith(SpringJUnit4ClassRunner.class)
public class FinancingForIltCreationTest {
#Autowired
IFinancingForIltDAO financingForIltDAO;
#Autowired
Sql sql;
#Autowired
DataSourceTransactionManager transactionManager;
private TransactionStatus transactionStatus;
#Before
public void setUp() {
transactionStatus = transactionManager.getTransaction(new DefaultTransactionDefinition());
}
#After
public void tearDown() {
transactionManager.rollback(transactionStatus);
transactionStatus = null;
}
#Test
public void shallCreateFinancingForIlt() throws Exception {
//given
IltOffering offering = new IltOffering("GOING_DOWN_TO_UBERGROUND", offeringTempId, java.sql.Date.valueOf("2011-07-21"), java.sql.Date.valueOf("2012-07-21"));
offering.insert(sql); // it's inserted by groovy
//when
FinancingForIltDTO financingForIltDTO = createFinancingForIlt(offering.id).build(financingForIltDAO); // it's my assembler inserting via iBatis
//then
assertNotNull(financingForIltDTO.id);
}
Configuration looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSourceIn"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>####</value>
</property>
<property name="url">
<value>####</value>
</property>
<property name="username">
<value>####</value>
</property>
<property name="password">
<value>####</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<constructor-arg ref="dataSourceIn" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<bean id="sql" class="groovy.sql.Sql">
<constructor-arg ref="dataSource" />
</bean>
Unfortunately it was a database schema issue.

Ant task for Spring Validation

I need an ANT task to validate spring configuration. I need to find problems at build time before runtime ? For example, In spring context file contains a property a bean, but this bean doesnt have this property.
In eclipse, there is a tool Spring Explorer that do this validation.
thanks,
org.springframework.web.context.ContextLoaderListener failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'readController' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'productOperations' of bean class [com.bee.view.json.ReadController]: Bean property 'productOperations' is not writable or has an invalid setter method.
Does the parameter type of the setter match the return type of the getter?.
An easy way to ensure that your context is valid would be to create a JUnit test, which loads the context. Using the spring-test.jar support classes makes that easy:
public class MyTest extends AbstractDependencyInjectionSpringContextTests {
// this will be injected by Spring
private QueryDao queryDao;
private MyBusinessObject myBusinessObject;
// ensure that spring will inject the objects to test by name
public MyTest () {
setAutowireMode(AUTOWIRE_BY_NAME);
}
#Override
protected String[] getConfigLocations() {
return new String[] { "applicationContextJUnit.xml" };
}
public void testQueryDao() {
List<SomeData> list = queryDao.findSomeData();
assertNotNull(list);
// etc
}
public void testMyBusinessObject() {
myBusinessObject.someMethod();
}
public void setQueryDao(QueryDao queryDao) {
this.queryDao = queryDao;
}
}
The problem with loading a context that is used in a web application is that JUnit does not necessarily have access to the same resources (e.g. JNDI data sources), so if you've got the following in your "applicationContext.xml":
<beans ...>
<bean id="myBusinessObject" class="com.test.MyBusinessObject">
<property name="queryDao" ref="queryDao"/>
</bean>
<bean id="queryDao" class="com.test.QueryDao">
<property name="dataSource" ref="dataSource"/>
</bean>
<jee:jndi-lookup
id="dataSource"
jndi-name="jdbc/mydatasource"
resource-ref="true"
cache="true"
lookup-on-startup="false"
proxy-interface="javax.sql.DataSource"/>
</beans>
and your "applicationContextJUnit.xml" would import your "real" application context and redefine resources:
<beans ...>
<import resource="classpath:applicationContext.xml"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:..."/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</bean>
</beans>
That way your unit tests will load the application context (even the ones that you don't explicitly test in your unit test), and you can have the confidence that your context is correct, because Spring itself loaded it. If you have an error, then the unit tests will fail.

how to configure java.xml.transform.transformer in spring

How can i configure java.xml.transform.Transformer in spring framework ? I need instance of transformer to transform xml to text via xslt. Hence it is critical that the configured transformer should have knowledge of xslt stylesheet. I am not using this in a web project, rather i am using this in a standalone java program.
Well, the Java to configure a Transformer is like this:
Source stylesheetSource = new StreamSource(new File("/path/to/my/stylesheet.xslt"));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(stylesheetSource);
If you really want to do this purely in Spring XML, this is the equivalent:
<bean id="stylesheetSource" class="javax.xml.transform.stream.StreamSource">
<property name="systemId" value="/path/to/my/stylesheet.xslt"/>
</bean>
<bean id="transformerFactory" class="javax.xml.transform.TransformerFactory" factory-method="newInstance"/>
<bean id="transformer" factory-bean="transformerFactory" factory-method="newTransformer">
<constructor-arg ref="stylesheetSource"/>
</bean>
You're going to need to get an instance of a Transformer from an appropriate TransformerFactory. You can use the built-in xerces transformer factory or a 3rd party XSLT processor, like saxonica.
Using Spring's IoC you can configure a Xerces XML transformer like this:
<bean id="transformerFactory" class="org.apache.xerces.jaxp.SAXParserFactoryImpl" />
or a saxon XML transformer like this:
<bean id="transformerFactory" class="net.sf.saxon.TransformerFactoryImpl" />
Once you have a TransformerFactory you can use dependency injection to get a new instance of a transformer either inside your bean or using the IoC. Switching to be inside your class you might have some property, say transFact that you need to set:
<bean id="myBean" class="myClass">
<property name="transFact" ref="transformerFactory" />
</bean>
Then in your class:
public class myClass {
// ...
private javax.xml.transformer.TransformerFactory transFact;
public void myMethod(){
StreamSource transformerStream = new StreamSource(getResourceAsStream(pathToXslt));
javax.xml.transformer.Transformer myTrans = transFact.newTransformer(transformerStream);
// now you've got a myTrans
}
// ...
public setTransFact(javax.xml.transformer.TransformerFactory transFact){
this.transFact = transFact;
}
}
Alternatively you can get a new transformer within IoC using the factory-method with a little more effort.
I found this way to provide xsl file from the classpath:
<bean id="errorLogTransformer" factory-bean="transformerFactory" factory-method="newTransformer" >
<constructor-arg>
<bean class="org.springframework.xml.transform.ResourceSource">
<constructor-arg>
<value>classpath:errorLog.xsl</value>
</constructor-arg>
</bean>
</constructor-arg>
</bean>
Pretty ugly, but it works!

Resources