(一) spring boot 之 helloworld
标签: 微服务
spring boot作为java生态圈中的微服务之一,可以说是最佳实现方式。下面我们通过实际演练一起来看看spring boot为何被称为“最佳实现”。
- 环境要求
- 依赖引入
- 第一个spring boot程序
- 总结
1.环境要求
我想这张图已经基本上能说明所需要的环境。
除此之外,spring boot还推荐用Maven或者Gradle来管理它的依赖。
2.依赖引入
<?xml version="1.0" encoding="UTF-8"?>
<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>base</groupId>
<artifactId>base</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>
<dependencies>
<!--spring boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
<build>
<!--打包用的插件-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
</project>
上面给出了一个完整的pom.xml内容,其中可以看到引入了spring-boot-starter-parent作为parent,这是spring boot的官方推荐做法,当然,通常确实只需要这样做就可以了。
不过考虑到如果我们需要使用自己的parent,那就不能这样做了,解决方式有两种:
1.在自己parent中引入spring-boot-starter-parent
2.不引入spring-boot-starter-parent,而改成逐个引入需要的依赖
3.第一个spring boot程序
首先根据之前说的,配置好必要环境后,我们来创建一个工程。
相信大家一定看到这里有个BaseApplication这么一个类,确实,这个类是作为spring boot的入口存在的。
package com.qwf.base;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created by qwf on 16/7/18.
*/
@SpringBootApplication
public class BaseApplication {
public static void main(String[] args) {
SpringApplication.run(BaseApplication.class,args);
}
}
一般情况下这个类里提供一个main函数就差不多了,不过如果配置了一些拦截器,监听或者其他操作的,也会在这里进行注册,不过现在就不多说了。
大家可能会有一些疑惑,不是应该需要一些类似于@ComponentScan,@EnableAutoConfiguration等等的注解吗,不然它从哪里去扫描包获取bean啊,不然它怎么去进行配置啊。
这些其实都包含在@SpringBootApplication中了。
package org.springframework.boot.autoconfigure;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AliasFor;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {
Class<?>[] exclude() default {};
String[] excludeName() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackages"
)
String[] scanBasePackages() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackageClasses"
)
Class<?>[] scanBasePackageClasses() default {};
}
至于说为何要把BaseApplication放在最外层包下,因为spring boot在进行包扫描时,如果不指定扫描包,那默认就是从BaseApplication所在包的位置开始扫。
那有了入口,没有我们熟悉controller,service怎么行。
那我们在创建一个TestController(起名比较随意,大家不要在意)。
package com.qwf.base.controller;
import com.qwf.base.util.RedisTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisCluster;
/**
* Created by qwf on 16/8/18.
*/
@RestController
public class TestController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String hello(){
return "hello";
}
}
@RestController是spring mvc的注解,相当于@Controller+@ResponseBody+启动json转换器注解,用他的话可以直接返回json。
package org.springframework.web.bind.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
String value() default "";
}
好,那基本上已经完成了,接下去就是配置下启动服务了。
Main Class指的就是spring boot的入口类
VM options 这个大家不用关注,只是我习惯用的是8800端口。这里提一下,可能有些人会说,“呀,你改端口不是应该在tomcat的配置文件里改吗”,这是因为spring boot内嵌了一个tomcat,所以可以这样来操作,这部分在后期会详细提到他,这里就不用纠结了。
Use classpath of module 这个东西是指定运行的模块,我们这里就一个base模块,自然不用管它了。
最后点击ok就可以啦。
然后我们运行它
运行方式可以选择IDE的run,也可以用mvn spring-boot:run这样的命令方式来启动,其实差不多。就是用命令启动的时候似乎会额外下载一些东西。
看到这个基本就是启动成功了。
里面的那个warn大家不用在意,因为这是一个以前写的工程,用到了freemarker,这东西默认资源在templates下的,不过templates文件夹被我删了,所以就有了这么一个warn。
成功输入一个hello!
那到这里,第一个spring boot程序就完成了
4.总结
虽然第一个spring boot程序看上去很简洁,但是还是有不少地方需要我们去注意。
1.配置问题,引入jar依赖时,如果不按照官方推荐的方式来,那么需要小心不要漏了或者重复了,不然可能会导致一些奇奇怪怪的错误
2.入口类,@SpringBootApplication注解不要忘记加,或者不喜欢一件配置的朋友可以单独加@Configuration,@ComponentScan等,但是也是要注意不要漏写或者错写。