纯JAVA配置Spring(抛弃XML配置)

Spring神一样存在的框架,帮助Java开发者巧妙快速的应对公司或企业的一个个变态的需求.这偏文章不谈原理,只论快速配置使用.

笔者所用开发工具是MyEclipse,使用了强大的Maven管理项目.废话不多语,开始....

** 1.创建项目**

纯JAVA配置Spring(抛弃XML配置)_第1张图片
![Uploading Snip20170531_4_284300.png . . .]
纯JAVA配置Spring(抛弃XML配置)_第2张图片
Snip20170531_4.png
纯JAVA配置Spring(抛弃XML配置)_第3张图片
Snip20170531_5.png
纯JAVA配置Spring(抛弃XML配置)_第4张图片
Snip20170531_6.png

** 2.添加Spring依赖(笔者使用的是4.1.0)**

        
            org.springframework
            spring-webmvc
            4.1.0.RELEASE
        

** 3.创建Java配置文件WebConfig**

具体注解的使用以及意义,读者自行查阅文档.

package demo.config;

import java.io.IOException;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;



@Configuration
@EnableWebMvc
@ComponentScan("demo") 
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean 
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }
    

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        // TODO Auto-generated method stub
        configurer.enable();
    }

    @Bean
    public MultipartResolver multipartResolver() throws IOException{
        return new StandardServletMultipartResolver();
    }
    


}

** 4.创建RootConfig**


package demo.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan(basePackages={"demo"},
                excludeFilters={
        @Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)
})

public class RootConfig {

}

** 5.创建DispatcherServlet**

package demo.config;


import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class DispatcherServlet extends
        AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return new Class[] {RootConfig.class};
    }

    @Override
    protected Class[] getServletConfigClasses() {
        // TODO Auto-generated method stub
        return new Class[] {WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        return new String[] {"/"};
    }
}

** 6.创建控制器**

package demo.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {
    @RequestMapping(value="/home")
    public String home(){
        return "Home";
    }
}

** 7.在WEB-INF 下创建views文件夹,并创建Home.jsp 文件 最终项目结构如下**

纯JAVA配置Spring(抛弃XML配置)_第5张图片
Snip20170531_7.png

** 8.将程序部署到Tomcat 访问http://127.0.0.1:8080/Demo/home**

纯JAVA配置Spring(抛弃XML配置)_第6张图片
Snip20170531_8.png

纯Java配置Spring还是比较简单的,相对于传统的XML配置,我个人还是比较喜欢这种方式.具体如何看个人喜好.今天的教程就到此,有疑问的小伙伴可以给我留言,有不对的地方,希望大神不吝赐教.

你可能感兴趣的:(纯JAVA配置Spring(抛弃XML配置))