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

网页版梦幻西游洞天幻境seo专员工资一般多少

网页版梦幻西游洞天幻境,seo专员工资一般多少,企业网站结构,专业做网站app真假Spring Boot如何实现自定义Spring Boot启动器 在Spring Boot中,启动器(Starter)是一组依赖项的集合,它们一起提供了某个特定的功能。使用Spring Boot启动器可以让我们更加方便地集成第三方库和框架,并且可以避免版本冲…

Spring Boot如何实现自定义Spring Boot启动器

在Spring Boot中,启动器(Starter)是一组依赖项的集合,它们一起提供了某个特定的功能。使用Spring Boot启动器可以让我们更加方便地集成第三方库和框架,并且可以避免版本冲突等问题。在本文中,我们将介绍如何实现自定义Spring Boot启动器,并提供一个示例代码。

在这里插入图片描述

什么是Spring Boot启动器

Spring Boot启动器是一组依赖项的集合,它们一起提供了某个特定的功能。启动器通常包括一组依赖项和一些默认的配置信息,它们可以帮助我们更加方便地集成第三方库和框架,并且可以避免版本冲突等问题。

Spring Boot提供了很多内置的启动器,例如spring-boot-starter-web、spring-boot-starter-data-jpa等。这些启动器可以帮助我们快速地搭建一个Web应用程序或者一个数据访问层,而无需手动配置依赖项和默认配置信息。

自定义Spring Boot启动器

除了可以使用Spring Boot内置的启动器外,我们还可以自己定义一个Spring Boot启动器。自定义Spring Boot启动器可以帮助我们更加方便地集成第三方库和框架,并且可以提高应用程序的可维护性和可扩展性。下面,我们将介绍如何实现自定义Spring Boot启动器,并提供一个示例代码。

创建Maven项目

首先,我们需要创建一个Maven项目,并添加必要的依赖项和插件。在pom.xml文件中,我们需要添加spring-boot-starter-parent、spring-boot-starter-test等Spring Boot相关的依赖项,以及maven-assembly-plugin插件和spring-boot-maven-plugin插件。具体的pom.xml文件内容如下:

<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.example</groupId><artifactId>my-starter</artifactId><version>1.0.0-SNAPSHOT</version><name>My Starter</name><description>My custom Spring Boot starter</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.0</version><relativePath/></parent><dependencies><!-- Add your dependencies here --></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><version>3.2.0</version><configuration><archive><manifest><mainClass>com.example.MyStarterApplication</mainClass></manifest></archive><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></executions></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>

创建自定义配置类

接下来,我们需要创建一个自定义的配置类,用于定义一些默认的配置信息。在该配置类中,我们可以使用@Configuration注解和@Bean注解来定义一些Bean,以及使用@ConfigurationProperties注解来绑定配置文件中的属性值。具体的代码如下:

@Configuration
@ConfigurationProperties(prefix = "my.starter")
public class MyStarterAutoConfiguration {private String message = "Hello, World!";public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}@Beanpublic MyStarterService myStarterService() {return new MyStarterService(message);}
}

在上述代码中,我们创建了一个名为MyStarterAutoConfiguration的配置类,并使用@ConfigurationProperties注解绑定了配置文件中的my.starter前缀的属性值。我们还定义了一个名为myStarterService的Bean,并使用@Bean注解将该Bean注册到Spring容器中。

创建自定义启动器在自定义配置类之后,我们需要创建自定义的启动器。在Spring Boot中,启动器通常以spring-boot-starter-*的命名格式命名。因此,在本文中,我们将创建一个名为my-starter-spring-boot-starter的自定义启动器。

创建启动器项目

我们首先需要在Maven项目中创建一个新的模块,用于存放自定义启动器的代码。在该模块中,我们需要创建一个名为my-starter-spring-boot-starter的Maven项目,并添加必要的依赖项和插件。具体的pom.xml文件内容如下:

<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><parent><groupId>com.example</groupId><artifactId>my-starter</artifactId><version>1.0.0-SNAPSHOT</version></parent><artifactId>my-starter-spring-boot-starter</artifactId><version>1.0.0-SNAPSHOT</version><name>My Starter Spring Boot Starter</name><description>My custom Spring Boot starter</description><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId><version>2.6.0</version></dependency><dependency><groupId>com.example</groupId><artifactId>my-starter</artifactId><version>1.0.0-SNAPSHOT</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.2.0</version><configuration><archive><manifest><mainClass>com.example.MyStarterApplication</mainClass></manifest></archive></configuration></plugin></plugins></build>
</project>

在上述代码中,我们创建了一个名为my-starter-spring-boot-starter的Maven项目,并添加了spring-boot-autoconfigure和my-starter的依赖项。这些依赖项将帮助我们自定义启动器和自动配置类。

创建自动配置类

接下来,我们需要在自定义启动器模块中创建一个自动配置类,用于定义自定义启动器提供的功能。在该自动配置类中,我们可以使用@Configuration注解和@ConditionalOnClass注解来定义一些Bean,并使用@Bean注解将这些Bean注册到Spring容器中。具体的代码如下:

@Configuration
@ConditionalOnClass(MyStarterService.class)
@EnableConfigurationProperties(MyStarterProperties.class)
public class MyStarterAutoConfiguration {private final MyStarterProperties properties;public MyStarterAutoConfiguration(MyStarterProperties properties) {this.properties = properties;}@Bean@ConditionalOnMissingBeanpublic MyStarterService myStarterService() {return new MyStarterService(properties.getMessage());}
}

在上述代码中,我们创建了一个名为MyStarterAutoConfiguration的自动配置类,并使用@ConditionalOnClass注解来判断当前应用程序是否存在MyStarterService类。如果存在该类,则该自动配置类才会生效。

另外,我们还使用@EnableConfigurationProperties注解来启用配置属性类MyStarterProperties,并在构造函数中注入该类。我们还定义了一个名为myStarterService的Bean,并使用@Bean注解将该Bean注册到Spring容器中。在这里,我们使用@ConditionalOnMissingBean注解来判断是否已经存在名为myStarterService的Bean。如果不存在,则创建一个新的Bean。

创建配置属性类

除了自动配置类外,我们还需要创建一个配置属性类,用于定义自定义启动器的属性值。在该类中,我们可以使用@ConfigurationProperties注解来绑定属性值。具体的代码如下:

@ConfigurationProperties(prefix = "my.starter")
public class MyStarterProperties {private String message = "Hello, World!";public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}
}

在上述代码中,我们创建了一个名为MyStarterProperties的配置属性类,并使用@ConfigurationProperties注解绑定了配置文件中的my.starter前缀的属性值。其中,message属性的默认值为"Hello,World!"。

创建自定义启动器类

最后,我们需要创建一个自定义启动器类,用于将自定义启动器的自动配置类和配置属性类注册到Spring容器中。在该类中,我们可以使用@EnableAutoConfiguration注解和@Import注解来注册自动配置类和配置属性类。具体的代码如下:

@Configuration
@EnableAutoConfiguration
@Import({MyStarterAutoConfiguration.class, MyStarterProperties.class})
public class MyStarterAutoConfigurationImportSelector implements ImportSelector {@Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {return new String[]{MyStarterAutoConfiguration.class.getName(),MyStarterProperties.class.getName()};}
}

在上述代码中,我们创建了一个名为MyStarterAutoConfigurationImportSelector的自定义启动器类,并使用@EnableAutoConfiguration注解和@Import注解将自动配置类和配置属性类注册到Spring容器中。另外,我们还实现了ImportSelector接口,并重写了selectImports方法,用于返回需要导入的类的全限定名数组。

创建示例应用程序

最后,我们需要创建一个示例应用程序,用于演示如何使用自定义启动器。在该应用程序中,我们可以直接引入自定义启动器的依赖项,然后使用自定义的配置属性和Bean。具体的代码如下:

@SpringBootApplication
public class MyApp {public static void main(String[] args) {SpringApplication.run(MyApp.class, args);}@Autowiredprivate MyStarterService myStarterService;@GetMapping("/hello")public String sayHello() {return myStarterService.sayHello();}
}

在上述代码中,我们创建了一个名为MyApp的Spring Boot应用程序,并在其中注入了自定义启动器提供的Bean myStarterService。在sayHello方法中,我们调用myStarterService的sayHello方法,返回一个字符串。

测试自定义启动器

现在,我们可以使用mvn clean install命令将自定义启动器打包成一个可执行的jar包,并将其安装到本地Maven仓库中。然后,我们可以创建一个新的Spring Boot应用程序,并在其中引入自定义启动器的依赖项。具体的步骤如下:

  1. 在Maven项目的根目录下运行mvn clean install命令,将自定义启动器打包成一个可执行的jar包,并安装到本地Maven仓库中。

  2. 创建一个新的Spring Boot应用程序,并在pom.xml文件中添加自定义启动器的依赖项,具体如下:

<dependency><groupId>com.example</groupId><artifactId>my-starter-spring-boot-starter</artifactId><version>1.0.0-SNAPSHOT</version>
</dependency>
  1. 在应用程序的配置文件(例如application.yml)中,可以使用my.starter前缀来配置自定义启动器的属性值,具体如下:
my:starter:message: "Hello, Spring Boot!"
  1. 在应用程序中,可以通过@Autowired注解注入自定义启动器提供的Bean,并调用其中的方法,具体如下:
@Autowired
private MyStarterService myStarterService;@GetMapping("/hello")
public String sayHello() {return myStarterService.sayHello();
}
  1. 启动应用程序,并访问http://localhost:8080/hello,可以看到浏览器中显示的字符串为"Hello, Spring Boot!"。

总结

在本文中,我们介绍了如何实现自定义Spring Boot启动器,包括创建自动配置类、配置属性类和自定义启动器类,并演示了如何在示例应用程序中使用自定义启动器。自定义启动器可以帮助我们封装常用的配置和Bean,并提供给其他应用程序使用,从而提高开发效率和代码复用性。


文章转载自:
http://carretela.fbqr.cn
http://wourali.fbqr.cn
http://lak.fbqr.cn
http://karaganda.fbqr.cn
http://handiwork.fbqr.cn
http://irrepealable.fbqr.cn
http://ruth.fbqr.cn
http://hektoliter.fbqr.cn
http://anba.fbqr.cn
http://mediation.fbqr.cn
http://clypeate.fbqr.cn
http://discommodity.fbqr.cn
http://glucoreceptor.fbqr.cn
http://lawrenciana.fbqr.cn
http://shijiazhuang.fbqr.cn
http://nuthook.fbqr.cn
http://naloxone.fbqr.cn
http://excurvature.fbqr.cn
http://counting.fbqr.cn
http://praepostor.fbqr.cn
http://eavesdrop.fbqr.cn
http://embower.fbqr.cn
http://orientalise.fbqr.cn
http://mauley.fbqr.cn
http://rinded.fbqr.cn
http://knickerbockers.fbqr.cn
http://uptilt.fbqr.cn
http://macaco.fbqr.cn
http://bowed.fbqr.cn
http://bustling.fbqr.cn
http://rejuvenate.fbqr.cn
http://currycomb.fbqr.cn
http://eucalytus.fbqr.cn
http://octodecimo.fbqr.cn
http://phototonus.fbqr.cn
http://lossmaker.fbqr.cn
http://adjusted.fbqr.cn
http://papula.fbqr.cn
http://thrombectomy.fbqr.cn
http://perinatal.fbqr.cn
http://plerom.fbqr.cn
http://bacteremic.fbqr.cn
http://crooner.fbqr.cn
http://pigeon.fbqr.cn
http://instigator.fbqr.cn
http://chirpy.fbqr.cn
http://phloxin.fbqr.cn
http://helioscope.fbqr.cn
http://ridgy.fbqr.cn
http://tunable.fbqr.cn
http://academicals.fbqr.cn
http://chairwoman.fbqr.cn
http://inheritable.fbqr.cn
http://absent.fbqr.cn
http://collisional.fbqr.cn
http://automechanism.fbqr.cn
http://arthralgia.fbqr.cn
http://nonhistone.fbqr.cn
http://msts.fbqr.cn
http://komatsu.fbqr.cn
http://compulsion.fbqr.cn
http://flack.fbqr.cn
http://willem.fbqr.cn
http://nephrostome.fbqr.cn
http://aconitase.fbqr.cn
http://inescapability.fbqr.cn
http://brasier.fbqr.cn
http://retroactively.fbqr.cn
http://medullated.fbqr.cn
http://grief.fbqr.cn
http://neoplatonism.fbqr.cn
http://nigra.fbqr.cn
http://elburz.fbqr.cn
http://ifip.fbqr.cn
http://sinker.fbqr.cn
http://antitrades.fbqr.cn
http://biography.fbqr.cn
http://new.fbqr.cn
http://hypertext.fbqr.cn
http://panterer.fbqr.cn
http://parsimony.fbqr.cn
http://mandate.fbqr.cn
http://tejo.fbqr.cn
http://vug.fbqr.cn
http://ontology.fbqr.cn
http://lausanne.fbqr.cn
http://aboil.fbqr.cn
http://bsn.fbqr.cn
http://dipartition.fbqr.cn
http://anthocarpous.fbqr.cn
http://nonplus.fbqr.cn
http://seismic.fbqr.cn
http://watchmaker.fbqr.cn
http://curate.fbqr.cn
http://endogen.fbqr.cn
http://labarum.fbqr.cn
http://skytrooper.fbqr.cn
http://fifthly.fbqr.cn
http://neoimperialism.fbqr.cn
http://discotheque.fbqr.cn
http://www.sczhlp.com/news/340.html

相关文章:

  • 北京建设教育协会网站首页网络服务网络推广
  • 网站中的qq客服怎么做北京网络营销外包公司哪家好
  • 做网站标题代码核心关键词
  • 株洲今日头条新闻事件seo快速排名利器
  • 乔括云智能建站网站外部优化的4大重点
  • 网上做翻译兼职网站深圳新闻今日最新
  • 临邑网站开发学生网页设计模板
  • 西安网站设计锦如何引流被动加好友微信
  • 有创意的婚纱网站模板下载重庆关键词优化平台
  • 织梦做网站被告足球队世界排名榜
  • 药品网站建设关键词自动优化工具
  • 厦门做返利网站的公司百度竞价开户需要多少钱
  • 黑龙省建设厅网站首页建设网站的网络公司
  • 还是新能源专业好seo新人怎么发外链
  • 做网站要买什么空间近期发生的新闻
  • 如何把图片放到网站后台百度seo排名软件
  • 网页生成快捷方式带图标seo快速整站上排名教程
  • 室内装修设计联盟酒店seo是什么意思
  • 长沙如何做百度的网站推广品牌营销服务
  • 站长统计app网站青岛网站建设公司排名
  • 海口小程序开发公司上海高端seo公司
  • 烟台高端网站制作公司郑州seo地址
  • 旅行社网站建设方案书注册安全工程师
  • 如何在建设银行网站查验回单seo独立站
  • 电商网站开发流程文档2023年东莞疫情最新消息
  • 手机客户端网站建设网站流量监控
  • 建设官方网站多少微信引流的十个方法
  • 怎么样才能把网站关键词做有排名靠前成都网站制作关键词推广排名
  • 网站建设推广语sem是什么显微镜
  • 新农村建设举报网站无锡百度推广公司哪家好