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

Swoft - Bean

一、Bean

在 Swoft 中,一个 Bean 就是一个类的一个对象实例。 它(Bean)是通过容器来存放和管理整个生命周期的。

最直观的感受就是省去了频繁new的过程,节省了资源的开销。

二、Bean的使用

1、创建Bean

在【gateway/app/Http/Controller】下新建一个名为【TestController.php】的文件,文件内容如下。注释:“gateway”为我的项目名称。

<?php declare(strict_types=1);
/*** This file is part of Swoft.** @link     https://swoft.org* @document https://swoft.org/docs* @contact  group@swoft.org* @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE*/namespace App\Http\Controller;use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Bean\Annotation\Mapping\Bean;/*** Class TestController** @since 2.0* @Bean()*/
class TestController
{/*** @RequestMapping("index")**/public function index(){return 'testBean';}
}

2、调用Bean

在【gateway/app/Http/Controller】目录下随便找个已近存在的文件进行编辑调用,本文就选择了【RpcController.php】文件,这个文件在项目创建之初就存在了。

<?php declare(strict_types=1);
/*** This file is part of Swoft.** @link     https://swoft.org* @document https://swoft.org/docs* @contact  group@swoft.org* @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE*/namespace App\Http\Controller;use Swoft\Bean\BeanFactory;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;/*** Class RpcController** @since 2.0** @Controller()*/
class RpcController
{/*** @RequestMapping("list")* @return array*/public function getList(): array{$bean = BeanFactory::getBean(TestController::class);return [$bean->index()];}}

3、展示结果

在【TestController】文件中的【index】方法中返回的内容是“testBean“,调用的时候返回了一个数组。我的浏览器安装了JSON的格式化工具,所以返回的数据都格式化了。

4、说明

在上面两个文件中都有标红的地方,两个文件的标红处是有关联的。

1、在【TestController】文件中,Bean的写法有很多种,如下所示:

  • 第一种
    •   写法:@Bean()
    •   调用:BeanFactory::getBean(TestController::class);
  • 第二种
    •   写法:@Bean("testBean")
    •   调用:BeanFactory::getBean('testBean');
    •   注意点:@Bean("testBean")中的引号一定要双引号,单引号要报错。
  • 第三种
    •   写法:@Bean(name="testBean",scope=Bean::SINGLETON)
    •   调用:BeanFactory::getBean('testBean');
    •   注意点:scope=Bean::SINGLETON 中的scope有三个值,分别是 Bean::SINGLETON(默认), Bean::PROTOTYPE, Bean::REQUEST。
  • 第四种
    •   写法:@Bean(name="testBean",scope=Bean::SINGLETON,alias="testBeanAlias")
    •   调用:BeanFactory::getBean('testBeanAlias');
    •   注意点:alias 表示别名的意思,使用了 alias 那么在调用时可以用别名调用。当然,设置别名后也可以不用别名调用,还是用原来的name也是可以的。

2、关于Bean的源码,可以打开【gateway/vendor/swoft/bean/src/Annotation/Mapping/Bean.php】文件查看。我下载的Swoft版本是2.0的,Bean.php文件内容如下:

<?php declare(strict_types=1);
/*** This file is part of Swoft.** @link     https://swoft.org* @document https://swoft.org/docs* @contact  group@swoft.org* @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE*/namespace Swoft\Bean\Annotation\Mapping;use Doctrine\Common\Annotations\Annotation\Attribute;
use Doctrine\Common\Annotations\Annotation\Attributes;
use Doctrine\Common\Annotations\Annotation\Enum;
use Doctrine\Common\Annotations\Annotation\Target;/*** Class Bean** @Annotation* @Target("CLASS")* @Attributes({*     @Attribute("name", type="string"),*     @Attribute("scope", type="string"),*     @Attribute("alias", type="string"),* })** @since 2.0*/
final class Bean
{/*** Singleton bean*/public const SINGLETON = 'singleton';/*** New bean*/public const PROTOTYPE = 'prototype';/*** New bean from every request*/public const REQUEST = 'request';/*** New bean for one session*/public const SESSION = 'session';/*** Bean name** @var string*/private $name = '';/*** Bean scope** @var string* @Enum({Bean::SINGLETON, Bean::PROTOTYPE, Bean::REQUEST})*/private $scope = self::SINGLETON;/*** Bean alias** @var string*/private $alias = '';/*** Bean constructor.** @param array $values*/public function __construct(array $values){if (isset($values['value'])) {$this->name = $values['value'];}if (isset($values['name'])) {$this->name = $values['name'];}if (isset($values['scope'])) {$this->scope = $values['scope'];}if (isset($values['alias'])) {$this->alias = $values['alias'];}}/*** @return string*/public function getName(): string{return $this->name;}/*** @return string*/public function getScope(): string{return $this->scope;}/*** @return string*/public function getAlias(): string{return $this->alias;}
}
http://www.lryc.cn/news/279563.html

相关文章:

  • 【产品人卫朋】硬件产品经理:从入门到精通
  • swing快速入门(四十)JList、JComboBox实现列表框
  • React Native 原生组件回调JS层方法和 JS 层调用原生组件的事件方法
  • Go-安装与基础语法
  • 【同济子豪兄斯坦福CS224W中文精讲】NetworkX代码学习笔记
  • java+ssm+vue代码视频学习讲解
  • [计算机提升] 创建FTP共享
  • R语言将list转变为dataframe(常用)
  • 【JAVA】OPENGL+TIFF格式图片,不同阈值旋转效果
  • Linux系统中使用ln命令创建软连接
  • Spark---RDD(Key-Value类型转换算子)
  • 后台代码New出来DataGridTextColumn 动态添加到DataGrain 设置 Margin属性
  • MySQL面试题(下)
  • 【Linux】如何检查Linux用户是否具有sudo权限
  • 2024.1.13 Kafka六大机制和Structured Streaming
  • 遥感影像-语义分割数据集:Landsat8云数据集详细介绍及训练样本处理流程
  • YOLOV8在coco128上的训练
  • 设计模式——享元模式
  • 【Python机器学习】分类器的不确定估计——决策函数
  • 云原生周刊:K8sGPT 加入 CNCF | 2024.1.8
  • LightGBM原理和调参
  • ROS无人机开发常见错误
  • Baumer工业相机堡盟工业相机如何联合NEOAPI SDK和OpenCV实现相机图像转换为AVI视频格式(C#)
  • 第一次面试总结 - 迈瑞医疗 - 软件测试
  • 利用Qt输出XML文件
  • OpenWrt智能路由器Wan PPPoE拨号配置方法
  • (十一)IIC总线-AT24C02-EEPROM
  • 现在做电商还有发展空间吗?哪个平台的盈利比较大?
  • 多节点 docker 部署 elastic 集群
  • 2023年全国职业院校技能大赛软件测试赛题—单元测试卷⑨