admin 1 年之前
当前提交
014f52517d
共有 24 个文件被更改,包括 478 次插入0 次删除
  1. 63 0
      format-starter/pom.xml
  2. 26 0
      format-starter/src/main/java/org/example/configuration/FormatAutoConfiguration.java
  3. 16 0
      format-starter/src/main/java/org/example/configuration/FormatProperties.java
  4. 46 0
      format-starter/src/main/java/org/example/entity/User.java
  5. 11 0
      format-starter/src/main/java/org/example/service/FastFormatProcessor.java
  6. 13 0
      format-starter/src/main/java/org/example/service/FormatProcessor.java
  7. 15 0
      format-starter/src/main/java/org/example/service/FormatTemplate.java
  8. 13 0
      format-starter/src/main/java/org/example/service/GsonFormatProcessor.java
  9. 26 0
      format-starter/src/main/java/org/example/service/YKFormatConfiguration.java
  10. 2 0
      format-starter/src/main/resources/META-INF/spring.factories
  11. 48 0
      testProfile/pom.xml
  12. 12 0
      testProfile/src/main/java/org/example/Main.java
  13. 21 0
      testProfile/src/main/java/org/example/controller/controllerBook.java
  14. 22 0
      testProfile/src/main/java/org/example/entity/Book.java
  15. 18 0
      testProfile/src/main/java/org/example/service/DevelopmentHello.java
  16. 5 0
      testProfile/src/main/java/org/example/service/HelloService.java
  17. 16 0
      testProfile/src/main/java/org/example/service/ProductionHello.java
  18. 1 0
      testProfile/src/main/resources/application-dev.properties
  19. 1 0
      testProfile/src/main/resources/application-prod.properties
  20. 2 0
      testProfile/src/main/resources/application.properties
  21. 54 0
      testStarter/pom.xml
  22. 12 0
      testStarter/src/main/java/org/example/MyApplication.java
  23. 31 0
      testStarter/src/main/java/org/example/controller/FormatController.java
  24. 4 0
      testStarter/src/main/resources/application.properties

+ 63 - 0
format-starter/pom.xml

@@ -0,0 +1,63 @@
+<?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>org.example</groupId>
+    <artifactId>format-starter</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.2.2.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>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <version>1.2.56</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.google.code.gson</groupId>
+            <artifactId>gson</artifactId>
+            <version>2.6.2</version>
+        </dependency>
+
+
+
+
+
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 26 - 0
format-starter/src/main/java/org/example/configuration/FormatAutoConfiguration.java

@@ -0,0 +1,26 @@
+package org.example.configuration;
+
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
+import org.example.service.FormatProcessor;
+import org.example.service.FastFormatProcessor;
+import org.example.service.GsonFormatProcessor;
+
+@Configuration
+public class FormatAutoConfiguration {
+
+    @ConditionalOnClass(name="com.alibaba.fastjson.JSON")
+    @Bean
+    @Primary
+    public FormatProcessor fastjsonFormat(){
+        return new FastFormatProcessor();
+    }
+
+    @ConditionalOnClass(name="com.google.gson.Gson")
+    @Bean
+    public FormatProcessor gsonFormat(){
+        return new GsonFormatProcessor();
+    }
+}

+ 16 - 0
format-starter/src/main/java/org/example/configuration/FormatProperties.java

@@ -0,0 +1,16 @@
+package org.example.configuration;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+@ConfigurationProperties(prefix = FormatProperties.ORMAT_PREFIX)
+public class FormatProperties {
+     public static final String ORMAT_PREFIX = "yk.format";
+
+     private String type;
+
+     public String getType(){return type; }
+
+    public void setType(String type) {
+        this.type = type;
+    }
+}

+ 46 - 0
format-starter/src/main/java/org/example/entity/User.java

@@ -0,0 +1,46 @@
+package org.example.entity;
+
+public class User {
+    // 主键
+    private Long id;
+    // 用户名
+    private String username;
+    // 密码
+    private String password;
+    // 姓名
+    private String name;
+
+    //此处省略setter和getter方法... ...
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}

+ 11 - 0
format-starter/src/main/java/org/example/service/FastFormatProcessor.java

@@ -0,0 +1,11 @@
+package org.example.service;
+
+import com.alibaba.fastjson.JSON;
+
+public class FastFormatProcessor implements FormatProcessor{
+
+    @Override
+    public <T> String format(T obj) {
+        return "FastjsonFormatProcessor: " + JSON.toJSONString(obj);
+    }
+}

+ 13 - 0
format-starter/src/main/java/org/example/service/FormatProcessor.java

@@ -0,0 +1,13 @@
+package org.example.service;
+
+public interface FormatProcessor {
+
+    /**
+     * 定义一个格式化的方法
+     *
+     * @param obj
+     * @param <T>
+     * @return
+     */
+    <T> String format(T obj);
+}

+ 15 - 0
format-starter/src/main/java/org/example/service/FormatTemplate.java

@@ -0,0 +1,15 @@
+package org.example.service;
+
+public class FormatTemplate {
+
+    private FormatProcessor formatProcessor;
+
+    public FormatTemplate(FormatProcessor formatProcessor){
+        this.formatProcessor = formatProcessor;
+    }
+
+    public <T> String doFormat(T obj){
+        return formatProcessor.format(obj);
+    }
+
+}

+ 13 - 0
format-starter/src/main/java/org/example/service/GsonFormatProcessor.java

@@ -0,0 +1,13 @@
+package org.example.service;
+
+import com.google.gson.Gson;
+
+public class GsonFormatProcessor implements FormatProcessor{
+
+    @Override
+    public <T> String format(T obj) {
+        Gson gson = new Gson();
+        String jsonStr = gson.toJson(obj);
+        return jsonStr;
+    }
+}

+ 26 - 0
format-starter/src/main/java/org/example/service/YKFormatConfiguration.java

@@ -0,0 +1,26 @@
+package org.example.service;
+
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+import org.example.configuration.FormatAutoConfiguration;
+import org.example.configuration.FormatProperties;
+
+@Import(FormatAutoConfiguration.class)
+@EnableConfigurationProperties(FormatProperties.class)
+@Configuration
+public class YKFormatConfiguration {
+
+    @Bean
+    public FormatTemplate helloFormatTemplate(FormatProperties formatProperties, FormatProcessor formatProcessor) {
+        if ("fastjson".equals(formatProperties.getType())){
+            return new FormatTemplate(new FastFormatProcessor());
+        }
+        if ("gson".equals(formatProperties.getType())){
+            return new FormatTemplate(new GsonFormatProcessor());
+        }
+        return new FormatTemplate(formatProcessor);
+    }
+
+}

+ 2 - 0
format-starter/src/main/resources/META-INF/spring.factories

@@ -0,0 +1,2 @@
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+org.example.service.YKFormatConfiguration

+ 48 - 0
testProfile/pom.xml

@@ -0,0 +1,48 @@
+<?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>org.example</groupId>
+    <artifactId>testProfile</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.2.2.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>
+
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 12 - 0
testProfile/src/main/java/org/example/Main.java

@@ -0,0 +1,12 @@
+package org.example;
+
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class Main {
+    public static void main(String[] args){
+        SpringApplication.run(Main.class, args);
+    }
+}

+ 21 - 0
testProfile/src/main/java/org/example/controller/controllerBook.java

@@ -0,0 +1,21 @@
+package org.example.controller;
+
+import org.example.entity.Book;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@EnableConfigurationProperties({Book.class})
+public class controllerBook {
+
+    @Autowired
+    private Book book;
+
+    @RequestMapping("/book")
+    public Book index(){
+
+        return book;
+    }
+}

+ 22 - 0
testProfile/src/main/java/org/example/entity/Book.java

@@ -0,0 +1,22 @@
+package org.example.entity;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+/**
+ * Created by liz19 on 2017/1/26.
+ */
+@Component
+@ConfigurationProperties(prefix = "book")
+public class Book {
+
+    private String name;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}

+ 18 - 0
testProfile/src/main/java/org/example/service/DevelopmentHello.java

@@ -0,0 +1,18 @@
+package org.example.service;
+
+import org.example.service.HelloService;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+@ConfigurationProperties(prefix = "person")
+@Component
+public class DevelopmentHello implements HelloService {
+
+    //这个值是读取开发环境下的配置文件注入
+    private String name;
+
+    public String sayHello() {
+        return String.format("hello,I'm %s,this is a development environment!", name);
+    }
+
+}

+ 5 - 0
testProfile/src/main/java/org/example/service/HelloService.java

@@ -0,0 +1,5 @@
+package org.example.service;
+
+public interface HelloService {
+    public String sayHello();
+}

+ 16 - 0
testProfile/src/main/java/org/example/service/ProductionHello.java

@@ -0,0 +1,16 @@
+package org.example.service;
+
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+@ConfigurationProperties(prefix = "person")
+public class ProductionHello {
+    //这个值读取生产环境下的配置注入
+    private String name;
+
+    public String sayHello() {
+        return String.format("hello,I'm %s,this is a produce environment!",
+                name);
+    }
+}

+ 1 - 0
testProfile/src/main/resources/application-dev.properties

@@ -0,0 +1 @@
+book.name=spring boot dev

+ 1 - 0
testProfile/src/main/resources/application-prod.properties

@@ -0,0 +1 @@
+book.name=spring boot prod

+ 2 - 0
testProfile/src/main/resources/application.properties

@@ -0,0 +1,2 @@
+spring.profiles.active=dev
+

+ 54 - 0
testStarter/pom.xml

@@ -0,0 +1,54 @@
+<?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>org.example</groupId>
+    <artifactId>testStarter</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.2.2.RELEASE</version>
+    </parent>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.example</groupId>
+            <artifactId>format-starter</artifactId>
+            <version>1.0-SNAPSHOT</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-actuator</artifactId>
+            <version>3.1.0</version>
+        </dependency>
+
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 12 - 0
testStarter/src/main/java/org/example/MyApplication.java

@@ -0,0 +1,12 @@
+package org.example;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class MyApplication {
+    public static void main(String[] args) {
+        SpringApplication.run(MyApplication.class, args);
+    }
+}

+ 31 - 0
testStarter/src/main/java/org/example/controller/FormatController.java

@@ -0,0 +1,31 @@
+package org.example.controller;
+
+import org.example.entity.User;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Import;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestController;
+import org.example.service.FormatTemplate;
+
+import javax.jws.soap.SOAPBinding;
+import java.util.Iterator;
+
+
+@RestController
+public class FormatController {
+    @Autowired
+    FormatTemplate formatTemplate;
+
+    @RequestMapping("/hello")
+    public String formatTest(){
+        User user = new User();
+        user.setId(783L);
+        user.setName("efse");
+        user.setPassword("abcdefg");
+        user.setUsername("Geralt");
+        return formatTemplate.doFormat(user);
+    }
+
+}

+ 4 - 0
testStarter/src/main/resources/application.properties

@@ -0,0 +1,4 @@
+#yk.format.type=fastjson
+yk.format.type=gson
+
+management.endpoints.web.exposure.include=*