Insert data by mobile application and web application throws duplicated entry exception - spring

I have an existing web application developed with JSF 2.0 for the view layer and Spring 3 for data access, service and security layers. Then I tried to create the mobile application so I used a Spring #controller class to listen on ws requests but the problem is that when I insert data by mobile application and web application a duplicated entry exception was thrown. It seems to me that Spring create two instances of my Dao in different application contexts one for my managed beans and other one for controller.
NB: I use criteria for data base querys
Can some one help me ?

Finally, i can use a spring controller with jsf web application. i created a applicationContext.xml file then i moved all my beans into it and i conserve my servlet.xml file but with no beans inside, that's will create one root webapp context, by consequence hibernate will create one session for the managed beans and the controller
Difference between applicationContext.xml and spring-servlet.xml in Spring

Related

spring boot servlet context vs application context

I Come from years with Spring MVC, I'm trying to understand some key differences with Spring boot.
With Spring MVC I used to make a very clear distinction between application context and servlet context(s).
With Spring boot it looks like the servlet context has been pretty much deprecated and all the beans created by means of autoconfig mechanism live in the app context.
you can still create your servlet context of course, you have just to keep in mind the autoconfig is using application context.
so for example one implication of this is that #RestControllers and #Controllers live in the application context and the Spring Boot autoconfig servlet dispatchers will use any #RestController or #Controller annotated beans in the app context.
Can you help me confirm on this or make me understand what I'm missing here ?
In spring-springMVC system, there are two containers as your mentioned. For springboot-springMVC, debug in your controller and service with implementing ApplicationContextAware
they use the same global applicationContext
org.springframework.boot.web.servlet.contextAnnotationConfigServletWebServerApplicationContext

DispatcherServlet and ContextLoaderListener in Spring

What is the difference between DispatcherServlet and ContextLoaderListener in Spring framework? Do we need to configure both of them in web.xml when we are using spring framework?
AFAIK each DispatcherServlet will have a WebApplicationContext. By default the DispatcherServlet looks for a spring configuration file named [appname]-servlet.xml under WEB-INF folder.
Do we need to configure DispatcherServlet?
Yes, every spring application should configure DispatcherServlet as it is the one through which all the requests are routed. It decides the appropriate method of the controller class to handle the request. Once controller returns the model along with the logical view, DispatcherServlet takes the help of ViewResolver to resolve the view (generally JSPs) and will pass the model data to the view, which is finally rendered on the browser.
Do we need to configure ContextLoaderListener?
No, this is not mandatory. Spring applications can live with out ContextLoaderListener.
Why do we need ContextLoaderListener?
Usually when we build multi-tier applications we don't want to clutter all the beans in one config file [appname]-servlet.xml. For example if you configure spring security you wanted to include all those beans in security-context.xml, in the same way all the beans belonging to service layer are configured in applicationContext.xml and some would like to configure beans belonging to DAO layer in dao-context.xml. So when you configure all these beans in different context files, you need to let know spring that these files exist as spring only knows about [appname]-servlet.xml. ContextLoaderListener will help spring recognize all the other context files.
Hope this helps!
The root WebApplicationContext is a Spring Application Context shared across the application.
A DispatcherServlet instance actually has its own
WebApplicationContext.
One can have multiple DispatcherServlet instances in an application, and each will have its own WebApplicationContext.
The root WebApplicationContext is shared across
the application, so if you have a root WebApplicationContext and
multiple DispatcherServlets, the DispatcherServlets will share the
root WebApplicationContext.
However, for a simple Spring MVC application, one can even have a situation where there is no need to have a root WebApplicationContext. A DispatcherServlet would still have its own WebApplicationContext, but it doesn’t actually need to have a parent root WebApplicationContext.
So, which beans should go in the root Web Application Context and which beans should go in the DispatcherServlet’s Web Application Context?
Well, general beans such as services and DAOs make their way in root Web Application Context, and more web-specific beans such as controllers are included in DispatcherServlet’s Web Application Context.
When DispatcherServlet starts up, it creates a Spring application context and starts
loading it with beans declared in the configuration files or classes that it’s given.
But in Spring web applications, there’s often another application context. This
other application context is created by ContextLoaderListener
Whereas DispatcherServlet is expected to load beans containing web components
such as controllers, view resolvers, and handler mappings, ContextLoaderListener is
expected to load the other beans in your application. These beans are typically the
middle-tier and data-tier components that drive the back end of the application.
Good luck!

Creating Spring Service that reads a file and serve queries based on its data

In a Java Web Project with Spring and JSF, I want do this: I want to have a service that in the first run of the app, reads a file and puts its data to a variable. then other classes can read that variable.
In fact I want that file reads one time and after that I just query the data, even web pages changed via links and navigation system.
Is there a Spring annotation to turn class to a service like this? Should I have some XML config files to specify a class as a service? I don't know what I have to do. What I know is that it can be done via Spring and I can get its data from JSF components, but how?
I have to do this based on MVC.
You can have it lazy-loaded on first call of the service. Or you can add a ContextListener to initialize the service on startup of the web context, assuming you are deploying to a J2EE container. Or you can make that service a spring bean with an init-method that initializes the data. Or any one of a hundred different ways. You'll need to determine what works best for your application.
Here's a spring example:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="myService"
class="org.your.Service"
init-method="initializeData"></bean>
</beans>
How you get hold of that service is up to you. You can either have the data stored statically on the class, or you can use the spring context to retrieve this single instance of the spring bean.
Spring documentation for lifecycle methods: http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-factory-lifecycle

Spring MVC two configurations?

I created a simple spring-web-mvc project based on maven. The sample includes two configuration files. One in src / main / resources / spring / application-config.xml
and a second in src / main / webapp / WEB-INF / mvc-config.xml
Is that config normal? I think it is just configuration splitting basics mvc stuff in mvc-config.xml and application-config.xml for spring commons or?
For a web application then that is fairly normal - although the naming convention for the two files is often dispatcher-servlet.xml and applicationContext.xml.
The dispatcher-servlet.xml (or mvc-config.xml as in your question) is the configuration file for the web application context and contains the web specific beans and configuration for Spring MVC. It is loaded by the DispatcherServlet when the application starts up.
The applicationContext.xml (or application-config.xml as in your question) is the configuration file for the main Spring application context and contains the non-web business beans (typically services, DAOs etc). This file is often spilt into fragments - with a fragment containing the beans for each logical layer within the app. This file is typically loaded by the ContextLoaderListener defined in the web.xml.
Spring automatically sets the main application context as the parent of the web application context. This ensures that web components (such as controllers) have access to business beans in the application context. However, business beans are not able to see beans in the web application context.

Access Dispatcher servlet without accessing Application Context

I'm creating a simple Spring MVC app with a DAO layer. Now I want to access the Spring JdbcTemplate beans that I've initialized in the dispatcher servlet. I've been using FileSystemXmlApplicationContext to get to my dispatcher servlet till now, but I realized its not a good practice for MVC app when I need to deploy it elsewhere.
Can somebody let me know if I can use XmlWebApplicationContext or ClassPathXmlApplicationContext to get hold of the dispatcher servlet config file which resides in the WEB-INF folder?
I don't want to extend any ApplicationAware interface and I've also not got access to servlet context, as I'm using ModelAttribute annotations to get the data from beans in JSPs. Also, I don't have any applicationContext.xml in my web-app.

Resources