赞
踩
SprintBoot对应目录结构和包的规范与通配的项目开发基本类似,只是多了项目主包;
1.目录及包结构:
可以根据实际项目要求进行包构建;
注意:项目进行包构建,那么作为AppConfig作为配置类,要加入@ComponentScan()注解对工程进行扫描,其要求数组参数,加入你要扫描的包路径(开发前就要设置完成)
package com.zxy.springbootquickstart.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
/**实现对于其他xml的配置与自定义需要扫描的文件或类,通常创建XXconfig;
* 1.创建AppConfig类增加Configuration;
* 2.Configuration是springframework即spring提供;
* 3.使用此注解代表让整个spring容器识别此类,并且加载此类;
* 4.可以理解为此类就是一个xml文件,被天然的引入到spring中;
* @author zxy
*
*/
@Configuration
@ComponentScan({"com.zxy.springBootquickstart.*"}) //全项目扫描加载
public class AppConfig {
/**
* 1.数据源配置
* 2.参数配置
* 都可以直接写入此类
*/
/**
* 调用application.properties中自定义属性的方法有两种:
* 1. 通过注解的形式(推荐)
*/
@Value("${custom.group}")
private String customGroup;
/**
* 2.通过注入全局类方式
*/
@Autowired
private Environment environment;
public void output() {
System.out.println("通过@Value注解读取配置:"+this.customGroup);
System.out.println("通过注入Environment对象读取配置:"+environment.getProperty("custom.team"));
}
}
2.自定义配置文件my-config.properties,并通过AppConfig加载:
启动项目运行结果如下:
3.生产环境中动态设置:(为什么要动态设置?在正式生产环境中,由于存在着生产环境配置、测试环境配置、开发环境配置都不尽相同,配置文件要根据不同环境来更改不同的配置内容,例如,端口号、数据源、阈值、阀值等等,)
更改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-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zxy</groupId>
<artifactId>springboot-quickstart</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-quickstart</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<!-- profile 配置动态切换:mvn clean install -P dev/prod/local/test -->
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
</dependencies>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
<zxy.dynamic>2019-001</zxy.dynamic><!-- 自定义属性 -->
</properties>
</profile>
<profile>
<id>prod</id>
<!--
<activation>
<activeByDefault>true</activeByDefault>
</activation>
-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
</dependencies>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
<zxy.dynamic>2019-002</zxy.dynamic>
</properties>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
-->
<build>
<!-- 定义工程打包名字 -->
<finalName>springboot-001-quickstart</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 解决maven update project 后版本降低为1.5的bug -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- maven打包时动态切换:mvn clean install -P dev/prod/local/test -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>default-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/classes</outputDirectory>
<useDefaultDelimiters>false</useDefaultDelimiters>
<delimiters>
<delimiter>$</delimiter><!-- 自定意通配符,在$符合中的東西可以在配置文件中使用 -->
</delimiters>
<resources>
<!-- 打包时包含propertise、xml -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<!-- 是否替換資源中的屬性 -->
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources/</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.yml</include>
<include>**/*.properties</include>
</includes>
</resource>
<!-- 配置一些不需要 -->
<resource>
<directory>src/main/resources/</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/*.xml</exclude>
<exclude>**/*.yml</exclude>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!-- 单元测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip><!-- 跳过测试,保障打包时让测试 -->
<includes>
<include>**/*Test*.java</include>
</includes>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<!-- 绑定到特定的生命周期之后,运行maven-source-pluin 运行目标为jar-no-fork -->
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
4.修改代码,测试动态打包效果:
修改Appconfig代码:
package com.zxy.springbootquickstart.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
/**实现对于其他xml的配置与自定义需要扫描的文件或类,通常创建XXconfig;
* 1.创建AppConfig类增加Configuration;
* 2.Configuration是springframework即spring提供;
* 3.使用此注解代表让整个spring容器识别此类,并且加载此类;
* 4.可以理解为此类就是一个xml文件,被天然的引入到spring中;
* @author zxy
*
*/
@Configuration
@ComponentScan({"com.zxy.springBootquickstart.*"}) //全项目扫描加载
@PropertySource("classpath:my-config.properties") //加载指定的配置
public class AppConfig {
/**
* 1.数据源配置
* 2.参数配置
* 都可以直接写入此类
*/
/**
* 调用application.properties中自定义属性的方法有两种:
* 1. 通过注解的形式(推荐)
*/
@Value("${custom.group}")
private String customGroup;
@Value("${zxy.name}")
private String name;
@Value("${zxy.title}")
private String title;
@Value("${zxy.attr}")
private int attr;
/**
* 读取自定义配置文件my-config.properties中zxy.dynamic属性值,
* my-config.properties中zxy.dynamic是根据$zxy.dynamic$获取
* pom.xml中的配置值:
* <properties>
<spring.profiles.active>dev</spring.profiles.active>
<zxy.dynamic>2019-001</zxy.dynamic><!-- 自定义属性 -->
</properties>
*/
@Value("${zxy.dynamic}")
private String dynamic;
/**
* 2.通过注入全局类方式
*/
@Autowired
private Environment environment;
public void output() {
System.out.println("通过@Value注解读取配置:"+this.customGroup);
System.out.println("通过注入Environment对象读取配置:"+environment.getProperty("custom.team"));
System.out.println("加载自己的配置文件内容:"+this.name+","+this.title+","+this.attr);
System.out.println("加载自己的配置文件,动态获取pom里的参数:"+this.dynamic);
}
}
使用maven打包:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。