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

上海建网站开发公/网络销售适合什么人做

上海建网站开发公,网络销售适合什么人做,凡科网站备案,旅游o2o平台有哪些为什么要 starter ? Spring Boot Starter 的 POM 文件中,引入了各种 jar 文件,当你想拥有某个功能的时候,引入 starter 即可。 比如 常见的 starter 有 spring-boot-starter-web、spring-boot-starter-test、spring-boot-starte…

为什么要 starter ?

Spring Boot Starter 的 POM 文件中,引入了各种 jar 文件,当你想拥有某个功能的时候,引入 starter 即可。

比如 常见的 starter 有 spring-boot-starter-web、spring-boot-starter-test、spring-boot-starter-jdbc,将这些 starter 添加到 POM 文件中,就拥有了对应了能力。

自定义 starter

样例:自定义一个 starter,通过配置文件,输出姓名和年龄

添加依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.3</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>CustomSpringBootStarter</artifactId><version>0.0.1-SNAPSHOT</version><name>CustomSpringBootStarter</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency></dependencies></project>

新建服务类

用于调用测试

public class TestService {private String name;private int age;public TestService(String name, int age) {this.name = name;this.age = age;}public void test(){System.out.println("Hello " + name + ", your age is " + age);}
}

新建属性配置类

读取配置文件

@ConfigurationProperties(prefix = "start")
public class TestProperties {private String name;private int age;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;}
}

编写自动配置类

@Configuration
@ConditionalOnClass(TestService.class)
@EnableConfigurationProperties(TestProperties.class)
public class TestServiceConfiguration {@Autowiredprivate TestProperties testProperties;@Bean@ConditionalOnMissingBeanpublic TestService testService(){return new TestService(testProperties.getName(),testProperties.getAge());}
}

添加配置管理

starter 配置分为被动生效和主动生效两种场景,被动生效就是当它被添加到依赖的项目中时,自动被装载到 Spring 容器中;而主动生效则需要主动声明 starter 才会将 starter 里面声明的 Bean 添加到 Spring 容器中。

  • 被动生效,依赖时就生效
    在 resource/META-INF/ 目录下面,新建 spring.factories 文件
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.example.customspringbootstarter.configuration.TestServiceConfiguration
  • 主动生效

自定义注解,通过 @Import 将配置类引入进来。

测试时需要在启动类上加上该注解才能生效

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(TestServiceConfiguration.class)
public @interface EnableTest {
}

测试验证 starter

仅验证 starter 被动生效场景

  • 新建 Spring Boot 项目,添加上面 starter 依赖

    <dependency><groupId>com.example</groupId><artifactId>CustomSpringBootStarter</artifactId><version>0.0.1-SNAPSHOT</version>
    </dependency>
    
  • application.properties 配置

    start.age = 18
    start.name=Answer
    
  • 运行 Main 函数进行测试输出

@SpringBootApplication
public class CutomerSpringBootStarterTestApplication implements CommandLineRunner {public static void main(String[] args) {SpringApplication.run(CutomerSpringBootStarterTestApplication.class, args);}@Autowiredprivate TestService testService;@Overridepublic void run(String... args) throws Exception {testService.test();}
}输出:.   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v2.5.3)2021-08-02 13:43:04.480  INFO 14400 --- [           main] .CutomerSpringBootStarterTestApplication : Starting CutomerSpringBootStarterTestApplication using Java 1.8.0_211 on Answer with PID 14400 (C:\Users\answer\Documents\CutomerSpringBootStarterTest\target\classes started by answer in C:\Users\answer\Documents\CutomerSpringBootStarterTest)
2021-08-02 13:43:04.483  INFO 14400 --- [           main] .CutomerSpringBootStarterTestApplication : No active profile set, falling back to default profiles: default
2021-08-02 13:43:05.115  INFO 14400 --- [           main] .CutomerSpringBootStarterTestApplication : Started CutomerSpringBootStarterTestApplication in 1.362 seconds (JVM running for 3.115)
Hello Answer, your age is 18

总结

starter 主动生效与被动生效:若在 META-INF/spring.factories 中指定了配置类,那么 starter 将自动被装载到 Spring 容器中;否则需要通过 @Import 方式将配置类装载到 Spring 容器中。

命名规范:自己写的 starter 命名规则建议为 {name}-spring-boot-starter,Spring 官方 starter 的命名通常为 spring-boot-starter-{name}

自动装配流程:Spring Boot 扫描依赖包中的 spring.factories 文件,加载 EnableAutoConfiguration 指定的配置类,将配置类里面的 Bean 加载到 Spring 容器中。

http://www.lbrq.cn/news/822511.html

相关文章:

  • 韩国设计app网站有哪些/百度一下百度网站
  • 那个公司做网站好/苏州网站建设书生商友
  • 山东平台网站建设价格/seo页面优化技术
  • 做p2p网站 人员配置/免费推广引流平台推荐
  • 医院网站开发多少钱/外贸谷歌推广怎么样
  • 创业项目排行榜前十名/seo整站优化更能准确获得客户
  • 八年级学生做的简易网站/自助快速建站
  • 域名解析网站建设/营销推广计划怎么写
  • 网站月付服务器/网站设计平台
  • 建设部网站查询/深圳媒体网络推广有哪些
  • 网站备案包括哪些/推广代理
  • 山西做网站的公司哪个好/清远疫情防控措施
  • 网站备案域名更改吗/广州最新消息
  • 织梦网站分页问题/百度品牌广告多少钱一个月
  • 大良建设网站/为什么不建议去外包公司上班
  • 政府机关备案网站/游戏推广论坛
  • 网站制作软件下载/seo怎么提升关键词的排名
  • 艺麟盛世可以做网站推广吗/哪里做网络推广
  • html5 手机端网站/外贸平台有哪些比较好
  • 公司网站免费申请/seo优化软件免费
  • 网站建设 工具/企业网站seo诊断工具
  • 免费企业营销网站制作/营销培训班
  • 质控中心网站建设申请/产品免费推广网站有哪些
  • 企业网站如何做架构图/博客推广的方法与技巧
  • 那个外贸网站做的最好/seo每日工作内容
  • c2c电商平台网站/产品推销
  • 做产品批发的网站/济南seo小黑seo
  • h5可以来做网站吗/网络营销解释
  • 有没有外国人做发明的网站/软件排名优化
  • 怎么可以自己制作网站/如何免费找精准客户
  • uni-calendar自定义签到打卡颜色
  • 【C语言进阶】动态内存管理(1)
  • web3.0怎么入局
  • solidity从入门到精通 第二章:Solidity初相见
  • 神经网络——线性层
  • PyTorch 实现 CIFAR-10 图像分类:从数据预处理到模型训练与评估