spring boot 学习小结1 spring mvc环境搭建

spring boot 官方指导文档地址
前段时间,了解到spring boot 比较适合用来快速进行 web项目开发,就有了研究下该框架怎么使用的想法。在搜索相关内容方面,发现比较少,于是就自己去看官方文档,终于成功搭建了一个demo。
运行结果如下图:
spring boot 学习小结1 spring mvc环境搭建_第1张图片

下面是环境搭建的具体步骤
1:创建一个webapp的maven项目,pom文件内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>improve.skngroupId>
    <artifactId>demoartifactId>
    <packaging>warpackaging>
    <version>0.0.1-SNAPSHOTversion>
    <name>demo Maven Webappname>
    <url>http://maven.apache.orgurl>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.3.1.RELEASEversion>
    parent>


    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.apache.tomcat.embedgroupId>
            <artifactId>tomcat-embed-jasperartifactId>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-tomcatartifactId>
            <scope>providedscope>
        dependency>

        

        <dependency>
            <groupId>jstlgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>

        <dependency>
            <groupId>taglibsgroupId>
            <artifactId>standardartifactId>
            <version>1.1.2version>
        dependency>

        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-coreartifactId>
            <version>4.3.2.Finalversion>
        dependency>

        
    dependencies>
    <build>
        <finalName>demofinalName>
    build>
project>

注意其中的parent元素,里面定义了很多的maven配置信息,我们直接继承就好了。

2:编写controller层、service层和dao层代码
controller代码:

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping
    public String showLogin(){
        return "/WEB-INF/jsp/login.jsp";
    }

}

service代码(这里我只粘贴了impl的代码):

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
}

dao代码:

@Repository
public class UserDao {

}

从上面可以看出来,我只是简单的分了个层,但是具体的业务代码我都没有去写,当然我这只是一个简单的demo项目,所以也不需要写。顺便说下,spring boot 1.3.1 引用的spring版本是4.2.4 Release。
接下来,最重要的步骤来了,就是配置spring boot启动类来启动demo。

3:配置spring boot 启动入口类,来启动整个spring boot demo项目
启动类代码如下:

@SpringBootApplication(scanBasePackages = {"improve.skn.demo.controller","improve.skn.demo.service.impl","improve.skn.demo.dao"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@SpringBootApplication 的作用是标识这个类可以被spring boot框架识别,其中的scanBasePackages 属性的作用是定义注解扫描的目录。从代码中可以看出,分别取扫描了 controller、serviceimpl、dao 层的类。
直接运行Application类的main方法,启动spring boot 项目。

为了更容易理解,我把我的demo项目目录结构分享下:
spring boot 学习小结1 spring mvc环境搭建_第2张图片

再分享下目录spring boot启动成功的控制台信息


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.1.RELEASE)

2016-01-12 09:42:19.284  INFO 4196 --- [           main] i.skn.demo.application.Application       : Starting Application on shikenian with PID 4196 (C:\developer\workspace\demo\target\classes started by shikenian in C:\developer\workspace\demo)
2016-01-12 09:42:19.284  INFO 4196 --- [           main] i.skn.demo.application.Application       : No active profile set, falling back to default profiles: default
2016-01-12 09:42:19.401  INFO 4196 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6cbc2704: startup date [Tue Jan 12 09:42:19 CST 2016]; root of context hierarchy
2016-01-12 09:42:20.695  INFO 4196 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2016-01-12 09:42:21.476  INFO 4196 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-01-12 09:42:21.492  INFO 4196 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-01-12 09:42:21.492  INFO 4196 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.30
2016-01-12 09:42:22.163  INFO 4196 --- [ost-startStop-1] org.apache.jasper.servlet.TldScanner     : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2016-01-12 09:42:22.163  INFO 4196 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-01-12 09:42:22.163  INFO 4196 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2762 ms
2016-01-12 09:42:22.632  INFO 4196 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-01-12 09:42:22.647  INFO 4196 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-01-12 09:42:22.647  INFO 4196 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-01-12 09:42:22.647  INFO 4196 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-01-12 09:42:22.647  INFO 4196 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2016-01-12 09:42:22.975  INFO 4196 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6cbc2704: startup date [Tue Jan 12 09:42:19 CST 2016]; root of context hierarchy
2016-01-12 09:42:23.037  INFO 4196 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/login]}" onto public java.lang.String improve.skn.demo.controller.LoginController.showLogin()
2016-01-12 09:42:23.037  INFO 4196 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET]}" onto public java.lang.String improve.skn.demo.controller.SpringBootTestController.hello()
2016-01-12 09:42:23.037  INFO 4196 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-01-12 09:42:23.053  INFO 4196 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-01-12 09:42:23.084  INFO 4196 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-12 09:42:23.084  INFO 4196 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-12 09:42:23.209  INFO 4196 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-12 09:42:23.475  INFO 4196 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-01-12 09:42:23.600  INFO 4196 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-01-12 09:42:23.600  INFO 4196 --- [           main] i.skn.demo.application.Application       : Started Application in 4.909 seconds (JVM running for 5.283)
2016-01-12 09:43:05.023  INFO 4196 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-01-12 09:43:05.023  INFO 4196 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2016-01-12 09:43:05.039  INFO 4196 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 16 ms

你可能感兴趣的:(springboot,spring,mvc,spring,mvc)