一、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的更多相关文章

  1. Swoft 容器使用

    可以借助Swoft下的Bean类操作容器 示例: 将类绑定至容器 use Swoft\Bean\Annotation\Bean; /** * @Bean("imageLogic") ...

  2. swoft| 源码解读系列二: 启动阶段, swoft 都干了些啥?

    date: 2018-8-01 14:22:17title: swoft| 源码解读系列二: 启动阶段, swoft 都干了些啥?description: 阅读 sowft 框架源码, 了解 sowf ...

  3. swoft 使用协程 初试

    控制器访问 /hi /** * @Swoft\Bean\Annotation\Mapping\Inject("UserService") * @var UserService */ ...

  4. swoft运行流程

    启动命令 php bin/swoft http:start 或者  swoftctl run -c http:start 1 入口文件 bin/swoft.php #!/usr/bin/env php ...

  5. [转] Swoft HTTP 服务

    转载自Go语言中文网, https://studygolang.com/articles/20667 传统架构 PHP-FPM + Nginx 传统架构中所使用的Nginx + PHP-FPM的模型中 ...

  6. Swoft2.x 小白学习笔记 (一) ---控制器

    Swoft通过官方文档进行学习,这里不做介绍,直接上手. 涉及到Swoft方面:(配置.注意的坑) 1.控制器(路由.验证器.中间件) 2.mysql  (Model使用).Redis配置及通用池 3 ...

  7. swoft 源码解读【转】

      官网: https://www.swoft.org/ 源码解读: http://naotu.baidu.com/file/814e81c9781b733e04218ac7a0494e2a?toke ...

  8. Swoft 图片上传与处理

    上传 在Swoft下通过 \Swoft\Http\Message\Server\Request -> getUploadedFiles()['image'] 方法可以获取到一个 Swoft\Ht ...

  9. Swoole和Swoft的那些事 (Http/Rpc服务篇)

    https://www.jianshu.com/p/4c0f625d5e11 Swoft在PHPer圈中是一个门槛较高的Web框架,不仅仅由于框架本身带来了很多新概念和前沿的设计,还在于Swoft是一 ...

  10. Swoft 源码剖析 - Swoole和Swoft的那些事 (Http/Rpc服务篇)

    前言 Swoft在PHPer圈中是一个门槛较高的Web框架,不仅仅由于框架本身带来了很多新概念和前沿的设计,还在于Swoft是一个基于Swoole的框架.Swoole在PHPer圈内学习成本最高的工具 ...

随机推荐

  1. ABP 使用Except 和EqualityHelper<T> 实现去重

    先上一端代码!!! railwayCar中有10条记录,train参考railwayCar创建了5条记录.要实现,当train再次参考railwayCar创建记录时,使用过的记录在展示列表时不可以再次 ...

  2. 微信小程序-behaviors

    什么是 behaviors behaviors 是用于组件间代码共享的特性,类似于一些编程语言中的 "mixins" 每个 behavior 可以包含一组属性,数据,生命周期函数和 ...

  3. SqlSugar Code First

      注意点 1.SqlSugar Code First可以快速开发,使用起来也要分阶段使用,比如早期随便搞,中后期需要禁用一些功能保证数据安全(标题6和7 ) 2.数据库账号需要有比较高的权限, 3. ...

  4. 2.5 PE结构:导入表详细解析

    导入表(Import Table)是Windows可执行文件中的一部分,它记录了程序所需调用的外部函数(或API)的名称,以及这些函数在哪些动态链接库(DLL)中可以找到.在Win32编程中我们会经常 ...

  5. 从嘉手札<2024-1-17>

    昨天我以为 人生是一场体验 是一辆不会回头的列车 我们遇到了风景 感悟了风景 放下了风景 构成了自己 今天我以为 静水流深.光而不耀 可多思必多疑 思维是一种极为复杂的东西 我曾经觉得知行合一是对自我 ...

  6. 手写 Spring,写到简历上被怼?

    作者:小傅哥 博客:https://bugstack.cn 图书:https://u.jd.com/4LapTH4 沉淀.分享.成长,让自己和他人都能有所收获! 一直都有一个非常好的硬核项目在你我身边 ...

  7. 使用vscode 用git 拉取代码,提示:在签出前,请清理存储库工作树

    使用vscode 用git 拉取代码,提示:在签出前,请清理存储库工作树 如图: 问题: git仓库上的代码和本地代码存在冲突了所以会报这个报错. 解决办法: 手动解决①git stash 先将本地修 ...

  8. Atom N2600, N2800 安装 Ubuntu22.04 卡住的问题处理

    问题描述 Atom N2600, N2800 的某些旧型号机器, 安装 Ubuntu 时在安装界面选择安装后, 启动过程中会卡住, 或者数秒即黑屏, 再无反应. 这个问题对于Debian系的其他发行版 ...

  9. Swoole从入门到入土(18)——WebSocket服务器[心跳ping]

    由于 WebSocket 是长连接,如果一定时间内没有通讯,连接可能会断开.这时候需要心跳机制,WebSocket 协议包含了 Ping 和 Pong 两个帧,可以定时发送 Ping 帧来保持长连接. ...

  10. Oracle数据库报ORA-01078和LRM-00109错误解决方法

    创建实例后,进入sqlplus启动报错:     sqlplus / as sysdba;     SQL*Plus: Release 11.1.0.6.0 - Production on Wed A ...