DI and IOC in spring mvc implementation - java

I am new to spring mvc and DI. I have came to know about the flow of the spring project and i know how the web projects in spring mvc is developed and worked on few projects too. All the annotation uses and xml configuration files in the spring mvc. But i am confused where the DI is used? and how the DI is implemented in spring with the help of IOC??
Can anyone please explain me the concept of DI and IOC and their implementation in spring mvc.
Thanks in advance!!!

DI and IOC happening through web.xml where you created the dispatcherservlet.
From Spring MVC docs :
The DispatcherServlet, provides a shared algorithm for request processing while actual work is performed by configurable, delegate components
The DispatcherServlet, as any Servlet, needs to be declared and mapped according to the Servlet specification using Java configuration or in web.xml. In turn the DispatcherServlet uses Spring configuration to discover the delegate components it needs for request mapping, view resolution, exception handling, and more.
internally it will register Spring mvc app and it will create an object and inject dependencies .

Related

Grails app using services from spring-mvc backend

We have a fairly involved web application written using spring-mvc with a maven build system and would like to harness all the power of Grails for the front end.
So the Grails app will essentially call into the spring-mvc app's service layer to access its business logic and data.
I need some guidance with my architectural approach to this integration at a high level.
From my understanding, I will need to;
- add my spring-mvc app as a compile dependency in my BuildConfig.groovy.
- Expose the service layer objects as service beans in my conf/spring/resources.groovy and inject them into my controllers
Questions:
My spring-mvc app has lots of dependencies of its own (which it obviously has to have) which are causing lots of dependency errors. Should I be setting "transitive=false" in my config and calling all of these in my Grails app?
How should the datasource get configured? I guess I have to integrate the applicationContext of my spring-mvc app by calling it from my Grails applicationContext and hope it all bootstraps nicely?
So the Grails app will essentially call into the spring-mvc app's service layer to access its business logic and data
Can you be a bit more specific about which components of the Spring MVC you want to use from Grails, is it just the services and datasource?
I will need to add my spring-mvc app as a compile dependency in my BuildConfig.groovy
yes
Expose the service layer objects as service beans in my conf/spring/resources.groovy
Although you could make the Spring beans known to your Grails app by defining them individually in resources.groovy, this is unnecessary because you've already defined them in an Spring XML file (presumably) in the Spring MVC project.
Instead you can use the importBeans method of the BeanBuilder to import the Spring beans defined in this XML file into the Grails app. Once you've added the Spring MVC project as a dependency of your Grails app, the Spring XML file should be on your classpath, so all you need to do is add the following to resources.groovy
beans = {
importBeans('classpath:/path/to/file/applicationContext-services.xml')
}
How should the datasource get configured?
A Spring bean named dataSource defines the datasource that a Grails app uses. In a standard Grails app, this bean is created based on the configuration in DataSource.groovy. If your Spring MVC app defines a bean with this name, then this should be used instead after making the changes above. To be sure that Grails is using the datasource from your Spring MVC app rather than whatever is in DataSource.groovy, I guess you could delete the contents of the latter.

what's the difference between managed bean and spring controller?

I am developing spring-hibernate-jsf application but i dont understand the difference between a managedbean and a spring controller. I think managedbeans work like controllers. Is there any advantage of using controller or managedbean?
Managed Beans provides services and are used as model for UI components. Controllers are request/response components like Servlets.
JSF is a component based web framework & Spring is a DI framework. JSF & Spring manages their own beans, so to reference ManagedBeans and inject in them you need to mark JSF ManagedBeans as Spring Controllers using #Controller annotation.
If you are thinking of replacing one with other, then no you have to use both of them if you want to use both Spring & JSF together.

Integrating JSF with Spring

I haven't implemented any code, I'm still working the overall architecture for a new application and this going to be the first time I use JSF+Spring.
I need to put web services in front of the Spring service beans (business logic tier) since these beans could be accessed by other applications besides the presentation tier. While defining the different layers or tiers for the application, I feel unsure about how to integrate JSF (the presentation tier) with Spring (the business tier in this application).
I'm considering to define some sort of common tier or service tier in order to provide the glue code for JSF and Spring, but before that I want to hear from others what have they done or if they have used other frameworks to help with the glue code for this scenario (I already checked Spring MVC/Spring Faces, but I'm not sure if that's what I need since I'm thinking of this application more like JSF-centric than Spring-centric, but maybe you could help me about considering another approach).
Thanks in advance.
The "glue" is the spring ELResolver, which you must configure in your faces-config.xml:
<application>
<!--
This is the JSF 1.2 ELResolver that delegates to the Spring root
WebApplicationContext resolving name references to Spring-defined
beans.
-->
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
This means that each #{bean.property} is handled by the resolving the bean in the spring context.

Spring MVC 3.0: how do I define an interceptor with annotations?

I am developing an app with Spring MVC (3.0).
I defined some controllers with annotations, how can I define an intereceptor with annotations too.
You can't. Hopefully we'll get that in 3.1. Until then, we have to use the HandlerInterceptor interface.

Using Spring AOP in an JSF application

consider a JSF web application with a managed bean FooBean.java. I've declared this "FooBean" in my faces-config.xml file. Now, if I want to add the Spring AOP advice for the methods of FooBean, how do I do that?
Should I add an applicationContext.xml file and declare the managed beans inside it?
or will it work even if I am not declaring the managed beans inside a Spring configuration file?
Note: I've created an Aspect bean and defined a point-cut like #Pointcut("within(dummy.web.reporting..*)") inside the aspect bean.
You can load a regular spring context xml file from within your web.xml like so:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring-context.xml</param-value>
</context-param>
You can then define your managed beans in here in the regualar spring way and you can still refer to these beans by id in your jsps.
You will also be able to use all the standard Spring AOP stuff within your spring-context.xml
I am using Spring AOP heavily in a Spring JSF application, I would rather suggest you to load your JSF beans via Spring container and also let Spring manage the Scope of the beans.
In such a scenario all beans would be loaded by Spring container thus it would become very easy to implement Spring AOP.
More info on such type of Spring-JSF integration
http://xebee.xebia.in/2011/10/31/spring-jsf-integration-made-easy-clean-using-spring-annotations/

Categories