一、创建spring boot 的starter项目,添加依赖,完整pom如下:
4.0.0 cn.edu.tju spring-boot-starter-demo2.0.6 org.springframework.boot spring-boot-starter-parent2.3.7.RELEASE org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-data-rediscom.google.code.gson gson2.8.6 org.mybatis.spring.boot mybatis-spring-boot-starter1.3.2 mysql mysql-connector-java5.1.30 org.springframework.cloud spring-cloud-starter-openfeign2.1.5.RELEASE com.github.pagehelper pagehelper-spring-boot-starter1.2.13 org.springframework.boot spring-boot-autoconfigureorg.springframework.boot spring-boot-autoconfigure-processortrue org.apache.maven.plugins maven-compiler-plugin1.8 1.8
二、创建实体类,这个类将被作为自动装配的bean类
package cn.edu.tju.domain;
public class UserInfo {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int 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 UserInfo(String username, String password) {
this.username = username;
this.password = password;
}
public UserInfo(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "UserInfo{" +
"id=" + id +
", username='" + username + ''' +
", password='" + password + ''' +
'}';
}
}
三、创建配置类,用来从application.properties中读取配置:
package cn.edu.tju.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "tju")
public class MyConfig {
private String username;
private String password;
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;
}
}
四、创建Configuration类,注入步骤三中的配置类,并使用@Bean注解来自动装配bean
package cn.edu.tju.config;
import cn.edu.tju.domain.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Random;
@Configuration
@EnableConfigurationProperties(MyConfig.class)
public class MyBeanConfig {
@Autowired
private MyConfig myConfig;
@Bean
public UserInfo getMyUserInfo(){
String username=myConfig.getUsername();
String password=myConfig.getPassword();
int id= new Random().nextInt(100);
UserInfo userInfo=new UserInfo(id,username,password);
return userInfo;
}
}
五、在项目的resources目录里建目录META-INF,并在META-INF里建spring.factories文件:
org.springframework.boot.autoconfigure.EnableAutoConfiguration= cn.edu.tju.config.MyBeanConfig
六、打包这个项目,在idea里运行
mvn clean package install
,将生成的jar包安装到本地maven仓库。
七、新创建一个spring boot项目,并通过maven引入上述jar包,完整pom如下:
4.0.0 com.example auto-config-test1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent2.3.7.RELEASE org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-data-rediscom.google.code.gson gson2.8.6 org.mybatis.spring.boot mybatis-spring-boot-starter1.3.2 mysql mysql-connector-java5.1.30 org.springframework.cloud spring-cloud-starter-openfeign2.1.5.RELEASE com.github.pagehelper pagehelper-spring-boot-starter1.2.13 cn.edu.tju spring-boot-starter-demo2.0.6 org.springframework.boot spring-boot-maven-plugin
八、配置application.properties,其中配置了starter项目中需要注入的属性:
tju.username
tju.password
server.port=8087 spring.datasource.url=jdbc:mysql://xxx.xxx.xxx.xxx/test spring.datasource.username=root spring.datasource.password=xxxxxxx spring.datasource.driver-class-name=com.mysql.jdbc.Driver tju.username=hi tju.password=hello
九、创建controller,并使用@Autowired注解注入starter项目中定义的实体类(UserInfo)
package com.example.controller;
import cn.edu.tju.domain.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private UserInfo userInfo;
@RequestMapping("/myStarter")
public String getInfo(){
String result=userInfo.toString();
return result;
}
}
十、创建启动类,并启动程序
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Start {
public static void main(String[] args) {
SpringApplication.run(Start.class,args);
}
}
十一、访问接口http://localhost:8087/



