The release of Spring 2.5 reduce the burden of XML by introduction annotation based configuration, but you still needed to bootstrap Spring in XML. However in Servlet 3 and Spring 3.1 we can now drop XML completely and have 100% code based configuration. All thanks to the Servlet 3 specification.
So lets see today how you can create Spring MVC application XML Free.
Step-by-Step Guide
Step 1) Create a Enterprise Java project in Eclipse and include Spring v3.1+ jars.
Step 2) Create a class which implements spring
WebApplicationInitializer and override onStartup() method.
Inside onStartup()
method, instantiate AnnotationConfigWebApplicationContext class and set
ServletContext. We also need to set a base package name of your configuration
files.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package org.techzoo.springmvc.bootstrap;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class MyWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
final AnnotationConfigWebApplicationContext root = newAnnotationConfigWebApplicationContext();
root.setServletContext(servletContext);
root.scan("org.techzoo.springmvc.bootstrap");
root.refresh();
final Dynamic servlet = servletContext.addServlet("spring", newDispatcherServlet(root));
servlet.setLoadOnStartup(1);
servlet.addMapping("/*");
}
}
|
Step 3) Next step is to configure Spring MVC. I do that
in following class.
@EnableWebMvc annotation used together with
@Configuration enables default Spring MVC configuration, equivalent to .
With @ComponentScan annotation we make sure our @Controller will be added to the
application context. The configuration class also defines one @Bean: our default
view resolver.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package org.techzoo.springmvc.bootstrap;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "org.techzoo.springmvc.controller")
public class ControllerConfiguration {
@Bean
public InternalResourceViewResolver configureInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
|
Step 4) Now create a simple Controller to see whether its working fine or Not.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package org.techzoo.springmvc.controller;
import java.io.IOException;
import java.io.Writer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@RequestMapping(value = "/")
public void home(@RequestBody final String body, final Writer writer)
throws IOException
{
writer.append("<h2>Welcome, XML
Free Spring MVC!</h2>");
}
}
|
Output
I really like this configuration changes in Spring MVC. It is easy to bootstrap the application with no xml files in place. Of course, this is not everything that Spring MVC 3 brings to the developers. With all the configuration in code it is so easy to refactor and navigate. No more back and forth with XML For more feature of spring mvc, please keep visiting TechZoo.
Happy Coding
reference from:http://www.techzoo.org/spring-framework/step-by-step-xml-free-spring-mvc-3-configuration.html
Step-by-Step XML Free Spring MVC 3 Configuration--reference,布布扣,bubuko.com