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;
}
}
Swoft - Bean的更多相关文章
- Swoft 容器使用
可以借助Swoft下的Bean类操作容器 示例: 将类绑定至容器 use Swoft\Bean\Annotation\Bean; /** * @Bean("imageLogic") ...
- swoft| 源码解读系列二: 启动阶段, swoft 都干了些啥?
date: 2018-8-01 14:22:17title: swoft| 源码解读系列二: 启动阶段, swoft 都干了些啥?description: 阅读 sowft 框架源码, 了解 sowf ...
- swoft 使用协程 初试
控制器访问 /hi /** * @Swoft\Bean\Annotation\Mapping\Inject("UserService") * @var UserService */ ...
- swoft运行流程
启动命令 php bin/swoft http:start 或者 swoftctl run -c http:start 1 入口文件 bin/swoft.php #!/usr/bin/env php ...
- [转] Swoft HTTP 服务
转载自Go语言中文网, https://studygolang.com/articles/20667 传统架构 PHP-FPM + Nginx 传统架构中所使用的Nginx + PHP-FPM的模型中 ...
- Swoft2.x 小白学习笔记 (一) ---控制器
Swoft通过官方文档进行学习,这里不做介绍,直接上手. 涉及到Swoft方面:(配置.注意的坑) 1.控制器(路由.验证器.中间件) 2.mysql (Model使用).Redis配置及通用池 3 ...
- swoft 源码解读【转】
官网: https://www.swoft.org/ 源码解读: http://naotu.baidu.com/file/814e81c9781b733e04218ac7a0494e2a?toke ...
- Swoft 图片上传与处理
上传 在Swoft下通过 \Swoft\Http\Message\Server\Request -> getUploadedFiles()['image'] 方法可以获取到一个 Swoft\Ht ...
- Swoole和Swoft的那些事 (Http/Rpc服务篇)
https://www.jianshu.com/p/4c0f625d5e11 Swoft在PHPer圈中是一个门槛较高的Web框架,不仅仅由于框架本身带来了很多新概念和前沿的设计,还在于Swoft是一 ...
- Swoft 源码剖析 - Swoole和Swoft的那些事 (Http/Rpc服务篇)
前言 Swoft在PHPer圈中是一个门槛较高的Web框架,不仅仅由于框架本身带来了很多新概念和前沿的设计,还在于Swoft是一个基于Swoole的框架.Swoole在PHPer圈内学习成本最高的工具 ...
随机推荐
- IM开源项目OpenIM部署文档-从准备工作到nginx配置
IM开源项目OpenIM部署文档-从准备工作到nginx配置 2022-11-14 22:27·OpenIM 一.准备工作 运行环境 linux系统即可, Ubuntu 7.5.0-3ubuntu1~ ...
- IDM(最佳的Windows下载工具)
如果你是一名互联网"老司机",那么一定听过「IDM」这款下载工具的大名!它的全名叫做 Internet Download Manager (互联网下载管理器),缩写就是 IDM. ...
- 6.5 Windows驱动开发:内核枚举PspCidTable句柄表
在 Windows 操作系统内核中,PspCidTable 通常是与进程(Process)管理相关的数据结构之一.它与进程的标识和管理有关,每个进程都有一个唯一的标识符,称为进程 ID(PID).与之 ...
- MySQL 索引与性能调优
索引用于快速找出在某个列中有一特定值的行,如果不使用索引MySQL必须从第l条记录开始读完整个表,直到找出相关的行.表越大,查询数据所花费的时间越多,如果表中查询的列有一个索引,MySQL能快速到达某 ...
- LyScriptTools 调试控制类API接口手册
LyScriptTools模块中的DebugControl类主要负责控制x64dbg调试器的行为,例如获取或设置寄存器组,执行单步命令等,此类内的方法也是最常用的. 插件地址:https://gith ...
- centos环境下nginx1.19.7离线升级至1.22.1
环境 centos7 nginx1.19.7 下载新版本nginx 下载地址:http://nginx.org/en/download.html 升级 先看一下原版本: 新安装包传至服务器,升级: # ...
- docker 安装的jenkins如何执行宿主机的shell启动jar包
最近在用docker做自动化部署的时候遇到一个问题,就是用docker装的jenkins可以通过映射执行宿主机脚本.但是,却无法通过shell脚本启动宿主机jar包.经排查最终用以下方案完美解决. 一 ...
- Java商城单体和微服务架构有什么区别
微服务架构 概述 BizSpring移动全端国际化电商平台,是建立在Spring Cloud 基础上的微服务应用,服务化是系统达到一定规模以后的必然选择,主流的互联网公司基本都在迁移到服务化架构. 我 ...
- Java 数字 默认是 Integer类型的问题,System.currentTimeMillis() + (180 * 24 * 60 * 60 * 1000)的问题,剖析、Long + Integer的问题
最终结论: (180 * 24 * 60 * 60) 这种计算表达式在 Java中是默认以 Integer类型来的,若不超过 Integer的最大值则没有问题,若超过则必须用 (180 * 24 * ...
- ABC 313
前三题过水. D题 与 5+*的题解 注意:交互题每输出一次,就要 fflush(stdout); 一次 E 其实不是太难,但是赛时一直在搓 D 还没搓出来 首先如果有两个大于 \(1\) 的数相邻, ...