<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.tszr</groupId> <artifactId>chapter023</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> </parent> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>11</source> <target>11</target> </configuration> </plugin> </plugins> </build> </project>
server.port=8081
server.port=8083
server.port=8082
spring.profiles.active=dev
package com.tszr.service; public interface DBConectionService { public String dbConection(); }
package com.tszr.serviceimpl; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import com.tszr.service.DBConectionService; @Configuration @Profile("dev") public class DBConectionServicesImpl implements DBConectionService{ public String dbConection() { return "数据库配置环境dev"; } }
package com.tszr.serviceimpl; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import com.tszr.service.DBConectionService; @Configuration @Profile("prod") public class ProdImpl implements DBConectionService{ public String dbConection() { return "数据库配置环境prod"; } }
package com.tszr.serviceimpl; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import com.tszr.service.DBConectionService; @Configuration @Profile("test") public class TestImpl implements DBConectionService{ public String dbConection() { return "数据库配置环境test"; } }
package com.tszr.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.tszr.service.DBConectionService; @RestController public class DBController { @Autowired private DBConectionService dbConectionService; @GetMapping("/showDB") public String showDB() { String str = dbConectionService.dbConection(); System.out.println(str); return str; } }
package com.tszr; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }