Spring项目替换为SpringBoot

1、pom的修改


    org.springframework.boot
    spring-boot-starter-parent
    2.0.2.RELEASE

    org.springframework.boot
    spring-boot-starter


    org.springframework.boot
    spring-boot-starter-test
    test


    tk.mybatis
    mapper-spring-boot-starter
    2.0.4


    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    1.3.2


    org.springframework.boot
    spring-boot-starter-web
     
        
            org.apache.tomcat
            tomcat-jdbc
        
    


    com.alibaba
    fastjson
    1.2.47


    com.alibaba.boot
    dubbo-spring-boot-starter
    0.1.0


    org.slf4j
    log4j-over-slf4j


    com.101tec
    zkclient
    0.10
    
        
            activation
            javax.activation
        
        
            mail
            javax.mail
        
        
            slf4j-log4j12
            org.slf4j
        
        
            slf4j-api
            org.slf4j
        
        
            log4j
            log4j
        
        
            org.springframework.boot
            logback-classic
        
    


    org.apache.zookeeper
    zookeeper
    3.4.6
    
        
            slf4j-log4j12
            org.slf4j
        
        
            slf4j-api
            org.slf4j
        
    




    org.mariadb.jdbc
    mariadb-java-client
    2.3.0


    org.springframework.boot
    spring-boot-starter-jdbc

    service-omms
    
        
            org.apache.maven.plugins
            maven-compiler-plugin
            
                1.8
                1.8
                UTF-8
            
        
        
            org.apache.maven.plugins
            maven-surefire-plugin
            
                true
            
        
        
            org.springframework.boot
            spring-boot-maven-plugin
            
                
                    
                        repackage
                    
                
            
        
    

2、配置文件的修改

引入application.properties

# springboot的入口 开发时请配置成dev
spring.profiles.include=dev
#spring.profiles.include=production
##############以下公共属性#####################################
#必须改成自己项目名字
spring.boot.admin.client.name=demo
#必须 改为本工程的启动类
develop.startClass=com.ymhy.Application
####################tomcat端口################################
server.port=8080
####################dubbo端口号################################
dubbo.port=20000
dubbo.applicationName=${spring.boot.admin.client.name}
######################日志############################
logging.path=/logs
logging.file=${spring.boot.admin.client.name}.log
#######################mybatis##################################
#改成自己的持久层路径
mybatis.mapper-locations=classpath:com/demo/persist/mapping/*.xml
########################数据库连接池############################
#最大连接池数量
druid.maxActive=10
#最小连接池数量
druid.minIdle=3
#初始化时建立物理连接的个数
druid.initialSize=1
#获取连接时最大等待时间,单位毫秒
druid.maxWait=60000
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis=60000
#配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis=300000
########################redis############################
redis.maxTotal=30
redis.maxIdle=10
redis.numTestsPerEvictionRun=1024
redis.timeBetweenEvictionRunsMillis=3000
redis.minEvictableIdleTimeMillis=1800000
redis.softMinEvictableIdleTimeMillis=10000
redis.maxWaitMillis=1500
redis.testOnBorrow=true
redis.testWhileIdle=true
redis.blockWhenExhausted=false

数据源的类配置:

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * Created by Administrator on 2019/2/18.
 */
@Configuration
public class DruidConfiguration {

    @Value("${druid.maxActive}")
    private String maxActive;
    @Value("${druid.minIdle}")
    private String minIdle;
    @Value("${druid.initialSize}")
    private String initialSize;
    @Value("${druid.maxWait}")
    private String maxWait;
    @Value("${timeBetweenEvictionRunsMillis}")
    private String timeBetweenEvictionRunsMillis;
    @Value("${minEvictableIdleTimeMillis}")
    private String minEvictableIdleTimeMillis;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;
    @Value("${spring.datasource.url}")
    private String dbUrl;
    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;

    @Bean(destroyMethod = "close", initMethod = "init")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        druidDataSource.setUrl(dbUrl);
        druidDataSource.setDriverClassName(driverClassName);
        druidDataSource.setInitialSize(Integer.valueOf(initialSize));
        druidDataSource.setMinIdle(Integer.valueOf(minIdle));
        druidDataSource.setMaxActive(Integer.valueOf(maxActive));
        druidDataSource.setMaxWait(Integer.valueOf(maxWait));
        druidDataSource.setUseGlobalDataSourceStat(true);
        return druidDataSource;
    }
}

Redis的类配置

import com.ymhy.cache.CacheClient;
import com.ymhy.cache.RedisClusterImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisConfig {

    @Value("${redis.maxIdle}")
    private Integer maxIdle;

    @Value("${redis.password}")
    private String password;

    @Value("${redis.maxTotal}")
    private Integer maxTotal;

    @Value("${redis.maxWaitMillis}")
    private Integer maxWaitMillis;

    @Value("${redis.minEvictableIdleTimeMillis}")
    private Integer minEvictableIdleTimeMillis;

    @Value("${redis.numTestsPerEvictionRun}")
    private Integer numTestsPerEvictionRun;

    @Value("${redis.timeBetweenEvictionRunsMillis}")
    private long timeBetweenEvictionRunsMillis;

    @Value("${redis.testOnBorrow}")
    private boolean testOnBorrow;

    @Value("${redis.testWhileIdle}")
    private boolean testWhileIdle;

    @Value("${redis.blockWhenExhausted}")
    private boolean blockWhenExhausted;

    @Value("${redis.softMinEvictableIdleTimeMillis}")
    private Integer softMinEvictableIdleTimeMillis;

    @Value("${redis.address}")
    private String clusterNodes;

    @Value("${spring.profiles.include}")
    private String env;

    /**
     * JedisPoolConfig 连接池
     *
     * @return
     */
    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);
        // 最大空闲数
        jedisPoolConfig.setMaxIdle(maxIdle);
        // 连接池的最大数据库连接数
        jedisPoolConfig.setMaxTotal(maxTotal);
        // 最大建立连接等待时间
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        // 逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
        jedisPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        // 每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
        jedisPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
        // 逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
        jedisPoolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        // 是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
        jedisPoolConfig.setTestOnBorrow(testOnBorrow);
        // 在空闲时检查有效性, 默认false
        jedisPoolConfig.setTestWhileIdle(testWhileIdle);
        jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
        return jedisPoolConfig;
    }

    /**
     * 实例化 RedisTemplate 对象
     *
     * @return
     */
    @Bean
    public CacheClient functionDomainRedisTemplate(JedisPoolConfig jedisPoolConfig) {
        CacheClient cacheClient;
        if ("dev".equals(env) || "jinan".equals(env)) {
            cacheClient = new RedisClusterImpl(clusterNodes, jedisPoolConfig);
        } else {
            cacheClient = new RedisClusterImpl(clusterNodes, password, jedisPoolConfig);
        }
        return cacheClient;
    }

}

引入application-dev.properties

#######################Mysql################################
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
#换成自己项目的数据库地址
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
##############################################################
#此处配置后 会覆盖logback-spring.xml中的配置
logging.level.com=info
##############################################################
#zookeeper.address
zookeeper.address=
##############################################################
#Redis
redis.address=
redis.password=

3、启动类的加入

@SpringBootApplication
@ComponentScan(basePackages={"com.ymhy"})
@ImportResource(locations = {"classpath:spring-dubbo.xml"}) //引入dubbo
@MapperScan("com.ymhy.omms.persist.mapper") //mapper扫描
@EnableScheduling //支持定时任务
@EnableTransactionManagement //支持事务
public class Application {

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

}

4、加入日志配置logback.xml





   
   

   
   
      
         
         %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
      
   

   
   
   
      
         
         ${LOG_HOME}/service-omms.log.%d{yyyy-MM-dd}.log
         
         
         30
      
      
      
         info
         ACCEPT
         DENY
      
      
         
         %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -
            %msg%n
      
      
      
         10MB
      
   

   
   
      
         
         ${LOG_HOME}/service-omms_error.log.%d{yyyy-MM-dd}.log
         
         
         30
      
      
      
         error
         ACCEPT
         DENY
      
      
         
         %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -
            %msg%n
      
      
      
         10MB
      
   

   
   
      
      
      
   


5、spring-dubbo的配置











 

你可能感兴趣的:(Spring项目替换为SpringBoot)