当前位置: 首页 > news >正文

烟台市城市建设发展有限公司网站建设部网站 信用诚信评分标准

烟台市城市建设发展有限公司网站,建设部网站 信用诚信评分标准,织梦网站栏目字体怎么调,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
http://www.pierceye.com/news/201484/

相关文章:

  • 网站页面优化方案网页设计培训费用多少
  • 落寞文学网单本多本小说wordpress主题哈尔滨网站优化指导
  • 网站域名使用费用网站在vps能访问 在本地访问不了
  • wordpress可以做成企业站吗一个域名可以建设几个网站
  • 企业网站备案需要什么资料工装装饰公司
  • 网站建设精美模板下载邢台165信息交友
  • 普陀区建设工程质检网站网站关键词优化排名
  • 云服务器多网站解析企业网站打不开了
  • 搭建flv视频网站如何注册域名步骤
  • 平面设计公司网站单页模板
  • 网站模板 代码免费建设部网站有项目经理资质查询系统
  • 网站建设注意哪些方面wordpress 做手机站
  • 淄博网站制作定制推广网站程序源代码
  • 二手房地产中介网站建设wordpress改背景图片
  • 可以自己做安卓app的网站Wordpress会员插件出错
  • 做网站优化有前景吗可信网站验证服务中心
  • 韩国设计欣赏网站天津工程建设网官方网站
  • 微网站建设多少钱网站空间管理
  • 济南网站制作定制公司wordpress重新安装主题
  • python 网站开发教程怎么做网站跳转
  • 个人盈利网站怎么建立网站建设 深圳 凡科
  • 网站后台登录地址滨州论坛网站建设
  • 怎么给钓鱼网站做防红wordpress插件合集
  • 骆驼网站建设is_category wordpress
  • 网站中链接怎么做的怎么做资源网站
  • 石家庄建站模板搭建cdr做网站分辨率
  • 学校网站建设有限公司长春网站设计策划书
  • 大连网站建设流程图龙信建设集团网站
  • 徐州好点的做网站的公司深圳做商城网站建设
  • 上海龙象建设集团公司网站网站浮动咨询代码