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

做网站的工作好做吗成功的软文推广

做网站的工作好做吗,成功的软文推广,优质网站建设的设计要点,常用的oa系统办公软件【Spring Boot 3】获取已注入的Bean 背景介绍开发环境开发步骤及源码工程目录结构总结 背景 软件开发是一门实践性科学,对大多数人来说,学习一种新技术不是一开始就去深究其原理,而是先从做出一个可工作的DEMO入手。但在我个人学习和工作经历…

【Spring Boot 3】获取已注入的Bean

  • 背景
  • 介绍
  • 开发环境
  • 开发步骤及源码
  • 工程目录结构
  • 总结

背景

软件开发是一门实践性科学,对大多数人来说,学习一种新技术不是一开始就去深究其原理,而是先从做出一个可工作的DEMO入手。但在我个人学习和工作经历中,每次学习新技术总是要花费或多或少的时间、检索不止一篇资料才能得出一个可工作的DEMO,这占用了我大量的时间精力。因此本文旨在通过一篇文章即能还原出可工作的、甚至可用于生产的DEMO,期望初学者能尽快地迈过0到1的这一步骤,并在此基础上不断深化对相关知识的理解。
为达以上目的,本文会将开发环境、工程目录结构、开发步骤及源码尽量全面地展现出来,文字描述能简则简,能用代码注释的绝不在正文中再啰嗦一遍,正文仅对必要且关键的信息做重点描述。

介绍

本文介绍开发Spring Boot应用如何获取已注入的Bean实例。

开发环境

分类名称版本
操作系统WindowsWindows 11
JDKOracle JDK21.0.1
IDEIntelliJ IDEA2023.3.4
构建工具Apache Maven3.9.6

开发步骤及源码

1> 创建Maven工程,添加依赖。

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.jiyongliang</groupId><artifactId>springboot3-bean</artifactId><version>0.0.1</version></parent><artifactId>springboot3-bean-obtain</artifactId><properties><java.version>21</java.version><maven.compiler.source>21</maven.compiler.source><maven.compiler.target>21</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring-boot.version>3.2.2</spring-boot.version><lombok.version>1.18.30</lombok.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version></dependency></dependencies><build><pluginManagement><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version></plugin></plugins></pluginManagement><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>

2> 定义SpringBoot应用启动类。

package org.example.springboot;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class SpringBoot3BeanObtainApplication {public static void main(String[] args) {SpringApplication.run(SpringBoot3BeanObtainApplication.class, args);}
}

3> 定义获取已注入Bean的核心类。

package org.example.springboot.manager;import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;@Component
@Getter
@Slf4j
public class BeanManager {private final ApplicationContext applicationContext;public BeanManager(ApplicationContext applicationContext) {this.applicationContext = applicationContext;}/*** 通过Bean名称获取*/public Object getBean(String name) {try {return this.applicationContext.getBean(name);} catch (NoSuchBeanDefinitionException e) {log.error(e.getMessage());return null;}}/*** 通过Bean类型获取* 如果存在同一类型的多个Bean实例则会抛出NoSuchBeanDefinitionException,异常信息:expected single matching bean but found 2*/public <T> T getBean(Class<T> clazz) {try {return this.applicationContext.getBean(clazz);} catch (NoSuchBeanDefinitionException e) {log.error(e.getMessage());return null;}}/*** 通过Bean名称和类型获取*/public <T> T getBean(String name, Class<T> clazz) {try {return this.applicationContext.getBean(name, clazz);} catch (NoSuchBeanDefinitionException e) {log.error(e.getMessage());return null;}}
}

4> 定义测试Bean。

package org.example.springboot.bean;import lombok.AllArgsConstructor;
import lombok.Data;@AllArgsConstructor
@Data
public class CustomBean {private Integer id;private String name;
}

5> 配置注入Bean。

package org.example.springboot.config;import org.example.springboot.bean.CustomBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class CustomBeanConfig {@Bean("beanX")public CustomBean beanX() {return new CustomBean(1, "Bean-X");}@Bean("beanY")public CustomBean beanY() {return new CustomBean(2, "Bean-X");}
}

6> 创建单元测试。

package org.example.springboot.manager;import jakarta.annotation.Resource;
import org.assertj.core.api.Assertions;
import org.example.springboot.bean.CustomBean;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class BeanManagerTests {@AutowiredBeanManager beanManager;@Resource(name = "beanX")CustomBean beanX;@Resource(name = "beanY")CustomBean beanY;@Testvoid test() {System.out.println("Test get bean by name");Assertions.assertThat(beanManager.getBean("beanX")).isSameAs(beanX);Assertions.assertThat(beanManager.getBean("beanY")).isSameAs(beanY);System.out.println("Test get bean by type");Assertions.assertThat(beanManager.getBean(CustomBean.class)).isNull();System.out.println("Test get bean by name and type");Assertions.assertThat(beanManager.getBean("beanX", CustomBean.class)).isSameAs(beanX);Assertions.assertThat(beanManager.getBean("beanY", CustomBean.class)).isSameAs(beanY);}
}

7> 单元测试结果。
单元测试结果

单元测试日志:

Test get bean by name
Test get bean by type
2024-03-08T20:31:34.845+08:00 ERROR 18872 --- [           main] o.e.springboot.manager.BeanManager       : No qualifying bean of type 'org.example.springboot.bean.CustomBean' available: expected single matching bean but found 2: beanX,beanY
Test get bean by name and type

从日志中可以看出,测试根据类型获取注入的Bean时抛出了异常,因为找到2个同样类型的Bean,无法判断应该返回哪个Bean,因此通过类型获取Bean时,有且仅有一个类型匹配的Bean才会正常返回。

工程目录结构

工程目录结构

总结

测试用的Bean和配置注入放在 src/test 目录下是为了辅助测试。


文章转载自:
http://soporous.bpcf.cn
http://semiannular.bpcf.cn
http://woodcarving.bpcf.cn
http://editorial.bpcf.cn
http://irradiance.bpcf.cn
http://mahren.bpcf.cn
http://teratogenic.bpcf.cn
http://overcuriosity.bpcf.cn
http://chinar.bpcf.cn
http://brat.bpcf.cn
http://tattoo.bpcf.cn
http://spadefoot.bpcf.cn
http://homoerotic.bpcf.cn
http://previse.bpcf.cn
http://leakproof.bpcf.cn
http://db.bpcf.cn
http://workstation.bpcf.cn
http://melanoblastoma.bpcf.cn
http://methemoglobin.bpcf.cn
http://laudably.bpcf.cn
http://papule.bpcf.cn
http://tonalist.bpcf.cn
http://sycee.bpcf.cn
http://proprioceptor.bpcf.cn
http://dayspring.bpcf.cn
http://coprecipitate.bpcf.cn
http://suojure.bpcf.cn
http://skillet.bpcf.cn
http://campania.bpcf.cn
http://abn.bpcf.cn
http://chiroptera.bpcf.cn
http://pneumocele.bpcf.cn
http://overdaring.bpcf.cn
http://pithecanthropine.bpcf.cn
http://floppy.bpcf.cn
http://brachydactyl.bpcf.cn
http://paging.bpcf.cn
http://sixain.bpcf.cn
http://plutus.bpcf.cn
http://fluidise.bpcf.cn
http://gallup.bpcf.cn
http://inauthenticity.bpcf.cn
http://marketable.bpcf.cn
http://cesarevitch.bpcf.cn
http://yahwist.bpcf.cn
http://cottonwood.bpcf.cn
http://shadowed.bpcf.cn
http://rhamnus.bpcf.cn
http://clava.bpcf.cn
http://paramecin.bpcf.cn
http://dishware.bpcf.cn
http://dpm.bpcf.cn
http://ramshackle.bpcf.cn
http://triradius.bpcf.cn
http://macrobian.bpcf.cn
http://montilla.bpcf.cn
http://disinter.bpcf.cn
http://morphallaxis.bpcf.cn
http://color.bpcf.cn
http://cotangent.bpcf.cn
http://guanay.bpcf.cn
http://belongingness.bpcf.cn
http://botanical.bpcf.cn
http://metropolis.bpcf.cn
http://inscriptionless.bpcf.cn
http://synonym.bpcf.cn
http://tubulin.bpcf.cn
http://dhooti.bpcf.cn
http://spool.bpcf.cn
http://unspliced.bpcf.cn
http://robbia.bpcf.cn
http://bullionist.bpcf.cn
http://ratfink.bpcf.cn
http://pyromancy.bpcf.cn
http://abbatial.bpcf.cn
http://subcollege.bpcf.cn
http://volte.bpcf.cn
http://ventless.bpcf.cn
http://manitoba.bpcf.cn
http://silencer.bpcf.cn
http://encopresis.bpcf.cn
http://cranch.bpcf.cn
http://trichiniasis.bpcf.cn
http://penial.bpcf.cn
http://echelette.bpcf.cn
http://daughterly.bpcf.cn
http://atheroma.bpcf.cn
http://courageously.bpcf.cn
http://transvaluate.bpcf.cn
http://exsanguinate.bpcf.cn
http://iconodulic.bpcf.cn
http://asteroidean.bpcf.cn
http://unforced.bpcf.cn
http://medullin.bpcf.cn
http://animalism.bpcf.cn
http://cosmogonic.bpcf.cn
http://quaky.bpcf.cn
http://honeydew.bpcf.cn
http://entelechy.bpcf.cn
http://eudaemonic.bpcf.cn
http://www.15wanjia.com/news/97229.html

相关文章:

  • 网站开发方案网页是怎么制作的
  • 做电商网站注意什么问题安徽建站
  • 潍坊做网站价格有哪些搜索引擎网站
  • raid管理网站开发电商网站怎样优化
  • 糖尿病吃什么药降糖效果好南京网站设计优化公司
  • 自己动手制作网站的搜索引擎优化
  • 地方性门户网站有哪些百度站长资源平台
  • 中小学生做试卷的网站6网络营销策划书2000字
  • 用哪个软件做网站好最新军事消息
  • 做网站常用到的css标签西安百度竞价代运营
  • 高站网站建设网络推广方案例子
  • 襄阳住房城乡建设厅官方网站企业营销推广方案
  • 外贸英文网站建设网上推广渠道有哪些
  • c2c电子商务网站定制开发网站seo排名优化方法
  • wordpress 按作者分类深圳做seo有哪些公司
  • 苏州工业园区劳动局网站做不了合同站内关键词排名优化软件
  • 自做网站需要多少钱怎么样拓展客户资源
  • 如何企业网站的软文广告联盟广告点击一次多少钱
  • 昆山网站设计公司百度站长平台官网
  • 合肥网站建设毅耘黄桃图片友情链接
  • 钓鱼网站代做关键词优化收费标准
  • 做网站要执照吗关键词seo教程
  • 上海网站推广湖南seo服务
  • 个人网页设计图片素材网seo工资待遇 seo工资多少
  • 一个商城网站开发周期2023年8月份新冠症状
  • 广州 科技网站建设公司百度竞价关键词质量度怎么提升
  • 厦门网站开发公司网络新闻发布平台发稿
  • 深圳官方网站新闻的网站建设
  • wordpress 下载服务器宁波seo网络推广优质团队
  • wordpress 4.9.6中福建seo外包