烟台市城市建设发展有限公司网站,建设部网站 信用诚信评分标准,织梦网站栏目字体怎么调,php培训网站源码在上一篇文章中#xff0c;我们已经了解了一个starter实现自动配置的基本流程#xff0c;在这一小结我们将复现上一过程#xff0c;实现一个自定义的starter。 先来分析starter的需求#xff1a; 在项目中添加自定义的starter依赖#xff0c;自动在Spring中加载starter中的… 在上一篇文章中我们已经了解了一个starter实现自动配置的基本流程在这一小结我们将复现上一过程实现一个自定义的starter。 先来分析starter的需求 在项目中添加自定义的starter依赖自动在Spring中加载starter中的Bean从application.properties中加载指定配置创建项目 先创建一个名为starter的项目。?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdtop.ninwoo/groupIdartifactIddemo-starter/artifactIdversion1.0.0/versionpackagingjar/packagingdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot/artifactIdversion2.1.6.RELEASE/version/dependency/dependencies
/project 在resources中创建一个META-INF的目录并在目录中创建一个spring.factories。在这个配置中我们只设置一个EnableAutoConfiguration项并且对应只设置一个DemoAutoConfig配置类。org.springframework.boot.autoconfigure.EnableAutoConfigurationtop.ninwoo.config.DemoAutoConfig 创建DemoAutoConfig配置类 package top.ninwoo.config;import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
EnableConfigurationProperties(DemoStarterProperties.class)
public class DemoAutoConfig {BeanDemoBean demoBean() {return new DemoBean();}
} 这个配置类我们主要使用了Configuration和EnableConfigurationProperties两个注解。EnableConfigurationProperties启用一个ConfigurationProperties。创建ConfigurationProperties对应的DemoStarterProperties package top.ninwoo.config;import org.springframework.boot.context.properties.ConfigurationProperties;ConfigurationProperties(prefix top.ninwoo.demo)
public class DemoStarterProperties {private String name default;private int age 0;public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}
} 创建一个ConfigurationProperties类。这个类主要用来从application.properties中读取配置项并自动设置到相对应的字段上。创建一个测试用Bean并使用ConfigurationProperties类中的信息。 起初这里有个疑惑不知道如何使用这个ConfigurationProperties类。不过在spring中最常见的就是Bean我们可以大胆的猜测通过ConfigurationProperties注释的类将自动在Spring容器中自动创建一个Bean。而我们在使用的时候就通过普通的bean注入方式便可以使用ConfigurationProperties类中的信息。所以我们这样创建一个测试Bean package top.ninwoo;import javax.annotation.Resource;public class DemoBean {ResourceDemoStarterProperties properties;public String getName() {return properties.getName();}public String getAge() {return getAge();}
}同时在DemoAutoConfig中使用Bean注解创建一个Bean。到这里我们的starter就创建完成了。通过mvn打包或者创建同一个父项目的不同子Module的方式我们可以进行测试这个starter是否生效。 创建测试类 测试类使用一个spring boot web项目来完成主要创建了一个RestController并通过RestController获取Spring上下文中注册的bean names和starter中的测试Bean。 pom.xml ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdtop.ninwoo/groupIdartifactIdspringboot-demo/artifactIdversion1.0.0/versionparentartifactIdspring-boot-starter-parent/artifactIdgroupIdorg.springframework.boot/groupIdversion2.1.6.RELEASE/version/parentdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdtop.ninwoo/groupIdartifactIddemo-starter/artifactIdversion1.0-SNAPSHOT/version/dependency/dependencies
/project 在pom文件中我们添加了刚刚实现的starter。 RestController: RestController
public class IndexController implements ApplicationContextAware {ApplicationContext ctx null;ResourceDemoBean demoBean;RequestMapping(/getList)public String[] getBeanNames() {return ctx.getBeanDefinitionNames();}RequestMapping(/getDemoBean)public String demoBean() {return demoBean.getName();}Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {ctx applicationContext;}
}SpringBoot启动类 MainApp: package top.ninwoo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
public class MainApp {public static void main(String[] args) {SpringApplication.run(MainApp.class, args);}
} 我们可以看到与正常的一个web项目相比我们只是添加了一个依赖而并没有修改启动类。 测试 访问127.0.0.1:8080/getList接口我们可以看到最后的几个bean Names是 ...,top.ninwoo.config.DemoAutoConfig,demoBean,top.ninwoo.demo-top.ninwoo.config.DemoStarterProperties] 这证明通过注入我们starter依赖已经在Spring的上下文创建了starter配置类中的Bean。 在没有设置application.properties时直接访问http://127.0.0.1:8080/getDemoBean可以获取到测试用的Bean实例中默认的参数配置default. 添加application.properties top.ninwoo.demo.namejoliu 重启项目再次访问该接口发现测试用的Bean实例对应的属性已经安装配置类中的参数进行设置返回了joliu。 小结 到这里我们可以说已经了解了开发一个SpringBoot Starter最基本的流程我们可以尝试在我们日常的项目中开发这样的starter。 转载于:https://www.cnblogs.com/NinWoo/p/11305650.html