how to configure java.xml.transform.transformer in spring - 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!

Related

Convert XML bean definition with parent to Java with annotations

I have an application using a framework that provides certain Spring beans via XML files in the framework. The configuration of my application is currently done partly in XML but mostly with Spring annotations.
Some of the XML bean definitions have parents referring to beans supplied by the framework, e.g.
<bean id="MyBean" parent="FrameworkBean">
<property name="context">
<map merge="true">
<entry key="SomeKey" value-ref="SomeValue" />
</map>
</property>
</bean>
FramwworkBean is defined in an XML file in the framework. There is a chain of bean inheritance. At each step some entries are added to the context:
<bean id="FrameworkBean" parent="AbstractBean">
<map merge="true">...
<bean id="AbstractBean" abstract="true" class="ClassWithContext"/>
I understand the result of all this is construction of a ClassWithContext
instance with a map containing all the entries up the chain.
Is it possible to write Java code to do the same, without duplicating code from the framework XML files?
#Bean("MyBean") ClassWithContext myBean() {
return ??? // code that uses "FrameworkBean" somehow
}
The XML bean definition has no dependency on the type of AbstractBean.
If MyBean can be created by Java code, can that code be written to be equally type-agnostic? Or should I just leave this in XML?
If your "FrameworkBean" is not abstract bean you can try the following:
#Bean
public SomeType myBean(#Qualifier("FrameworkBean") FrameworkBeanType frameworkBean) {
SomeType type = getSomeType();
type.setFrameworkBean(frameworkBean);
return type;
}

How would I specify a bean reference as an argument-resolver, in Spring 3-1?

This is a syntax question about a Spring configuration (refer to spring-beans-3.1.xsd and spring-mvc-3.1.xsd).
I have a specific bean definition (id="SecurityRequestParametersArgumentResolver") that I want to register as a custom argument resolver. The xml fragment is:
<bean id="SecurityRequestParametersArgumentResolver"
class="xxx.security.web.SecurityRequestParametersArgumentResolver">
<property name="credentialsManager" ref="CredentialsManager" />
<property name="tokenService" ref="TokenService" />
</bean>
... AND I would like to use a bean reference. The following three lines don't obey the xsd-grammar ( what should be the correct tag declaration here? )
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean ref="SecurityRequestParametersArgumentResolver"/>
</mvc:argument-resolvers>
</mvc:annotation-driven>
... All of the examples I have seen look like THIS, and thus are going after the default no-argument constructor
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="class="xxx.security.web.SecurityRequestParametersArgumentResolver"/>
</mvc:argument-resolvers>
</mvc:annotation-driven>
Repeating the question, what should the Spring syntax be in order to use a bean reference as a custom argument resolver?
Thanks!
At least the constructor argument problem can be solved easily
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="class="xxx.security.web.SecurityRequestParametersArgumentResolver">
<constructor-arg value="123"/>
</bean>
</mvc:argument-resolvers>
</mvc:annotation-driven>
But I do not know any way to use a reference instead of a bean within <mvc:argument-resolvers>. But depending on what you want to do, there are several workarrounds.
Use a Bean Factory
Create a proxy class that implements the HandlerMethodArgumentResolver and forward every call to an other instance, then you can have your own SecurityRequestParametersArgumentResolver like a normal bean and register the Proxy in the <mvc:argument-resolvers> section.
Proxy:
class HandlerMethodArgumentResolverProxy
implements HandlerMethodArgumentResolver{
HandlerMethodArgumentResolver delegate;
publicHandlerMethodArgumentResolverProxy
(HandlerMethodArgumentResolver delegate){
this.delegate=delegate;
}
public boolean supportsParameter(MethodParameter parameter) {
this.delegate.supportsParameter(parameter);
}
//delegate for resolveArgument
}
config:
<bean id="securityRequestParametersArgumentResolver"
class="xxx.security.web.SecurityRequestParametersArgumentResolver">
...
</bean>
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class "HandlerMethodArgumentResolverProxy">
<constructor-arg
ref="securityRequestParametersArgumentResolver"/>
</bean>
</mvc:argument-resolvers>
</mvc:annotation-driven>

Loading Schema in Spring XML

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>

Is there a better way to Configure Bean?

Can the following Spring DI xml be improved? Below the xml is the programmatic approach of configuring the target bean.
<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />
<bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig"
factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" />
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="jacksonSerializationConfig" />
<property name="targetMethod" value="setSerializationInclusion" />
<property name="arguments">
<list>
<value type="org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion">NON_NULL</value>
</list>
</property>
</bean>
ObjectMapper mapper = new
ObjectMapper();
mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
XML is a really bad way of doing this. Yes, you can do this, but it's much easier to write a FactoryBean which configures your ObjectMapper:
public class MyObjectMapperFactoryBean extends AbstractFactoryBean<ObjectMapper> {
public Class<ObjectMapper> getObjectType() {
return ObjectMapper.class;
}
public ObjectMapper createInstance() {
// create and return ObjectMapper
}
}
and then in your XML:
<bean id="jacksonObjectMapper" class="x.y.MyObjectMapperFactoryBean" />
Still not totally ideal, but a little cleaner:
<bean id="objectMapperBuilder1" class="org.codehaus.jackson.map.ObjectMapper"/>
<bean id="objectMapperBuilder2" factory-bean="objectMapperBuilder1" factory-method="setSerializationInclusion">
<constructor-arg value="NON_NULL"/>
</bean>
<bean id="jsonWriter" factory-bean="objectMapperBuilder2" factory-method="writerWithDefaultPrettyPrinter" />
<!-- etc, etc -->
One downside is that you'll have unnecessary bean instances in memory. (I'm using this method, and I'll live with it until Spring decides to handle these). There are many threads here and on the Spring forums asking for support with fluent setters like the builder pattern used by Jackson, but until then you have to choose the lesser evil for you.
I agree with #skaffman's general approach of using a FactoryBean in place of the unavoidably convoluted Spring XML bean configuration to configure a Jackson ObjectMapper. Spring 3.2+ now provides such a FactoryBean out of the box. See JacksonObjectMapperFactoryBean / Jackson2ObjectMapperFactoryBean. Here's an example of the Spring XML to configure the ObjectMapper via the Spring FactoryBean -
<bean id="jacksonObjectMapper" factory-bean="&jacksonObjectMapperFactoryBean" factory-method="getObject"/>
<bean id="jacksonObjectMapperFactoryBean" class="org.springframework.http.converter.json.JacksonObjectMapperFactoryBean">
<property name="featuresToDisable">
<array>
<util:constant static-field="org.codehaus.jackson.map.SerializationConfig$Feature.WRITE_NULL_PROPERTIES"/>
</array>
</property>
</bean>
(Note the need to use &amp in the 'factory-bean' attribute to instruct Spring to use the factory method on the FactoryBean itself, rather than the bean it creates).

Reading valued from properties file at Runtime

I want to get specific value based on request from the property file.how to do that?
I have following spring configuration.i want to set the value for Exprops as per the request and get corresponding values from the properties file
<bean id="Prop" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:ErrorMessage.properties</value>
</property>
</bean>
<bean id="PropertiesBean" class="com.util.PropertiesUtil">
<property name="Exprops" value="${EXampleExceptiion}"></property>
</bean>
Use the PropertiesFactoryBean to inject the Properties in a Bean.
<bean id="myPropertiesBean"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:ErrorMessage.properties"/>
</bean>
This provides a Properties Object/Bean which can be injected under the name myPropertiesBean in any Bean (<property name="x" ref="myPropertiesBean"/>).
In addition Spring provides the util namespace (since Spring 2.5):
There you can write the PropertyFactoryBean definition a bit shorter:
<util:properties id="myPropertiesBean"
location="classpath:ErrorMessage.properties"/>
#see Spring Reference Chapter C.2.2.3.
All use the following for doing this programmatically
XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource("jdbc.properties"));
cfg.postProcessBeanFactory(factory);
<util:properties id="" location="location of prop file" />
this return java.util.Properties object

Resources