Writing a custom Spring #Cacheable annotation in Spring 3 - spring-3

I'm currently writing a custom #Cacheable annotation that will take additonal meta-data attributes in addition to those that Spring's #Cacheable provides. However, Spring would need to know how to parse this new annotation. My understanding is that I'd need to implement my own annotation parser by extending CacheAnnotationParser and inject it to AnnotationCacheOperationSource.
But by default AnnotationCacheOperationSource is using springcacheannotationparser, even i try to inject my custom parser as constructor injection to AnnotationCacheOperationSource , which doesn't work.
Can anyone please help on this?

Related

How to override default spring integration configuration

For example:
xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"
<int-jdbc:inbound-channel-adapter.......
I want this to use my custom implementation, so i need to know
1) What class it uses (so i can extend it and override whatever)
2) than how do i tell it to use my new class rather than the default one.
is this possible?
Thanks
There is currently no way to inject your own implementation using the XML namespace, but you can simply wire up <bean/> definitions. After all, that's all that the namespace parser does.
In this case, you need a bean of class SourcePollingChannelAdapterFactoryBean which needs PollerMetadata and a source (and creates a SourcePollingChannelAdapter), with the source being a bean of class JdbcPollingChannelAdapter (which is a MessageSource).
So you would subclass the JdbcPollingChannelAdapter.
If you think your implementation will have wide appeal, please open a JIRA issue and consider contributing it.

Hyperjaxb/lombok : how to force Hyperjaxb generate annotations mapping on attributes and not on the methods?

I would like jpa annotations on my attributes, not on getter and setter. It is possible to configure hyperjaxb for this ?
I want this because I use lombok in order to avoid getter and setter code and have "#getter" "#setter" above attributes.
thank you
This is not supported out of the box at the moment (please file an issue if you need this functionality).
However you can do it by writing and overriding your own implementation of org.jvnet.hyperjaxb3.ejb.strategy.annotate.AnnotateOutline. See https://svn.java.net/svn/hj3~svn/trunk/ejb/tests/custom-naming/ for an example of overriding a strategy.
But be aware that Hyperjaxb has to use getters/setters to workaround some of the JAXB/JPA incompatibilities (for example simple type which is not supported by JPA). If you put annotations on fields, this won't work.

JAXB : Is the annotation #XmlAccessorType is only for Serialization and nothing to do with Binding of data?

I wanted to know why do we need to specify the Annotation #XmlAccessorType when working with JAXB .
When i googled for this i found out this description from a website stating this
#XmlAccessorType sets default field and property serializability. By default, JAXB serializes public fields and properties. By setting #XmlAccessorType, the bean can choose to only allow annotated fields to be serialized.
Here the author mentions that with this annotation it gives control on serialization .
My question is , so #XmlAccessorType has nothing to do with the JAXB Binding and Unbinding from XML to java and java to XML , and it is all about Serialization only .
JAXB's #XmlAccessorType annotation is only used by JAXB (JSR-222) implementations for determining how to marshal a file to/from XML:
Normally the main decision to be made is between FIELD & PROPERTY/PUBLIC. FIELD is particularly useful when you have logic in your get/set methods that you do not want triggered during marshalling/unmarshalling. To see one way this choice affects the mapping metadata see:
http://blog.bdoughan.com/2012/02/jaxbs-xmltype-and-proporder.html
NONE is a useful choice when you have many unmapped properties and you want to tell your JAXB implementation to only map the fields/properties you have annotated. This can be alot easier than adding a lot of #XmlTransient annotations into your model.
Fore More Information
http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html

use Jackson JSON library with JAXB annotations

I'm trying to enable JAXB annotation support within my RESTEasy based web application and this article was suggested to me (http://wiki.fasterxml.com/JacksonJAXBAnnotations). I'm able to get the jackson-xc.jar but I don't see how to register the annotation introspector. Currently, RESTEasy automatically serializes my JSON responses, where would the below fit? Currently, RESTeasy serializes the JSON object automatically.
Include jackson-xc jar, which contains org.codehaus.jackson.xc.JaxbAnnotationIntrospector
Register this annotation introspector
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
// make deserializer use JAXB annotations (only)
mapper.getDeserializationConfig().setAnnotationIntrospector(introspector);
// make serializer use JAXB annotations (only)
mapper.getSerializationConfig().setAnnotationIntrospector(introspector);
What you need to figure out is how to make RESTeasy use ObjectMapper you have configured. JAX-RS offers generic way to do this via providers, but there may be simpler way(s) to do this with RESTeasy.
Some JAX-RS implementations also register JAXB annotation introspector by default.
Last thing: are you sure you need to use JAXB annotations? While Jackson can use them, preferable method is to either just use basic naming convention for discovery and jackson's own annotations in case where overrides are needed. Problem with JAXB is that it is xml-specific so there is little bit of impedance when used with JSON.
Thanks for the reply, I'll take a look at your suggestions...
The reason why we want to stick with the jaxb annotations is that the #XmlRootElement(name="userResponse") annotation allows an extra wrapping element that we want in our JSON responses. See example below:
We want to have {"response":{"userResponse":[{"user":{"businessRegion":"US","firstName":"joe","language":"en","lastName":"smith"}}}]} instead of what Jackson currently outputs, {"response":[{"user":{"businessRegion":"US","firstName":"joe","language":"en","lastName":"smith"}}]}. Is there anyway to mimic the #XmlRootElement in Jackson without adding an additional wrapper class?
User class:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name="userResponse")
public class UserResponse extends AbstractResponse{
private Users user;

How jaxb marshaller and unmarshallers uses generated classes

I have generated classes using wsimport/wsconsume for my webservice. I am using JAXB marshaller and unmarshaller support. Can any one tell me how these marshaller/unmarshaller uses gnereated classes? say it is geneated - requestFile,Response File,service,ServiceSoap,ObjectFactory, Package-info. Here Service is name of my webservice.
Any help is greatly appreciated.
JAXB uses the annotations on the generated classes to reflectively marshal/unmarshal XML documents. It tries to use sensible defaults if the annotations are missing.
package-info.java is a weird Java5 thing, which allows you to declare package-level annotations. JAXB uses this is do things like declare package-wide type converters, or XML namespaces.
ObjectFactory is a generate class that provides factory methods for the various JAXB types. You usually don't need to use that yourself.
The generated service classes are used by the JAX-WS runtime to assemble the JAXB-bound model objects into actual SOAP requests and responses.

Resources