Java Spring configuration and Resteasy - spring

I am trying to integrate Resteasy and Spring; I have followed both the docs for Resteasy and this post: Inject Spring beans into RestEasy .
I have it working using #Autowire or other Spring annotations on the rest class, but I would like to do this keeping my rest classes free from spring (or DI) dependencies.
I also would like to configure spring only through java configuration. In spring configuration I added this:
<context:component-scan base-package="package.where.spring.configuration.beans.are , package.where.rest.classes.are">
<context:include-filter type="annotation" expression="javax.ws.rs.Path"/>
</context:component-scan>
and of course I have in web.xml, so that spring config is picked up by SpringContextLoaderListener:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-config.xml</param-value>
</context-param>
Removing the #Autowire annotations, if I also remove the first package (where I configured the injections through Spring java config) no injection takes place and the fields remain null; if I remove the second package the urls for the rest classes are not recognized by resteasy.
I would like to configure the injections just in Spring configuration, is there a way to have resteasy recognize the paths from spring beans which are configured externally?
EDIT: I noticed that what I am trying to do works with #Provider annotated classes, given that you configure Spring correctly:
<context:component-scan base-package="my.package1 , my.package2">
<context:include-filter type="annotation" expression="javax.ws.rs.ext.Provider"/>
</context:component-scan>
But the mistery is deeper than I first thought... I am more confident that I am on the right track and just missed a step!

A much better approach is to use JSR-330 annotations.
Instead of #Autowired, prefer using #Inject. Spring supports JSR-330 annotations and will use Autowire implementation under the covers. For your Spring beans annotated with #Component, #Service, simply replace the annotation with JSR-330 specific #Named.
If you're using maven, simply include the following in your pom.xml file.
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>

Using JSR-250's #Resource instead of #Autowired is a big step forward; it does what is needed from #Autowired and it is JSR and not Spring specific.
Could be good enough for many uses.

Related

Is there an annotation equivalent to the Spring AOP aop:include configuration for autoproxies

When specifying the XML configuration below:
<aop:aspectj-autoproxy>
<aop:include name="myBean" />
</aop:aspectj-autoproxy>
We all know that the #EnableAspectJAutoProxy annotation is the equivalent to the aspectj-autoproxy XML configuration but is there a java-based annotation equivalent for the aop:include XML configuration? I searched extensively and could not find.
Normally you tell Spring that you are using a particular feature, like Transaction management and it will create the proxies needed.
For instance #EnableTransactionManagement will cause Spring to create proxies for Components (services, controllers and repositories) which use #Transactional, you don't need to declare this, Spring automatically finds the beans that need to be proxied.
It works the same way with #EnableScheduling causing Spring to detect #Scheduled methods, and #EnableCaching to detect #Cached method.

Spring AOP not working on all annotation methods

I have created a custom annotation in my spring mvc project.
The annotation is used to do an AOP
#Around("execution(#Cached * * (..)) && #annotation(cache)")
Here the annotation that I have created is "Cached", any method with the annotation is cached in couch base with the response as its value and the method argument as its key.
The problem is the annotation works (AOP works) on the controllers well. However from controllers, I am making call to different callable classes and utils. When I add the annotation" #Cached" on the callable classes or the util funcations the AOP doesn't work.
In the XML file, the following is what I have declared.
<aop:aspectj-autoproxy/>
<context:spring-configured/>
<context:component-scan base-package="com.abc.xyz">
<!--<context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>-->
</context:component-scan>
<bean id="universalController" class="com.abc.xyz.misc.UniversalController"/>
<bean class="com.abc.xyz.api.metric.SystemTiming"/>
<bean class="com.abc.xyz.api.annotations.URLCacheImpl"/>
With Spring AOP, your classes which match the pointcut (where you have placed your #Cached annotation in this specific case) should be Spring beans. So the best guess that I can make is that your utility classes are very likely not Spring beans and that is reason why they are not getting woven in. You have two options that I can think of:
Make your utility classes also clean Spring beans
Use full Aspectj support - this way even though your utility classes are not Spring beans they would be woven with the advice.

Resteasy and Spring integration without Spring ContextLoadListener

I am using Resteasy and Spring for my project. As Resteasy document said: http://docs.jboss.org/resteasy/docs/3.0.1.Final/userguide/html_single/index.html#RESTEasy_Spring_Integration.
I need to add a listener in the web.xml file:
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
<listener>
<listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
However, in my project we also used a cms called magnolia, and magnolia also has an implementation for SpringContextLoaderListener. If I put both context listener. The magnolia won't be started when I run the app.
So according to the Resteasy document said:
If you do not use a Spring ContextLoaderListener to create your bean factories, then you can manually register the RESTeasy BeanFactoryPostProcessor by allocating an instance of org.jboss.resteasy.plugins.spring.SpringBeanProcessor. You can obtain instances of a ResteasyProviderFactory and Registry from the ServletContext attributes org.jboss.resteasy.spi.ResteasyProviderFactory and org.jboss.resteasy.spi.Registry. (Really the string FQN of these classes). There is also a org.jboss.resteasy.plugins.spring.SpringBeanProcessorServletAware, that will automatically inject references to the Registry and ResteasyProviderFactory from the Servlet Context. (that is, if you have used RestasyBootstrap to bootstrap Resteasy).
Does anyone knows how can I achieve that without using Resteasy ContextLoaderListener? What do I need to put in my spring applicationContext xml file?
I've also had problem with RestEasy SpringContextListener (properties placeholders ${...} weren't processed - RESTEASY-787, Spring java config didn't work etc.).
So it's enough to drop RestEasy SpringContextListener and use default org.springframework.web.context.ContextLoaderListener or whatever listener you need to. You just have to define Spring bean in your Spring XML configuration like this:
<bean class="org.jboss.resteasy.plugins.spring.SpringBeanProcessorServletAware"/>
Than RestEasy should work even without their special SpringContextListener. It works for me.

JSF beans and serializability issue

I have problem with JSF beans using Spring managed services. I got an error telling, that spring bean used in JSF bean is not serializable.
#ManagedProperty("#{customerService}")
private CustomerService customerService;
I can't make the service serializable, because it is using JdbcTemplate which itself isn't serializable. Moreover, serializing Spring beans which have application scope makes no sense at all, so I don't understand, why someone's code is attempting to serialize them.
I have worked with JSF project using Spring services, and there were no such issues, so such cooperation must be possible. But this project is made from scratch based on example projects, so there must be something wrong with the configuration of spring-JSF cooperation, but I don't know where to search.
The configuration of Spring for JSF is:
<!-- JSF and Spring are integrated -->
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
How to solve this issue?
There is no way to avoid JSF serialization mist. Even ApplicationScoped beans are serialized (when they are injected into other beans).
But the solution was made on the Spring side. You have to use scoped proxy.
To wrap the bean into serializable proxy you have to add to bean body:
<aop:scoped-proxy proxy-target-class="true"/>
The spring aop namespace and spring-aop dependency must be added.
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
And this is it! In the bean will be the serializable element, the proxy that will re-load bean from Spring context on deserialization.
The only mist here is that I have to create cglib class-level-proxy. JRE proxy was not working because the interface was not available during deserialization... I don't understand fully why but I have working solution at least.

Autowire Annotation in Spring without using Component Scanning

Is it possible to autowire beans using the #Autowired annotation without using component scanning?
Yes. <context-component-scan .. /> is responsible for discovering beans annotated with #Component, #Controller, #Service, #Respository, etc.
In order to have annotations processed (#Autowired, #Resource, etc) you need <context:annotation-config />. Thus annotations are processed on beans that are listed in applicationContext.xml.
As far as I know, <context-component-scan .. /> activates <context:annotation-config /> automatically.
This is true for both spring 2.5 and 3.0. (thanks skaffman)
I have never tried without component-scanning enabled, however I can confirm that #Autowire annotations works in Spring 3.0.x even with beans that are defined via XML.
When using AnnotationConfigApplicationContext, annotation config processors are always registered, meaning that any attempt to disable them at the #ComponentScan level would be ignored.
If it is meant in the question that you should explicitly state:
- <context:component-scan ...> in your xml file(it enables <context:annotation-config />)
or
- #ComponentScan in your java config
Then the answer is - Yes, it is possible to enable component scanning without any of the stated above statements in your code or xml file.
Another approach is to use AnnotationConfigApplicationContext :
AnnotationConfigApplicationContext context=
new AnnotationConfigApplicationContext("org.example.your.package");
Where "org.example.your.package" is your package for stereotyped annotated classes: #Component, #Repository, #Service, etc.
AnnotationConfigApplicationContext will search for your beans in the base package and inner packages.
No, we must use #ComponentScan if you are using java based configuration
(or) <context-component-scan .. /> for xml based configuration.
Note: If you are not using any of the approaches no corresponding instances are created in AplicationContext.
and when you try to access a resource (http://localhost:8080/customers) will end up with
WARNING: No mapping found for HTTP request with URI [/customers] in
DispatcherServlet with name 'dispatcher

Resources