SpringBoot中自定义Stater
自定义Stater
1. 构建第一个Module
目录结构:
代码:
最底层的配置类:
package com.itheima.springbootconfig;
public class MakyRedis {
String name = "" ;
int id = 20;
public MakyRedis() {
}
public MakyRedis(String name, int id) {
this.name = name;
this.id = id;
System.out.println(name+""+id);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
//=========================================================================================================
package com.itheima.springbootconfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "maky")
public class RdisConfiguraProperties {
String name = "local";
int id = 10;
public RdisConfiguraProperties() {
}
public RdisConfiguraProperties(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
//======================================================================================================
package com.itheima.springbootconfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(RdisConfiguraProperties.class)
public class RedisConfiguration {
@Autowired
private RdisConfiguraProperties maky;
@Bean
public MakyRedis getRedis() {
return new MakyRedis(maky.getName(),maky.getId());
}
}
//======================================================================================================
================ pom文件:=================
org.springframework.boot
spring-boot-configuration-processor
true
Resources目录下要定义META-INF/spring.factories文件,为了让springboot启动时自动加载该文件,从而能够实现自动配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.itheima.springbootconfig.RedisConfiguration
2. 构建第二个Module
该module只负责将第一个module的项目id引入到pom配置文件中,视为starter
com.itheima
springboot-config
0.0.1-SNAPSHOT
4. 构建第三个Module
该module负责调用自定义的starter
1、首先在该module的pom文件中引入第二个项目的id
2、调用方法
com.itheima
springboot-starter
0.0.1-SNAPSHOT
package com.itheima.springbootenable;
import com.itheima.springbootconfig.RedisConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringbootEnableApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(SpringbootEnableApplication.class, args);
RedisConfiguration bean = run.getBean(RedisConfiguration.class);
System.out.println(bean.getRedis());
}
}
可以通过修改该module中的配置文件application.properties(yml) 来指定第一个module中被该注解指定的前缀为maky的值
@ConfigurationProperties(prefix = "maky")
示例:
maky.name=Maky
maky.id=18
目录结构: