Apache Camel -- Reference type constants in Spring DSL - spring

I'm trying to define a publish operation to Hazelcast topic using Spring DSL
<from uri="direct:inbound" />
<onCompletion>
<log message="onCompletion:- ${body}" />
<setHeader headerName="${type:org.apache.camel.component.hazelcast.HazelcastConstants.OPERATION}">
<simple>${type:org.apache.camel.component.hazelcast.HazelcastConstants.PUBLISH_OPERATION}</simple>
</setHeader>
<to uri="hazelcast:topic:foo" />
</onCompletion>
<log message="${body}" />
The above route works, but I have to use long SIMPLE scripts like "${type:org.apache.camel.component.hazelcast.HazelcastConstants.OPERATION}" to ref a constant value. Is there any simpler or short form for this?
I tried to define a spring bean for HazelcastConstants class and ref it through SIMPLE scripts as below but it's not working with MethodNotFoundException "Method with name: OPERATION not found on bean"
<bean id="hazelcastConstants" class="org.apache.camel.component.hazelcast.HazelcastConstants" />
... ...
<simple>${bean:hazelcastConstants.OPERATION}</simple>

Your bean workaround would work, if you defined a bean contained a method returning the constant in question, e.g.:
public class ContantRetriever() {
public String getHazelCastOperation() {
return org.apache.camel.component.hazelcast.HazelcastConstants.PUBLISH_OPERATION;
}
}
Your Spring context:
<bean id="hazelcastConstants" class="yourpackage.ContantRetriever"/>
<simple>${bean:hazelcastConstants.getHazelCastOperation}</simple>
If that is no good for you, I am afraid you are stuck with the long form of accessing constants.

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;
}

Spring: Setting a property annotated with #Resource

This following does not behave like I expect, using Spring 3.2.1.RELEASE, groovy 1.8.8. I've elided quite a bit of code. If this isn't sufficient, I'll put together a (not) working full example.
groovy bean:
package foo
import javax.annotation.Resource
class SomeBean {
#Resource
String someProp
}
spring xml:
<context:annotation-config />
<context:component-scan base-package="other.packages.not.foo" />
<bean id="someBean" class="foo.SomeBean">
<property name="someProp" value="bar" />
</bean>
This fails with an error about not being able to find a bean to satisfy the String property. To work around this, I ended up doing this:
groovy bean:
#Resource( name = 'someProp' )
String someProp
spring xml:
<bean id="someProp" class="java.lang.String">
<constructor-arg value="bar" />
</bean>
<bean id="someBean" class="foo.SomeBean" />
I don't like this solution, because it couples the bean source code to the spring config, and that's coupling in the wrong direction. And it creates an unnecessary bean for a simple String. Am I missing an obvious solution?
#Resource is not meant for property injection.
For this to work:
<bean id="someBean" class="foo.SomeBean">
<property name="someProp" value="bar" />
</bean>
you need to implement a setter method
public void setSomeBean(String value) {
someValue = value;
}
#Resource could be used if you want to inject a different bean from your context.
#Resource(name="someBean")
SomeBean someBean

Can I combine #controller and XML bean mapping in spring?

I currently have a #Controller declared in spring and have a bunch of mappings done like so:
#RequestMapping(value = "foo", method = RequestMethod.GET)
public ModelAndView foo() {
ModelAndView mav = new ModelAndView(
"myjsp");
return mav;
}
However every time I want to add a simple JSP mapping I need to recompile and build a new war and deploy.
This isnt so bad except sometimes other members of the team have requests and it would be easier if they can just go into the test env and create the mapping themselves without having to recompile.
I know that you can do similar mapping using xml but can I do this at the same time that I have the #Controller defined?
Like in the example above how could I define that mapping in XML rather than in java?
or say I needed foo2 to map to myjsp2.jsp
I am using spring MVC 3.2
Look into BeanNameUrlHandlerMapping which allows you specify url patterns for controllers in your configuration. Documentation
Example
<beans>
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name="/editaccount.form" class="org.springframework.web.servlet.mvc.SimpleFormController">
<property name="formView" value="account"/>
<property name="successView" value="account-created"/>
<property name="commandName" value="account"/>
<property name="commandClass" value="samples.Account"/>
</bean>
<beans>

How to get header value in bean method

I have custom bean which i use it in spring integration flow. It has method which takes hashmap as argument and its values are set dynamically. I am trying to set the value from the payload header field(iploc) but i am not able to achieve so. I tried some combination of spel but it not work. Any pointers?
<int:transformer id="ws.transformer"
input-channel="ws.transformer.in" output-channel="ws.transformer.out">
<bean class="com.my.Mybean">
<property name="map">
<map>
<entry key="user">
<value>"admin"</value>
</entry>
<entry key="location">
<value>"headers['iploc']"</value>
</entry>
</map>
</property>
</bean>
</int:transformer>
I can alternatively set the value in Service activator, but i am trying if i achieve this in the SI config itself.
Spring Integration Transformer can consume entire Message (payload and headers) so there is no point in passing header value by property since transformer already has access to all message headers.
Your transformer bean definition should contain only these properties that do not come from the Message being transformed:
<int:transformer id="ws.transformer" input-channel="ws.transformer.in" output-channel="ws.transformer.out">
<bean class="com.my.MyTransformer">
<property name="user" value="admin"/>
</bean>
</int:transformer>
And your transformer method:
#Transformer
OutgoingPayload transform(IncomingPayload payload, #Header("iploc") String iplocHeader) {
return doTransform(...);
}
or just consume entire message:
Message<OutgoingPayload> transform(Message<IncomingPayload> message) {
final String ipLocHeaderValue = message.getHeaders.get("iploc", String.class);
return doTransform(...);
}
The bean defined within a transformer like that will be instantiated at startup time, not each time a message is received. The normal way to handle a requirement like this is to have a stateless bean with a method that accepts the header value upon each invocation.

Spring Injection can not find variable in resource file

I have a resource file created in my project. I want to inject values from resource file into spring bean. i defined the place holder for resource file in the applicacationContext.xml.
<context:property-placeholder location="file:///${MRAPOR_PROPPATH}mRapor.properties" />
I can inject values to beans which is declared in the applicationContext.xml like :
<bean
id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean" >
<property
name="jndiName"
value="${database.jndiName}" />
<property
name="lookupOnStartup"
value="false" />
<property
name="cache"
value="true" />
<property
name="proxyInterface"
value="javax.sql.DataSource" />
</bean>
This works well. However, i can not inject values if i declare beans with spring annotations.
#Component("SecurityFilter")
public class SecurityFilter implements Filter {
public static final String USER = "USER_SESSION_KEY";
public static final String CENTRIFY_USERNAME_KEY = "REMOTE_USERNAME";
#Value("${url.logout}")//I get error here !!!!
private String logoutUrl;
//proper setters and getters.
}
Do you have any idea why i can not access values inside the beans declared using annotations.
Here is my exception
weblogic.application.ModuleException:
at weblogic.servlet.internal.WebAppModule.startContexts(WebAppModule.java:1510)
at weblogic.servlet.internal.WebAppModule.start(WebAppModule.java:482)
at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:425)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:52)
at weblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:119)
Truncated. see log file for complete stacktrace
Caused By: java.lang.IllegalArgumentException: Could not resolve placeholder 'url.logout' in string value [${url.logout}]
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:173)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:125)
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:255)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:748)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:745)
Are you sure that the instance of SecurityFilter that actually filters requests is managed by Spring?
By default Filters declared in web.xml are instantiated by servlet container, therefore they are not managed by Spring and Spring annotations such as #Value won't work in them.
However, Spring provides special support for your use case - you can delegate filtering to a component managed by Spring using DelegatingFilterProxy. Declare it in web.xml as follows:
<filter>
<filter-name>SecurityFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>...</filter-mapping>
DelegatingFilterProxy will delegate request filtering to a bean named SecurityFilter (as in your #Component).
The #Value is processed by the java compiler whereas the the XML is parsed by Spring Bean Processor - these are two very different things... Why do you assume that should work in the same manner?
Edit: I read up on it and it seems to actually be possible using the Spring EL, you only have to prefix with # instead of $:
private #Value( "#{application.url.logout}" ) String logoutUrl;
Cheers,

Resources