-
新建maven项目(这个地方就不在写步骤)
-
搭建后结构如下:
-
pom.xml内容:
|
<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.0.1.RELEASE</version>
</parent>
<groupId>com.wxt.gt</groupId>
<artifactId>springtest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<filter-resource-name>dev</filter-resource-name>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<filter-resource-name>test</filter-resource-name>
</properties>
</profile>
<profile>
<id>test2</id>
<properties>
<filter-resource-name>test2</filter-resource-name>
</properties>
</profile>
<profile>
<id>test3</id>
<properties>
<filter-resource-name>test3</filter-resource-name>
</properties>
</profile>
<profile>
<id>pre</id>
<properties>
<filter-resource-name>pre</filter-resource-name>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<filter-resource-name>prod</filter-resource-name>
</properties>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
</dependencies>
<build>
<finalName>springtest</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<filters>
<filter>src/main/resources/application-${filter-resource-name}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>filters/*</exclude>
<exclude>filters/*</exclude>
<exclude>application-dev.properties</exclude>
<exclude>application-test.properties</exclude>
<exclude>application-alpha.properties</exclude>
<exclude>application-prod.properties</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application-${filter-resource-name}.properties</include>
</includes>
</resource>
</resources>
</build>
</project>
- 多环境开发,比如测环境,与生产测试环境(application.properties)
# logger
logging.level.root=info
spring.profiles.active=@filter-resource-name@
server.port=8012
server.tomcat.uri-encoding=UTF-8
- main讲解
main里面启动的是config里面的AdminApplication,我们把对应的配置都放到config,
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
//@EnableAdminServer 可以增加admin控制层
@ComponentScan(basePackages = "com.wxt.springtest")
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class AdminApplication {
}
讲解:exclude是因为我们不需要对方提供的数据源,如在pom文件里面引入,jdbc的package包的话,spring boot会去找数据源,如果没有对应的配置就会报错,小G:为什么我这边会写上这个,因为有的公司会自己去写一套数据源配置不在用spring boot,根据公司情况而定,如果使用在application.*.properties配置就可以,配置如下,然后去掉exclude就可以
#spring.data.cassandra.keyspace-name=rima
##\u6D4B\u8BD5\u73AF\u5883
#spring.data.cassandra.contact-points=11.111.111.11
#spring.data.cassandra.username= rima
#spring.data.cassandra.port= 9042
#spring.data.cassandra.password= root
#
#spring.datasource.url = jdbc:mysql://localhost:3306/s?useUnicode=true&characterEncoding=UTF-8&useSSL=false
#spring.datasource.username = root
#spring.datasource.password = root
#spring.datasource.driverClassName = com.mysql.jdbc.Driver
#
-
CorsConfig
这个java是跨域配置 -
控制层,中使用RestController,这个跟spring一直,就不在讲解
@RestController
@RequestMapping(value = "api")
public class App {
private static final Logger logger = LoggerFactory.getLogger(App.class);
@RequestMapping(value = "/test")
public ResponseData check(HttpServletRequest request,HttpServletResponse response){
JSONObject data=new JSONObject();
data.put("y", "ok");
logger.debug("spring test");
return ResponseData.succe(data);
}
}
RestController里面大家可以看一下,其实包含了spring mvc里面的ResponseBody
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody```
解释:spring.profiles.active=@filter-resource-name@ 为激活filter文件,配置多环境pom文件中配置的profile是有关系的
源码下载地址:https://download.csdn.net/download/a372663325/10899408