我自定义了springboot启动程序,该代码在1.5.20版中生效,但在2.0版之后不起作用(我已经在2.0.9、2.1.1和2.1.4版上进行了测试)。
启动器的v1.x.x和v2.x.x有什么区别?
我应该怎么做才能使其在2.x.x版本上正确?
我创建了两个maven项目和一个spring starter项目,这些项目如下:
第一个名为“ share”的项目,它充当第三方jar。
第二个项目名为“ share-spring-boot-starter”,它取决于“ share”的罐子。
第三个项目名为“ springboot-demo”,它充当一个Web项目,取决于“ share-spring-boot-starter”的jar。
注意:第一个项目和第二个项目的jar由命令mvn clean install
构建,并由mvn install:install-file -Dfile=xxx ...
将jar安装到本地Maven存储库。
有关“共享”项目的代码:
ShareDemo.java
package com.example;
import com.alibaba.fastjson.JSONObject;
public class ShareDemo {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String doSomeThing() {
return this.toString();
}
@Override
public String toString() {
return JSONObject.toJSONString(this);
}
}
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>
<groupId>com.example</groupId>
<artifactId>share</artifactId>
<version>1.2.1</version>
<packaging>jar</packaging>
<name>share</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.57</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
有关“ share-spring-boot-starter”项目的代码:
ShareAutoConfiguration.java
package com.example.springboot.autoconfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.example.ShareDemo;
@Configuration
@EnableConfigurationProperties(ShareProperties.class)
public class ShareAutoConfiguration {
@Bean
public ShareDemo getShare(ShareProperties properties) {
ShareDemo shareDemo = new ShareDemo();
shareDemo.setName(properties.getName());
shareDemo.setAge(properties.getAge());
return shareDemo;
}
}
ShareProperties.java
package com.example.springboot.autoconfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "com.study")
public class ShareProperties {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String doSomeThing() {
return this.toString();
}
@Override
public String toString() {
return "从配置中读取到结果:【name=" + name + ",age=" + age + "】";
}
}
spring.factories
#Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.springboot.autoconfiguration.ShareAutoConfiguration
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>
<groupId>com.example</groupId>
<artifactId>share-spring-boot-starter</artifactId>
<version>1.1.0</version>
<packaging>jar</packaging>
<name>share-spring-boot-starter</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springboot.version>1.5.20.RELEASE</springboot.version>
</properties>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>share</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${springboot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${springboot.version}</version>
</dependency>
<!-- 生成配置元数据 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${springboot.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.57</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
有关“ springboot-demo”项目的代码:
UserController.java
package com.example.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.ShareDemo;
/**
* @author zero
*
*/
@RestController
public class UserController {
@Autowired
ShareDemo shareDemo;
@RequestMapping("/share")
public String doSomeThing() {
return shareDemo.doSomeThing();
}
}
application.properties
com.study.name=kitty
com.study.age=26
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>1.5.20.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-demo</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>springboot-demo</name>
<description>Springboot Project</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>share-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</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>
</project>
这些项目的所有代码都可以在这里找到:https://github.com/biaotang/custom-spring-boot-starter
我想在2.x.x版本上运行这些代码,但是它不起作用,并且在“ springboot-demo”项目中发生了编译错误
@Autowired
ShareDemo shareDemo; // can't be resovle the type