自定义 spring-boot组件自动注入starter
2023-12-20 03:15:37
1:创建maven项目
2:pom文件
<?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>com.ikeeper</groupId>
<artifactId>my-auto-bean-starter</artifactId>
<version>1.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>my-auto-bean-starter</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
</project>
3:config
package com.ikeeper.config;
import com.ikeeper.properties.MyAutoProperties;
import com.ikeeper.service.DemoService;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties({MyAutoProperties.class})
public class MyAutoBeanConfig {
@Bean
public DemoService demoService(MyAutoProperties myAutoProperties){
DemoService demoService = new DemoService();
demoService.setMyAutoProperties(myAutoProperties);
return demoService;
}
}
4.配置类
package com.ikeeper.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix ="my.auto")
public class MyAutoProperties {
private int point;
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point;
}
}
5:service
package com.ikeeper.service;
import com.ikeeper.properties.MyAutoProperties;
public class DemoService {
private MyAutoProperties myAutoProperties;
public String hi(String name){
return "hello:"+name + "."+myAutoProperties.getPoint();
}
public void setMyAutoProperties(MyAutoProperties myAutoProperties) {
this.myAutoProperties = myAutoProperties;
}
}
6:META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration = \
com.ikeeper.config.MyAutoBeanConfig
7:starter目录结构
8:其他应用业务代码pom依赖
<dependency>
<groupId>com.ikeeper</groupId>
<artifactId>my-auto-bean-starter</artifactId>
<version>1.0.1-SNAPSHOT</version>
</dependency>
9:业务配置
my.auto.point: 10
10:测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {AppMain.class})
public class SpringTest {
@Autowired
DemoService demoService;
@Test
public void hi(){
System.out.println(demoService.hi("henha"));
}
}
文章来源:https://blog.csdn.net/qq_36577291/article/details/135083195
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!