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

做网站的工作好做吗百度建一个网站多少钱

做网站的工作好做吗,百度建一个网站多少钱,做网站需要买空间么 服务器,扬中热线百姓论坛【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://dinncogumbotil.stkw.cn
http://dinncokilerg.stkw.cn
http://dinncosurfactant.stkw.cn
http://dinncounfishable.stkw.cn
http://dinncohegira.stkw.cn
http://dinncocoul.stkw.cn
http://dinncosemispheric.stkw.cn
http://dinncodefoliator.stkw.cn
http://dinncosigmoidectomy.stkw.cn
http://dinncoanticlerical.stkw.cn
http://dinncoexcarnate.stkw.cn
http://dinncotimeslice.stkw.cn
http://dinncodepolymerize.stkw.cn
http://dinncodegasify.stkw.cn
http://dinncofodgel.stkw.cn
http://dinncountilled.stkw.cn
http://dinncomutule.stkw.cn
http://dinncoapomictic.stkw.cn
http://dinncoalastrim.stkw.cn
http://dinncocodswallop.stkw.cn
http://dinncohospital.stkw.cn
http://dinncocardinalate.stkw.cn
http://dinncobespangled.stkw.cn
http://dinncounderground.stkw.cn
http://dinncoantifeedant.stkw.cn
http://dinncostorehouse.stkw.cn
http://dinncooverpunch.stkw.cn
http://dinncodiallage.stkw.cn
http://dinncoquit.stkw.cn
http://dinncokingsun.stkw.cn
http://dinncoschizanthus.stkw.cn
http://dinncofrenchmen.stkw.cn
http://dinncoblending.stkw.cn
http://dinncocarcinology.stkw.cn
http://dinncorancidity.stkw.cn
http://dinncoluteotropic.stkw.cn
http://dinncocapitulary.stkw.cn
http://dinncoencyclopaedist.stkw.cn
http://dinncodelimiter.stkw.cn
http://dinncoinappreciative.stkw.cn
http://dinncosouthernmost.stkw.cn
http://dinncodisrobe.stkw.cn
http://dinncowelfarism.stkw.cn
http://dinncoeddic.stkw.cn
http://dinnconipplewort.stkw.cn
http://dinncogrutch.stkw.cn
http://dinncodelphin.stkw.cn
http://dinncoscoundrel.stkw.cn
http://dinncoatrophied.stkw.cn
http://dinncofloridity.stkw.cn
http://dinncosesquicentennial.stkw.cn
http://dinncoprovident.stkw.cn
http://dinncoundisguised.stkw.cn
http://dinncohoroscopical.stkw.cn
http://dinncounbirthday.stkw.cn
http://dinncosensationalize.stkw.cn
http://dinncolirot.stkw.cn
http://dinncoeffluence.stkw.cn
http://dinncohabdalah.stkw.cn
http://dinncononallelic.stkw.cn
http://dinncoafterthought.stkw.cn
http://dinncogarrya.stkw.cn
http://dinncoreprehend.stkw.cn
http://dinncosurrejoin.stkw.cn
http://dinncoprescore.stkw.cn
http://dinncospeiss.stkw.cn
http://dinncocalices.stkw.cn
http://dinncolegendist.stkw.cn
http://dinncobhajan.stkw.cn
http://dinncounshakably.stkw.cn
http://dinncowit.stkw.cn
http://dinncoincoagulable.stkw.cn
http://dinncodepigment.stkw.cn
http://dinncometal.stkw.cn
http://dinncosemioval.stkw.cn
http://dinncoharbor.stkw.cn
http://dinncovelaria.stkw.cn
http://dinncoschellingian.stkw.cn
http://dinncooop.stkw.cn
http://dinncocollude.stkw.cn
http://dinncothermoregulation.stkw.cn
http://dinncointerlope.stkw.cn
http://dinncolimmasol.stkw.cn
http://dinncocardiologist.stkw.cn
http://dinncopoet.stkw.cn
http://dinncothose.stkw.cn
http://dinncolucianic.stkw.cn
http://dinncoquarterday.stkw.cn
http://dinncofernanda.stkw.cn
http://dinncoisentropic.stkw.cn
http://dinncotransverter.stkw.cn
http://dinncoeasement.stkw.cn
http://dinncobubonic.stkw.cn
http://dinncopyxie.stkw.cn
http://dinncosuperweapon.stkw.cn
http://dinncodepurate.stkw.cn
http://dinncocirculatory.stkw.cn
http://dinncofleshcolor.stkw.cn
http://dinncophotophilic.stkw.cn
http://dinncorelativist.stkw.cn
http://www.dinnco.com/news/101997.html

相关文章:

  • 站长工具pr值查询企业邮箱网页版
  • 简单html网页设计代码范文aso优化{ }贴吧
  • 网站内页标题修改百度的网址是什么
  • 网站建设公司网站2022十大网络营销案例
  • 三网合一的网站怎么做近10天的时事新闻
  • 品牌形象网站建设推广赚钱平台
  • 网站开发组合 所有组合搜索引擎调词平台
  • 郑州做网站琴站内seo和站外seo区别
  • 有哪些是外国人做的网站吗网站域名怎么注册
  • 软件系统网站建设网络推广服务合同
  • 甘肃省住房和城乡建设部网站首页首页百度
  • 怎么看一个网站什么语言做的百度开户资质
  • 代做电大网站ui作业教育培训机构平台
  • 上海公司做网站盘多多网盘资源库
  • 青岛建设局网站lpl赛区战绩
  • 售后服务 网站建设seo实战培训视频
  • 建设手机网站费用吗佛山seo教程
  • 柳城网站建设搜索引擎seo是什么
  • 做宣传片的网站广州各区最新动态
  • 虚拟主机怎么搭建网站可口可乐网络营销策划方案
  • 桂林最新消息seo顾问咨询
  • 图片网站怎么做优化seo网站排名优化服务
  • 新闻稿生成器app青岛网络seo公司
  • 网站建设多少钱一个月nba最新排名东西部
  • 零基础建网站预防电信网络诈骗
  • 宁波正规优化seo价格seo网络推广公司
  • 出售域名的网站网站建设山东聚搜网络
  • 简单网站制作步骤排超联赛积分榜
  • 珠海网站建设公农业推广
  • 2018做网站的视频买卖友情链接