首页 技术 正文
技术 2022年11月14日
0 收藏 864 点赞 4,008 浏览 2910 个字

用过springboot的自动配置会觉得非常方便,我们完全可以自己写一个starter pom,这样不仅可以有自动配置功能,而且具有更通用的的耦合度低的配置,

  • 新建一个starter的maven项目,以idea为例。

Spring boot 自定义一个starter pom

Spring boot 自定义一个starter pom

然后next-填好项目名字和路径finish。建好项目。

在pom.xml中加入springboot的自动配置依赖,代码如下

<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.demo</groupId>
<artifactId>spring-boot-starter-hello</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <name>spring-boot-starter-hello</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
  • 属性配置类,代码如下:
@ConfigurationProperties(prefix = "hello")
public class HelloServiceProperties {
private static final String MSG = "world"; private String msg = MSG; public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}
这里的配置类是类型安全的属性获取。在application.properties 中通过hello.msg=world。
  • 判断依据类,代码如下:
public class HelloService {
private String msg; public String sayHello() {
return "hello " + msg;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}

要根据这个类类判断是否创建这个类的bean,这个类可以是第三方类库的类。

  • 自动配置类
@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "hello",value = "enabled",matchIfMissing = true)
public class HelloServiceAutoConfiguration {
@Autowired
private HelloServiceProperties helloServiceProperties;
@Bean
@ConditionalOnMissingBean(HelloService.class)
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setMsg(helloServiceProperties.getMsg());
return helloService;
}
}

代码解释:

根据HelloServiceProperties提供的参数,并通过@ConditionalOnClass判断HelloService这个类在类路径是否存在,且当容器中没有这个bean的情况下自动配置这个bean。

  • 注册配置,若想自动配置生效,需要注册自动配置类,在src/main/resources下创建META-INF/spring.factories,结构如下图所示:

Spring boot 自定义一个starter pom

spring.factories中填写如下内容注册

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

com.demo.HelloServiceAutoConfiguration

若有多个配置用“,”隔开,“\”是为了换行后能读到属性。

  • 使用stater。我们可以将spring-boot-starter-hello 通过“mvn install”安装到本地库或者发布的maven私服使用。

mvn install:install-file -Dfile=jar包的位置 -DgroupId=groupId -DartifactId=artifactId -Dversion=version -Dpackaging=jar

新建springboot项目,并将我们的starter作为依赖,代码如下:

Spring boot 自定义一个starter pom

测试代码如下:

@Controller
public class IndexController { @Autowired
HelloService helloService; @RequestMapping("/starter")
public @ResponseBody String sayHello(){
return helloService.sayHello();
}
}

在代码中直接注入HelloService的bean,但在项目中,我们没有配置这个bean。这个是通过自动配置完成的。

访问后效果如下:

Spring boot 自定义一个starter pom

然后在application.properties中配置

hello.msg= spring boot

再次访问效果如下:

Spring boot 自定义一个starter pom

在application.properties中配置debug=true,可以看到自动配置的报告。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905