码迷,mamicode.com
首页 > 编程语言 > 详细

springboot

时间:2019-01-13 21:13:35      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:ada   time   table   问题   指定路径   依赖   增加   nfa   mapper   

1.pom配置

遇到的问题:

1.根pom依赖的jar,maven插件子模块共享。

2.如果不需要向私服发包,install、deploy,就不需要配置distributionManagement

技术分享图片

根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>

    <groupId>com.shfb</groupId>
    <artifactId>oa</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </parent>

    <modules>
        <module>report</module>
        <module>common</module>
    </modules>
    <properties>
        <java.version>1.8</java.version>
        <java.encoding>UTF-8</java.encoding>
    </properties>

    <dependencies>
        <!--springboot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <encoding>${java.encoding}</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-javadoc-plugin</artifactId>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <testFailureIgnore>false</testFailureIgnore>
                    <forkMode>once</forkMode>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

report_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>
        <artifactId>oa</artifactId>
        <groupId>com.shfb</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <packaging>jar</packaging>
    <artifactId>report</artifactId>

    <dependencies>
        <!--common util-->
        <dependency>
            <groupId>com.shfb</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.0</version>
        </dependency>
    </dependencies>

</project>

common_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">
    <parent>
        <artifactId>oa</artifactId>
        <groupId>com.shfb</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>
    <artifactId>common</artifactId>


    <!--发布到私服install、deploy-->
    <distributionManagement>
        <repository>
            <id>shfb-release</id>
            <name>User Project Release</name>
            <url>http://101.201.31.68:8888/repository/shfb-release/</url>
        </repository>
        <snapshotRepository>
            <id>shfb-snapshots</id>
            <name>User Project SNAPSHOTS</name>
            <url>http://101.201.31.68:8888/repository/shfb-release/</url>
        </snapshotRepository>
    </distributionManagement>
</project>

 

数据源配置

遇到的问题:

1.@value注入不进来,原因:application.properties未找到,需要直接挂在classpath下。也可以通过@PropertySource(value={"classpath:application.properties"})指定路径

DataSourceConfig.java

package com.shfb.easyoa.report.config.datasource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * 数据源配置
 * Created by shenjp on 2019/1/13.
 **/
@Configuration
public class DataSourceConfig {

    @Value("${easyoa.report.datasource.username}")
    private String username;

    @Value("${easyoa.report.datasource.password}")
    private String password;

    @ConfigurationProperties(prefix = "easyoa.report.datasource")
    @Bean(name = "dataSource")
    public DataSource dataSource() {
        return DataSourceBuilder.create()
                .username(this.username)
                .password(this.password)
                .build();
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") final DataSource dataSource)
            throws Exception {
        final SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }
}

application.properties

server.port=8080
server.servlet.context-path=/easyoa/report

mybatis.type-aliases-package=com.shfb.easy.report
mybatis.mapper-locations=classpath:mapper/*.xml

easyoa.report.datasource.type=com.alibaba.druid.pool.DruidDataSource
easyoa.report.datasource.driver-class-name=com.mysql.jdbc.Driver
easyoa.report.datasource.jdbc-url=***
easyoa.report.datasource.username=***
easyoa.report.datasource.password=***
easyoa.report.datasource.max-active=10
easyoa.report.datasource.min-idle=0
easyoa.report.datasource.maxWait=60000
easyoa.report.datasource.timeBetweenEvictionRunsMillis=60000
easyoa.report.datasource.minEvictableIdleTimeMillis=300000
easyoa.report.datasource.validationQuery=SELECT ‘x‘
easyoa.report.datasource.testWhileIdle=true
easyoa.report.datasource.testOnBorrow=false
easyoa.report.datasource.testOnReturn=false
easyoa.report.datasource.filters=stat,log4j

多环境部署

技术分享图片

 

 本地开发:在application.properties中增加配置:spring.profiles.active=dev

 部署:java -jar xxx.jar --spring.profiles.active=prod

springboot获取配置

遇到的问题:

1.static 变量在启动时注入不进来,解决:

@Configuration
public class PcacIntegrationUtil {

    private static String publicCertFilePath;

    @Value("${pcacmgr.publicCertFilePath}")
    public void setPublicCertFilePath(String publicCertFilePath) {
        PcacIntegrationUtil.publicCertFilePath = publicCertFilePath;
    }
    
}

 

1.利用@PropertySource获取resource下面的资源,Environment获取属性

@Configuration
@EnableTransactionManagement
@PropertySource(value = {"classpath:DBsource.properties"})
public class DBBeanConfig {
    @Autowired
    private Environment env;

    @Bean(destroyMethod = "close")
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(env.getProperty("source.driverClassName").trim());
        dataSource.setUrl(env.getProperty("source.url").trim());
        dataSource.setUsername(env.getProperty("source.username").trim());
        dataSource.setPassword(env.getProperty("source.password").trim());
        return dataSource;
    }
}

2.@PropertySource获取resource下面的资源,@ConfigurationProperties找到该资源的头部@Value注入属性

 

@PropertySource({"classpath:redis-config.properties"})
@ConfigurationProperties(prefix="spring.redis")
public class RedisConfig {

    @Value("${host}")
    private  String  host;
    @Value("${port}")
    private  int  port;
}

3.@ConfigurationProperties找到该资源的头部,通过getter、setter方法注入及获取配置

@PropertySource({"classpath:redis-config.properties"})
@ConfigurationProperties(prefix="spring.redis")
public class RedisConfig2 {

    private  String  host;
    private  int  port;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
}

4.若是获取application.yml、application.properties中的属性,可以直接注入

@Value("${spring.redis.host}")

 

5.利用PropertiesLoaderUtil加载配置文件

public class RedisConfig3 {

    public static String  host;
    public static int  port;
    private static String property="redis-config.properties";

    private static RedisConfig3 mConfig;
    static {
        mConfig=loadConfig();
    }
    public  static  RedisConfig3 loadConfig(){
        if (mConfig==null){
            mConfig=new RedisConfig3();
            Properties properties = null;
            try {
                properties = PropertiesLoaderUtils.loadAllProperties(property);

                host=properties.getProperty("spring.redis.host");
                port=Integer.valueOf(properties.getProperty("spring.redis.port"));
                System.out.println(host+":"+port);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return mConfig;
    }

    public RedisConfig3 getInstance(){
        return mConfig;
    }
}

 

springboot

标签:ada   time   table   问题   指定路径   依赖   增加   nfa   mapper   

原文地址:https://www.cnblogs.com/sjp007/p/10263813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!