Best practise - Struts2 / Spring Integrated application startup process - for adding default lookup values in application context - spring

I am a Struts2 and Spring newbie and looking for some insight. When we load a web application we would typically want to cache some default look up data. e.g. if we wanted to store states or other data that does not change frequently and add it to the application context where we can access it across the application. What is the best way to realize this in a Struts2 application integrated with Spring? I read a bit about annotating with #PostConstruct which means I define my own class/method that would get a handle to the context by calling ServletActionContext.getServletContext() and then use setAttribute to add something. Is that a good way of going about things or is there a better option? Or would simply implementing a ServletContextListener be ideal?
Thanks for any input.

If you want to use the ServletContext, use Spring's ServletContextAware interface and then use an #PostConstruct or afterPropertiesSet method to add items to the servlet context.
This is simpler to use than the listener and integrates seamlessly with Spring, giving you access to properties files declared in Spring and any other beans.

Related

Spring: `RequestScope` vs `RequestContextHolder`

I am writing a Spring Boot service and wanted to include some form of RequestContext available to the controllers that might store things like the authenticated user and a request id. However, I see multiple approaches:
Use an #RequestScope bean
Use ServletRequest.setAttribute
Use Spring RequestContextHolder
What are the tradeoffs between these approaches?
Broadly speaking, RequestScope is the Spring magic way. It internally uses a RequestContextHolder which in turn depends on ServletRequest.setAttribute.
Said differently, the Spring way, is IMHO RequestScope. RequestContextHolder makes sense if you prefer limit the magic of Spring annotations.
Finaly, ServletRequest.setAttribute is still lower level, and should mainly be used if you want the code to be compatible with a non Spring application.
Moreover, for the first two ways, Spring uses a thread scoped object to store a reference to the request context, which allows the programmer to access the beans even in methods that do not explicitely receive the Request object.

Integrate JSF2 and Spring3 (with AOP)

I am using Spring and JSF2 a lot and was wondering what the best way to integrate them is? Now i understand there are basically two ways to do this, but i have some problems with both:
a) Use normal #ManagedBean, and inject Spring-Services into that beans using #ManagedProperty: The problem is that i can't use Spring-AOP inside a #ManagedBean obviously, because it is not Spring-managed. I usually use an arround-aspect on every method annotated with my custom annotation #DatabaseOperation. Another example would be #Secured from Spring-Security and so on. I use a lot of AOP in my project and not beeing able to use them on "the top level" is really limiting for me.
b) Use SpringBeanFacesELResolver to make everything managed by Spring. On the pro side is that AOP works like a charm, but the cons are big too:
No ViewScope. I am not sure if i can trust custom view scope implementations like this https://github.com/michail-nikolaev/primefaces-spring-scopes on productive systems?
Serialization is not possible. It's already pretty complicated, but once i use AOP i can't get it to work because org.springframework.aop.aspectj.AspectJPointcutAdvisor is not Serializable
So my question is: How do you overcome this issues? How do YOU integrate JSF2 and Spring3.x? I have been using possibility b) mostly, but on my next project i need session replication..
Any further suggestions?
The main integration pain point of the two frameworks is that JSF is usually used in a stateful way. to make the integration the most seamless, you would have to let Spring handle the statefulness and page navigations aspects himself instead of JSF, as well as bean creation and scoping.
The second important integration point is at the level of the view expression language. We want to be able to access spring beans while building the view, which means the managed bean layer is no longer needed.
The best integration available for the two frameworks is provided by introducing Spring webflow, see here for further details. With SWF, JSF no longer manages beans itself, this is done by Spring. JSF does not manage page navigation anymore, this handled in the SWF flow definition.
Also the statefulness is handled by SWF. The flow definition XML file replaces large parts of the faces-config.xml for view navigation, transition actions, bean definition, etc.
Using the SWF JSF integration means that your faces-config.xml is mostly empty. Expression language accessing directly spring beans can be used while building the view, and a view scope is available.
If you want to keep a page isolated from the rest of the application, youcan create a flow with a single view state and self-transitions. An advantage of SWF is that it prevents duplicate form submissions via a POST-REDIRECT-GET mechanism that works transparently out of the box.

ClassBridge called by MassIndexer and access to spring service

I wonder whether it is possible to use any spring services from within hibernate search ClassBridge.
Abstract:
My entity does not have all information i'd like to put into indexed document. I can not get it by #IndexedEmbeded too. This data comes from external data source, and I have service to provide this. This is only needed when reindexing. When indexing single object save service provide this information in transient entity field.
For reindex I use MassIndexer.
My application is working in Spring MVC environment. I use annotation driven configuration. So to access my service I need only use #Autowired annotation and service is ready to be used.
Solution?
When using Hibernate Search life is not so easy (or I have no idea how it could be).
To get additional information I decided to use ClassBridge. I also implement simple MassIndexer procedure called from within my spring service.
In my ClassBridge spring does not autowire service. When I try do quick workaround and use static field in ClassBridge and pass service reference from MassIndexer caller the other problem occurred. Service reports exception "no session" (sessionFactory.getCurrentSession() throws exception)... I have no idea where to go further.
What is recommended way to access Spring service from within ClassBridge code?
How can I get active hibernate Session (or SessionFactory) from within ClassBridge?
Thanx for you time & hope your help.
The recommended way is through compile time weaving and #Configurable
A comment on this page (http://guylabs.ch/2014/02/22/autowiring-pring-beans-in-hibernate-jpa-entity-listeners/) provides a much simpler way of handling this that might be useful. You can have Spring autowire an object for you, more explicitly by calling:
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
Doc: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/context/support/SpringBeanAutowiringSupport.html

in Spring, what is the best way to dynamically create different objects of a certain class, each object being able to access a Spring bean?

Say I have a file with each line depicting a different command (but of the same kind), which I want to read out and check and run and maybe do other operations such as merging, comparing and most importantly, store the commands into database.
To do that, I create the Command Class, and new a new Command object while reading each line of the file. Now the problem is, a Command object need to make use of, say a Spring bean which provides database access. As a result, I have to pass in that bean as a constructor argument of the Command class, which is very ugly, which doesn't seem to be the "Spring way"...
and I don't want to use ApplicationContextAware to make my class coupled to the Spring context.
Is there a best practice for this situation?
I very new to Spring and I know it might be a dumb question ...
I would create a CommandFactory that is coupled with spring and use that in your consumer instead. If the factory implements an interface you are not coupling yourself in the consumer and you don't close your possibilities of using a different -non spring coupled- one at a later point (e.g. testing).
In this case I think it is the best way to make the classes created by new Spring Beans.
Therefor annotate them with #Configurable, enable AspectJ, and read the Spring Reference Chapter 7.8.1 Using AspectJ to dependency inject domain objects with Spring

A heavily customized Spring Web application and the dispatcher servlet

We have a web application that uses spring, struts and camel right now and there is a lot of customization we have done to allow us to know when beans are added to the context.
So, we have gotten to a point where we would like to remove struts from the application, because we are only using it to handle actions and we figure we could either use spring or camel to do the same thing. So I was able to get it to work with camel/velocity, but we didn't like how we really couldn't use the request object directly in the jsp (afaik, you have to put everything in the header of the Exchange and in the jsp you would do ${header.someReqVariableName}).
So we wanted to go the spring route, but since we load the context.xml directly, we have a provider that extends ContextSingletonBeanFactoryLocator and we pass the xml file name as a param, we haven't been able to figure out how to get the DispatcherServlet to work without giving it another configuration xml.
Is there a way to either:
Have camel use jsp for processing a jsp (and have all the usage of jsp tags)?
or
Have spring to see that a context has already been loaded and use that instead of having another new one?
or
Something better I have thought up?
You can use camel-jetty to expose HTTP endpoints, but I wouldn't use it for any complex web app development (JPS, etc). I'd use use Spring MVC (or similar) and use Camel for any complex routing/messaging requirements...
Here is another way, you can use the producer template to send the request to the camel context if you can get the reference of the camel context from the spring.

Resources