作家
登录

SpringMVC配置太多?试试SpringBoot

作者: 来源: 2017-10-17 10:21:46 阅读 我要评论


SpringMVC信赖大年夜家已经不再陌生了,大年夜家可能对于Spring的各类XML设备已经产生了厌恶的感到,Spring官方宣布的Springboot 已经很长时光了,Springboot是一款“商定优于设备”的轻量级框架;Springboot起首解决的就是各类繁琐的XML设备,你可以不消任何XML设备,进行web办事的搭建,其次是Springboot本身就持续了web办事器,如不雅松习端开辟人员想在本地启动后端办事不须要进行各类设备,几乎可以做到一键启动。

再有就是今朝大年夜热的微办事,而Springboot恰好知足了快速开辟微办事的开辟场景;对于今朝主流的框架Spring+MyBatis+redis的集成,好吧直接看代码...

以下代码是全部开辟框架集成完之后的,关于Spring官方那一套若何编写启动类,若何设备端口这些随便google一大年夜把的我就不再本文说清楚明了。下面的代码,mybatis mapper我就不贴了,平常怎么写如今也一样,还有redis存数据取数据什么的。本文给的都是划的重点啊!

1.数据源以及其他的设备文件(PS:说好了不设备,怎么刚开端就上设备? 答:不设备也可以,如不雅你想把数据源竽暌共编码写逝世的话。^_^)

jedis :   pool :     host : 127.0.0.1     port : 6379     config :       maxTotal: 100       maxIdle: 10       maxWaitMillis : 100000server :   port :  8080   jdbc:    datasource:        name: test        url: jdbc:mysql://127.0.0.1:3306/test        username: root        password: 123456        # 应用druid数据源        type: com.alibaba.druid.pool.DruidDataSource        driver-class-name: com.mysql.jdbc.Driver        filters: stat        maxActive: 20        initialSize: 1        maxWait: 60000        minIdle: 1        timeBetweenEvictionRunsMillis: 60000        minEvictableIdleTimeMillis: 300000        validationQuery: select 'x'        testWhileIdle: true        testOnBorrow: false        testOnReturn: false        poolPreparedStatements: true        maxOpenPreparedStatements: 20# MyBatismybatis:    typeAliasesPackage: com.xiaour.spring.boot.entity    mapperLocations: classpath*:/com/xiaour/spring/boot/mapper/*.xml    configLocation: classpath:mybatis-config.xml     # LOGGINGlogging:    level:       com.ibatis:DEBUG

沙龙晃荡 | 去哪儿、陌陌、ThoughtWorks在主动化运维中的实践!10.28不见不散!

2.Springboot启动类

package com.tony.spring.boot;import org.mybatis.spring.annotation.MapperScan;import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;import org.springframework.boot.web.servlet.ServletComponentScan;import org.springframework.boot.web.support.SpringBootServletInitializer;/** * Created by zhang.tao on 2017/4/19. */@SpringBootApplication(exclude = MybatisAutoConfiguration.class)@ServletComponentScan@EnableAutoConfiguration@MapperScan("com.tony.spring.boot.mapper")public class Application  extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer {    @Value("${server.port}")    private int port;//应用的端口    /**     * 启动人口     * @param args     */    public static void main(String ... args){        SpringApplication.run(Application.class, args);    }    /**     * 自定义端口     */    @Override    public void customize(ConfigurableEmbeddedServletContainer container) {        container.setPort(port);           }}

3.设备Mysql数据源

import java.sql.SQLException;import javax.sql.DataSource;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.bind.RelaxedPropertyResolver;import org.springframework.context.EnvironmentAware;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.env.Environment;import org.springframework.transaction.annotation.EnableTransactionManagement;import com.alibaba.druid.pool.DruidDataSource;@Configuration@EnableTransactionManagementpublic class DataBaseConfiguration implements EnvironmentAware {    private RelaxedPropertyResolver propertyResolver;    private static Logger log = LoggerFactory.getLogger(DataBaseConfiguration.class);    private Environment env;    @Override    public void setEnvironment(Environment env) {        this.env = env;        this.propertyResolver = new RelaxedPropertyResolver(env, "jdbc.datasource.");    }    /**     * 设备数据源     * @Description TODO     * @return     */    @Bean(name = "dataSource",destroyMethod = "close")    public DataSource dataSource() {        log.debug(env.getActiveProfiles().toString());                    DruidDataSource dataSource = new DruidDataSource();          dataSource.setUrl(propertyResolver.getProperty("url"));          dataSource.setUsername(propertyResolver.getProperty("username"));//用户名          dataSource.setPassword(propertyResolver.getProperty("password"));//暗码          dataSource.setDriverClassName(propertyResolver.getProperty("driver-class-name"));         dataSource.setInitialSize(Integer.parseInt(propertyResolver.getProperty("initialSize")));          dataSource.setMaxActive(Integer.parseInt(propertyResolver.getProperty("maxActive")));          dataSource.setMinIdle(Integer.parseInt(propertyResolver.getProperty("minIdle")));          dataSource.setMaxWait(Integer.parseInt(propertyResolver.getProperty("maxWait")));          dataSource.setTimeBetweenEvictionRunsMillis(Integer.parseInt(propertyResolver.getProperty("timeBetweenEvictionRunsMillis")));          dataSource.setMinEvictableIdleTimeMillis(Integer.parseInt(propertyResolver.getProperty("minEvictableIdleTimeMillis")));          dataSource.setValidationQuery(propertyResolver.getProperty("validationQuery"));          dataSource.setTestOnBorrow(Boolean.getBoolean(propertyResolver.getProperty("testOnBorrow")));          dataSource.setTestWhileIdle(Boolean.getBoolean(propertyResolver.getProperty("testWhileIdle")));          dataSource.setTestOnReturn(Boolean.getBoolean(propertyResolver.getProperty("testOnReturn")));          dataSource.setPoolPreparedStatements(Boolean.getBoolean(propertyResolver.getProperty("poolPreparedStatements")));          dataSource.setMaxPoolPreparedStatementPerConnectionSize(Integer.parseInt(propertyResolver.getProperty("maxOpenPreparedStatements")));          try {            dataSource.init();        } catch (SQLException e) {                     }         return dataSource;    }}	
				
			

  推荐阅读

  十年云计算老兵零基础进军深度学习方法论

沙龙晃荡 | 去哪儿、陌陌、ThoughtWorks在主动化运维中的实践!10.28不见不散! 人工智能是当今的热议行业,深度进修是热点中的热点,浪尖上的海潮,但对传统 IT 大年夜业人员来说,人工智>>>详细阅读


本文标题:SpringMVC配置太多?试试SpringBoot

地址:http://www.17bianji.com/lsqh/37982.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)