【0.SpringBoot专栏的相关文章都在这里哟,后续更多的文章内容可以点击查看】
【1.SpringBoot初识之Spring注解发展流程以及常用的Spring和SpringBoot注解】
【2.SpringBoot自动装配之SPI机制&SPI案例实操学习&SPI机制核心源码学习】
【3.详细学习SpringBoot自动装配原理分析之核心流程初解析-1】
特别说明:感谢恩师dpb,博客:波波烤鸭
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
<version>2.1.6.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.56version>
<optional>trueoptional>
dependency>


public interface FormatProcessor {
/**
* 定义一个格式化的方法
* @param obj
* @param
* @return
*/
<T> String formate(T obj);
}

public class JsonFormatProcessor implements FormatProcessor {
@Override
public <T> String formate(T obj) {
return "JsonFormatProcessor:" + JSON.toJSONString(obj);
}
}

public class StringFormatProcessor implements FormatProcessor {
@Override
public <T> String formate(T obj) {
return "StringFormatProcessor:" + obj.toString();
}
}

@Configuration
public class FormatAutoConfiguration {
@ConditionalOnMissingClass("com.alibaba.fastjson.JSON")
@Bean
@Primary // 优先加载
public FormatProcessor stringFormatProcessor(){
return new StringFormatProcessor();
}
@ConditionalOnClass(name="com.alibaba.fastjson.JSON")
@Bean
public FormatProcessor jsonFormatProcessor(){
return new JsonFormatProcessor();
}
}

public class HelloFormatTemplate {
private FormatProcessor formatProcessor;
public HelloFormatTemplate(FormatProcessor processor){
this.formatProcessor = processor;
}
public <T> String doFormat(T obj){
StringBuilder builder = new StringBuilder();
builder.append("Execute format : ").append("
");
builder.append("Object format result:" ).append(formatProcessor.formate(obj));
return builder.toString();
}
}

@Configuration
@Import(FormatAutoConfiguration.class)
public class HelloAutoConfiguration {
@Bean
public HelloFormatTemplate helloFormatTemplate(FormatProcessor formatProcessor){
return new HelloFormatTemplate(formatProcessor);
}
}

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.mashibingedu.autoconfiguration.HelloAutoConfiguration



<dependency>
<groupId>com.ljwgroupId>
<artifactId>self-starterartifactId>
<version>1.0-SNAPSHOTversion>
dependency>

public class User {
private String username;
private Integer password;
public User(){}
public User(String username,Integer password){
this.username=username;
this.password=password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getPassword() {
return password;
}
public void setPassword(Integer password) {
this.password = password;
}
}
import com.ljw.teststarter.domain.User;
import com.ljw.util.HelloFormatTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private HelloFormatTemplate helloFormatTemplate;
@GetMapping("/format")
public String format(){
User user = new User();
user.setUsername("ljw");
user.setPassword(18);
return helloFormatTemplate.doFormat(user);
}
}

有些情况下我们可以需要用户在使用的时候动态的传递相关的配置信息,比如Redis的ip,端口等等,这些信息显然是不能直接写到代码中的,这时我们就可以通过SpringBoot的配置类来实现。
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<version>2.2.6.RELEASEversion>
<optional>trueoptional>
dependency>

import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Map;
@ConfigurationProperties(prefix = HelloProperties.HELLO_FORMAT_PREFIX)
public class HelloProperties {
public static final String HELLO_FORMAT_PREFIX="com.ljw.hello.format";
private String name;
private Integer age;
private Map<String,Object> info;
public Map<String, Object> getInfo() {
return info;
}
public void setInfo(Map<String, Object> info) {
this.info = info;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}

@Configuration
@Import(FormatAutoConfiguration.class)
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfiguration {
@Bean
public HelloFormatTemplate helloFormatTemplate(HelloProperties helloProperties,FormatProcessor formatProcessor){
return new HelloFormatTemplate(helloProperties,formatProcessor);
}
}

/**
* 自定义一个工具类
*/
public class HelloFormatTemplate {
private FormatProcessor formatProcessor;
private HelloProperties helloProperties;
public HelloFormatTemplate(HelloProperties helloProperties, FormatProcessor processor){
this.helloProperties = helloProperties;
this.formatProcessor = processor;
}
public <T> String doFormat(T obj){
StringBuilder builder = new StringBuilder();
builder.append("Execute format : ").append("
");
builder.append("HelloProperties:").append(formatProcessor.formate(helloProperties.getInfo())).append("
");
builder.append("Object format result:" ).append(formatProcessor.formate(obj));
return builder.toString();
}
}
{
"properties": [
{
"name": "com.ljw.hello.format.name",
"type": "java.lang.String",
"description": "用户名",
"defaultValue": "root"
},{
"name": "mashibing.hello.format.age",
"type": "java.lang.Integer",
"description": "年龄",
"defaultValue": 18
}
]
}

好了,到这里【详细学习SpringBoot自动装配原理之自定义手写Starter-2】就结束了,关于SpringBoot更多优质的内容后续持续更新创作中,谢谢您的关注,笔芯 ^ _ ^