純JAVA配置Spring(拋棄XML配置)

Spring神一樣存在的框架,幫助Java開發(fā)者巧妙快速的應(yīng)對公司或企業(yè)的一個個變態(tài)的需求.這偏文章不談原理,只論快速配置使用.

筆者所用開發(fā)工具是MyEclipse,使用了強(qiáng)大的Maven管理項(xiàng)目.廢話不多語,開始....

** 1.創(chuàng)建項(xiàng)目**

![Uploading Snip20170531_4_284300.png . . .]
Snip20170531_4.png
Snip20170531_5.png
Snip20170531_6.png

** 2.添加Spring依賴(筆者使用的是4.1.0)**

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.0.RELEASE</version>
        </dependency>

** 3.創(chuàng)建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.創(chuàng)建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.創(chuàng)建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.創(chuàng)建控制器**

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 下創(chuàng)建views文件夾,并創(chuàng)建Home.jsp 文件 最終項(xiàng)目結(jié)構(gòu)如下**

Snip20170531_7.png

** 8.將程序部署到Tomcat 訪問http://127.0.0.1:8080/Demo/home**

Snip20170531_8.png

純Java配置Spring還是比較簡單的,相對于傳統(tǒng)的XML配置,我個人還是比較喜歡這種方式.具體如何看個人喜好.今天的教程就到此,有疑問的小伙伴可以給我留言,有不對的地方,希望大神不吝賜教.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,604評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,275評論 6 342
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,138評論 25 708
  • 作用: 1,僅僅對屏幕進(jìn)行分類,真正排布UI元素還得使用autolayout;2,不再有橫豎屏的概念,只有屏幕尺寸...
    小子愛搞事閱讀 969評論 5 0
  • 維度分兩種。 低維:競爭,低價,攻擊,時刻警惕 高維:無競爭,共贏,共成長 你想要什么樣的人生? 1) 看微博:關(guān)...
    利曉琳閱讀 551評論 0 0

友情鏈接更多精彩內(nèi)容