我的第一篇博客spring boot学习

spring boot最近貌似很火,号称(微服务).....不多说了上代码

看了很多篇博客,大都差不多,总结了下有几个注意的地方下面说明。

首先项目用maven构建。

先来看下目录结构



一、pom.xml(很关键)

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com</groupId>
    <artifactId>mySpringBoot</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>mySpringBoot</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.RELEASE</version>
    </parent>

    <dependencies>
        <!--springboot依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--springboot依赖-->

        <!-- 模板引擎 类似jstl 更适合html5 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!-- spring热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        
        <!--springboot修改代码立即生效--><!--调试的时候方便不加的话每次需要重启运行Application.java-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>utf8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

二、启动类Application.java

package mySpringBoot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//@Configuration  
//@ComponentScan 
//@EnableAutoConfiguration//自动配置  

@SpringBootApplication//和上面的三个注解作用相同
public class Application {

	public static void main(String[] args) throws Exception {
		SpringApplication.run(Application.class, args);
	}

}


直接运行main方法启动,这里卡了好久报个各种错误(具体情况以后再补充),启动成功如下

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

2016-09-20 19:17:33.800  INFO 31607 --- [  restartedMain] mySpringBoot.Application                 : Starting Application on jcz-Latitude-3550 with PID 31607 (/home/jcz/workspace/mySpringBoot/target/classes started by jcz in /home/jcz/workspace/mySpringBoot)
2016-09-20 19:17:33.805  INFO 31607 --- [  restartedMain] mySpringBoot.Application                 : No profiles are active
2016-09-20 19:17:34.295  INFO 31607 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@678b18dd: startup date [Tue Sep 20 19:17:34 CST 2016]; root of context hierarchy
2016-09-20 19:17:36.076  INFO 31607 --- [  restartedMain] 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-09-20 19:17:37.071  INFO 31607 --- [  restartedMain] e.j.JettyEmbeddedServletContainerFactory : Server initialized with port: 8080
2016-09-20 19:17:37.078  INFO 31607 --- [  restartedMain] org.eclipse.jetty.server.Server          : jetty-9.2.14.v20151106
2016-09-20 19:17:37.267  INFO 31607 --- [  restartedMain] application                              : Initializing Spring embedded WebApplicationContext
2016-09-20 19:17:37.267  INFO 31607 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2974 ms
2016-09-20 19:17:37.990  INFO 31607 --- [  restartedMain] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-09-20 19:17:37.993  INFO 31607 --- [  restartedMain] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-09-20 19:17:37.993  INFO 31607 --- [  restartedMain] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-09-20 19:17:37.994  INFO 31607 --- [  restartedMain] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-09-20 19:17:37.994  INFO 31607 --- [  restartedMain] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2016-09-20 19:17:38.293  INFO 31607 --- [  restartedMain] o.e.jetty.server.handler.ContextHandler  : Started o.s.b.c.e.j.JettyEmbeddedWebAppContext@745e6f01{/,file:/tmp/jetty-docbase.2885017018384146467.8080/,AVAILABLE}
2016-09-20 19:17:38.294  INFO 31607 --- [  restartedMain] org.eclipse.jetty.server.Server          : Started @6514ms
2016-09-20 19:17:38.482  INFO 31607 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@678b18dd: startup date [Tue Sep 20 19:17:34 CST 2016]; root of context hierarchy
2016-09-20 19:17:38.591  INFO 31607 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/action2]}" onto public java.lang.String mySpringBoot.action.Action2.hello(org.springframework.ui.Model)
2016-09-20 19:17:38.593  INFO 31607 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/action1]}" onto public java.lang.String mySpringBoot.action.Action1.action1()
2016-09-20 19:17:38.600  INFO 31607 --- [  restartedMain] 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)
2016-09-20 19:17:38.601  INFO 31607 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-09-20 19:17:38.658  INFO 31607 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-20 19:17:38.658  INFO 31607 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-20 19:17:38.749  INFO 31607 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-20 19:17:39.580  INFO 31607 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2016-09-20 19:17:39.700  INFO 31607 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-09-20 19:17:39.732  INFO 31607 --- [  restartedMain] application                              : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-09-20 19:17:39.732  INFO 31607 --- [  restartedMain] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2016-09-20 19:17:39.764  INFO 31607 --- [  restartedMain] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 32 ms
2016-09-20 19:17:39.869  INFO 31607 --- [  restartedMain] o.eclipse.jetty.server.ServerConnector   : Started ServerConnector@52d2343d{HTTP/1.1}{0.0.0.0:8080}
2016-09-20 19:17:39.872  INFO 31607 --- [  restartedMain] .s.b.c.e.j.JettyEmbeddedServletContainer : Jetty started on port(s) 8080 (http/1.1)
2016-09-20 19:17:39.881  INFO 31607 --- [  restartedMain] mySpringBoot.Application                 : Started Application in 7.515 seconds (JVM running for 8.101)

三、写一个Action代码

package mySpringBoot.action;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController//是Controller的一种,HTTP Response的Body部分返回给浏览器
@EnableAutoConfiguration
public class Action1 {
	
	@RequestMapping("/action1")
	public String action1() {
		return "Hello SpringBoot action1";
	}
}



好,接下来再写一个跳页面的Action

package mySpringBoot.action;

import java.util.HashMap;
import java.util.Map;

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

@Controller
public class Action2 {
    @RequestMapping("/action2")
	public String hello(Model model) {
    	
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("id", 1);
		map.put("name", "赵六");
		map.put("job","java coding man");
		
    	model.addAttribute("map", map);
    	model.addAttribute("message", "你好");
    	
        return "hello";//跳转到hello页面
	}
}

这里跳的不是jsp了,而是html5,请看pom引入了thymeleaf,好不多说,看页面代码

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:text="'Hello, ' + ${map.name} + '!'" />
    <p th:text="${map.id}+${map.name}+${map.job}" />
</body>
</html>

是不是和jstl很像!!嘻嘻

到这里很容易就跑起来了spring项目了。

我启动时的错,引入tomcat目录bin下的tomcat-juli.jar启动成功。

有的错误还和pom.xml里的spring boot版本有关,具体的还待研究(也是群里各种问大笑)。

注意:1、Application.java这个类所在位置要在最外层,这样才能spring扫描到它下面的所有action、serveice、dao等

最后毕竟是最近比较火,有些不稳定莫名其妙报错
写到这里,待续......




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值