首页 > 其他分享 >Swoft - Bean

Swoft - Bean

时间:2024-01-11 09:35:43浏览次数:19  
标签:调用 name Swoft Bean values scope testBean

一、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  [email protected]
 * @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  [email protected]
 * @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  [email protected]
 * @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;
    }
}

 

标签:调用,name,Swoft,Bean,values,scope,testBean
From: https://www.cnblogs.com/mklblog/p/17956758

相关文章

  • 解决Django Elastic Beanstalk与RDS MySQL连接问题
    根据错误消息,问题在于您的ElasticBeanstalk环境中缺少MySQL配置。这可能是由于缺少所需的软件包或依赖项导致的。解决此问题的步骤如下:在您的项目根目录中创建一个名为.ebextensions的文件夹。在.ebextensions文件夹中创建一个名为packages.config的文件,并在其......
  • Spring如何利用三级缓存解决单例Bean的循环依赖
    循环依赖:就是N个类循环(嵌套)引用。通俗的讲就是N个Bean互相引用对方,最终形成闭环。用一幅经典的图示可以表示成这样(A、B、C都代表对象,虚线代表引用关系):注意:其实可以N=1,也就是极限情况的循环依赖:自己依赖自己可以设想一下这个场景:如果在日常开发中我们用new对象的方式,若构造......
  • Apache NetBeans IDE 20 输出中文乱码
    修改安装程文件夹下: C:\ProgramFiles\NetBeans-20\netbeans\etc下的 netbeans.conf第五十九行最后加上:  -J-Dfile.encoding=UTF-8 #LicensedtotheApacheSoftwareFoundation(ASF)underone#ormorecontributorlicenseagreements.SeetheNOTICEfile#d......
  • Spring BeanFactoryAware 解决 prototype 作用域失效问题
    跟着孙哥学Spring,b站:https://www.bilibili.com/video/BV185411477k/?spm_id_from=333.337.search-card.all.click在Spring中,如果一个singletonbean依赖了一个prototypebean,那么这个prototypebean在初始化时只会被创建一次,这就是所谓的"prototypescope失效"的问题......
  • springboot 中,ApplicationRunner、InitializingBean、@PostConstruct 执行顺序
    划水。。。ApplicationRunner、InitializingBean、@PostConstruct执行顺序InitializingBean是Spring提供的一个接口,它只有一个方法afterPropertiesSet(),该方法会在容器初始化完成后被调用。ApplicationRunner是SpringBoot提供的一个接口,它有一个方法run(),该方法会在......
  • java浅拷贝BeanUtils.copyProperties引发的RPC异常 | 京东物流技术团队
    背景近期参与了一个攻坚项目,前期因为其他流程原因,测试时间已经耽搁了好几天了,本以为已经解决了卡点,后续流程应该顺顺利利的,没想到人在地铁上,bug从咚咚来~没有任何修改的服务接口,抛出异常:java.lang.ClassCastException:java.util.HashMapcannotbecasttocn.xxx.xxx.xxx.xxx.Ba......
  • java浅拷贝BeanUtils.copyProperties引发的RPC异常 | 京东物流技术团队
    背景近期参与了一个攻坚项目,前期因为其他流程原因,测试时间已经耽搁了好几天了,本以为已经解决了卡点,后续流程应该顺顺利利的,没想到人在地铁上,bug从咚咚来~没有任何修改的服务接口,抛出异常:java.lang.ClassCastException:java.util.HashMapcannotbecasttocn.xxx.xxx.xxx.xx......
  • springboot项目Mapper注入失败:@org.springframework.beans.factory.annotation.Autowi
    同事发给我一个项目,说启动时,报mapper无法注入,让我帮忙排查一下问题记录一下我自己遇到这个问题的排查顺序首先先排除以下问题:1.mapper类是否加入到ioc容器中(有没有使用@Mapper标签),如果报错是service层,那就看看是不是没有添加server标签2.检查项目是否扫描mapper类所在......
  • 深入了解 Spring Boot 核心特性、注解和 Bean 作用域
    SpringBoot是什么?SpringBoot是基于SpringFramework构建应用程序的框架,SpringFramework是一个广泛使用的用于构建基于Java的企业应用程序的开源框架。SpringBoot旨在使创建独立的、生产级别的Spring应用程序变得容易,您可以"只是运行"这些应用程序。术语SpringCor......
  • Spring Bean的生命周期
    在Spring框架中,在IOC容器中管理的Bean分为单例和原型两种,单例Bean在容器启动时就实例化,原型Bean则是每次从容器中请求时才会实例化。而不管是单例还是原型,Bean的生命周期都是基本一致的。生命周期流程图SpringBean的生命周期分为四个阶段:实例化Instantiation-->属性赋值Pop......