Spring-Boot入门

之前项目中有个服务需要独立出来,同事用springboot改成了微服务,这两天没啥忙的就给拿出瞅瞅学习一下~

Demo只是对 这个例子 重新写了一遍加了一些注释和自己的东西 = =


新建一个maven项目

我用的是IDEA,新建maven项目时,记得添加一个属性 archetypeCatalog=internal

archetypeCatalog表示插件使用的archetype元数据,不加这个参数时默认为remote,local,即中央仓库archetype元数据,由于中央仓库的archetype太多了,所以导致很慢,指定internal来表示仅使用内部元数据


添加配置

pom.xml文件添加响应的配置

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-tools</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <name>Spring Boot Configuration Processor</name>
    <description>Spring Boot Configuration Processor</description>
    <url>http://projects.spring.io/spring-boot/</url>
    <organization>
        <name>Pivotal Software, Inc.</name>
        <url>http://www.spring.io</url>
    </organization>
    <properties>
        <main.basedir>${basedir}/../..</main.basedir>
    </properties>
    <dependencies>
        <!-- Compile (should stick to the bare minimum) -->
        <dependency>
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android-json</artifactId>
        </dependency>
        <!-- Test -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test-support</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <!-- Ensure own annotation processor doesn't kick in -->
                    <proc>none</proc>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

在application.properties中写入相关配置

server.port=9000

druid.url=jdbc:mysql://127.0.0.1:3306/test
druid.username=root
druid.password=root
druid.initialSize=1
druid.minIdle=1
druid.maxActive=20
druid.testOnBorrow=true

#springboot默认的日志系统是logback
logging.level.tk.mybatis=TRACE

#freemarker 文件的路径
spring.mvc.view.prefix=/templates/
spring.mvc.view.suffix=.ftl
spring.freemarker.cache=false
spring.freemarker.request-context-attribute=request

#mybatis扫描的文件路径
mybatis.type-aliases-package=tk.yoruo.springboot.model
mybatis.mapper-locations=classpath:mapper/*.xml

#通用mapper的配置
mapper.mappers=tk.yoruo.springboot.util.MyMapper
mapper.not-empty=false
mapper.identity=MYSQL

#mybatis分页插件的配置
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql


静态资源的路径

用了freemarker模版引擎 配置一些静态资源文件路径

package tk.yoruo.springboot.conf;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Created by LXC on 2017/2/9.
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    /**
     * spring-boot配置外部静态资源的方法
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}

其他的service层,实体,controller就不阐述了。


添加数据库配置

创建一个DruidProperties的类

添加一些属性。并加上@ConfigurationProperties(prefix = "druid")注解,告诉boot这个是个读取配置文件的类并且从 application.properties 中取前缀为 druid的value。

接着创建DruidAutoConfiguration类 加上@Configuration告诉boot这个是配置类。

注入DruidProperties 并加入@Bean

package tk.yoruo.springboot.druid;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.sql.SQLException;

/**
 * Created by LXC on 2017/2/9.
 */

@Configuration
@EnableConfigurationProperties(DruidProperties.class)
// 对应的类在classpath中有存在时才会注入
@ConditionalOnClass(DruidDataSource.class)

// 对应的前缀和属性名 在classpath中有存在时才会注入
@ConditionalOnProperty(prefix = "druid", name = "url")
public class DruidAutoConfiguration {

    @Autowired
    private DruidProperties druidProperties;

    @Bean
    public DruidDataSource dataSource() {

        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(druidProperties.getUrl());
        druidDataSource.setUsername(druidProperties.getUsername());
        druidDataSource.setPassword(druidProperties.getPassword());
        if (druidProperties.getInitialSize() > 0) {
            druidDataSource.setInitialSize(druidProperties.getInitialSize());
        }
        if (druidProperties.getMinIdle() > 0) {
            druidDataSource.setMinIdle(druidProperties.getMinIdle());
        }
        if (druidProperties.getMaxActive() > 0) {
            druidDataSource.setMaxActive(druidProperties.getMaxActive());
        }
        druidDataSource.setTestOnBorrow(druidProperties.isTestOnBorrow());
        try {
            druidDataSource.init();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return druidDataSource;
    }
}

接着 在src/main/resources目录下新建META-INF文件夹,然后新建spring.factories文件,这个文件用于告诉Spring Boot去找指定的自动配置文件

所以spring.factories内容为

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
tk.yoruo.springboot.druid.DruidAutoConfiguration

添加Druid Web监控

分别继承 Druid中的过滤器和servlet。

package tk.yoruo.springboot.druid;

import com.alibaba.druid.support.http.WebStatFilter;

import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;

/**
 * Created by LXC on 2017/2/8.
 */

@WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*",
        initParams = {
                @WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源
        })
public class DruidStatFilter extends WebStatFilter {

}


package tk.yoruo.springboot.druid;

import com.alibaba.druid.support.http.StatViewServlet;

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

/**
 * Created by LXC on 2017/2/8.
 */

@SuppressWarnings("serial")
@WebServlet(urlPatterns = "/druid/*",
        initParams = {
                @WebInitParam(name = "allow", value = "127.0.0.1"),// IP白名单 (没有配置或者为空,则允许所有访问)
//                @WebInitParam(name="deny",value="192.168.16.111"),// IP黑名单 (存在共同时,deny优先于allow)
                @WebInitParam(name = "loginUsername", value = "admin"),// 用户名
                @WebInitParam(name = "loginPassword", value = "admin"),// 密码
                @WebInitParam(name = "resetEnable", value = "true")// 禁用HTML页面上的“Reset All”功能
        })
public class DruidStatViewServlet extends StatViewServlet {

}

注意还需要在Application类上加入 @ServletComponentScan的注解,否则扫描不到你添加的servlet。

package tk.yoruo.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import tk.yoruo.springboot.util.MyMapper;

/**
 * Created by LXC on 2017/2/9.
 */


@Controller
@ServletComponentScan
@EnableWebMvc
//@ComponentScan(basePackages = {"tk.yoruo.springboot.controller","tk.yoruo.springboot.service"})
@SpringBootApplication
@MapperScan(basePackages = "tk.yoruo.springboot.mapper", markerInterface = MyMapper.class)
public class Application {

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

    @RequestMapping("/")
    String home() {
        return "redirect:countries";
    }

}


注意

Application springboot启动的类,不建议放在顶级的包。例如src/main/java下面。

如果这样做了会和我一样出现这个错误

** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.


如果一定要这样做,需要在Application上加这样的注解。标明你要扫描的包

@ComponentScan(basePackages = {"tk.yoruo.springboot.controller","tk.yoruo.springboot.service"})

这样话静态资源会找不到。

那应该怎么找到... 我还没去试 = =

建议别这样做,放在外面启动服务时间会变久。


欢迎光临我的个人博客

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容