hyperf 依赖注入 - 胡勇健 - D1h.Net第一号博客
返回主页

胡勇健

hyperf 依赖注入

通过构造方法注入

控制器 app/Controller/IndexController.php

<?php namespace App\Controller; use App\Service\UserService; use Hyperf\HttpServer\Contract\RequestInterface; class IndexController { /** * @var UserService */ private $userService; // 通过在构造函数的参数上声明参数类型完成自动注入 public function __construct(UserService $userService) { $this->userService = $userService; } public function index(RequestInterface $request) { $id = $request->input('id',1); // 直接使用 return $this->userService->getInfoById($id); } } 

service类 app/Service/UserService.php

<?php namespace App\Service; class UserService { public function getInfoById(int $id) { return 'user-service:'.$id; } }

测试访问

curl 118.195.173.53:9501?id=12

结果显示

user-service:12

通过 @Inject 注解注入

控制器 app/Controller/IndexController.php

<?php namespace App\Controller; use App\Service\UserService; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Contract\RequestInterface; class IndexController { /** * 通过 `@Inject` 注解注入由 `@var` 注解声明的属性类型对象 * @Inject * @var UserService */ private $userService; // 通过在构造函数的参数上声明参数类型完成自动注入 public function __construct(UserService $userService) { $this->userService = $userService; } public function index(RequestInterface $request) { $id = $request->input('id',1); // 直接使用 return $this->userService->getInfoById($id); } }

service类 app/Service/UserService.php

<?php namespace App\Service; class UserService { public function getInfoById(int $id) { return 'user-service:'.$id; } }

测试访问

curl 118.195.173.53:9501?id=2

结果显示

user-service:2

抽象对象注入

控制器 app/Controller/IndexController.php

<?php namespace App\Controller; use App\Service\UserService; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Contract\RequestInterface; class IndexController { /** * 通过 `@Inject` 注解注入由 `@var` 注解声明的属性类型对象 * @Inject * @var UserServiceInterface */ private $userService; // 通过在构造函数的参数上声明参数类型完成自动注入 public function __construct(UserService $userService) { $this->userService = $userService; } public function index(RequestInterface $request) { $id = $request->input('id',1); // 直接使用 return $this->userService->getInfoById($id); } }

userService接口 app/Service/UserServiceInterface.php

<?php namespace App\Service; interface UserServiceInterface { public function getInfoById(int $id); }

userService接口实现类 app/Service/Userservice.php

<?php namespace App\Service; class UserService implements UserServiceInterface { public function getInfoById(int $id) { return 'user-service-抽象对象注入:'.$id.PHP_EOL; } }

接口和实现类依赖配置 config/autoload/dependencies.php

<?php return [ \App\Service\UserServiceInterface::class => \App\Service\UserService::class ];

测试访问

curl 118.195.173.53:9501?id=3

结果显示

user-service-抽象对象注入:3

工厂类注入

控制器 app/Controller/IndexController.php

<?php namespace App\Controller; use App\Service\UserServiceInterface; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Contract\RequestInterface; use Psr\Container\ContainerInterface; class IndexController { /** * 通过 `@Inject` 注解注入由 `@var` 注解声明的属性类型对象 * @Inject * @var UserServiceInterface */ private $userService; public function index(RequestInterface $request) { $id = $request->input('id',1); // 直接使用 return $this->userService->getInfoById($id); } }

接口 app/Service/UserServiceInterface.php

<?php namespace App\Service; interface UserServiceInterface { public function getInfoById(int $id); }

工厂类 app/Service/UserServiceFactory.php

<?php namespace App\Service; use Hyperf\Contract\ConfigInterface; use Psr\Container\ContainerInterface; class UserServiceFactory { // 实现一个 __invoke() 方法来完成对象的生产,方法参数会自动注入一个当前的容器实例 public function __invoke(ContainerInterface $container) { $config = $container->get(ConfigInterface::class); //获取author配置 $author = $config->get('author', 'hu'); return make(UserService::class, compact('author')); } }

UserService类 app/Service/UserService.php

<?php namespace App\Service; class UserService implements UserServiceInterface { /** * @var string */ private $author; public function __construct(string $author) { // 接收值并储存于类属性中 $this->author = $author; } public function getInfoById(int $id) { return 'user-service-工厂对象注入1'.$id.',author:'.$this->author.PHP_EOL; } }

接口依赖配置 config/autoload/dependencies.php

<?php declare(strict_types=1); return [ \App\Service\UserServiceInterface::class => \App\Service\UserServiceFactory::class ];

添加author项配置 config/config.php

'author'=>'huyongjian'

测试访问

curl 118.195.173.53:9501?id=4

结果显示

user-service-工厂对象注入4,author:huyongjian
posted @ 2021-09-13 16:35  胡勇健  阅读(22)  评论(0编辑  收藏  举报
Copyright © 2021 胡勇健
Powered by .NET 6 on Kubernetes

问答 28u iTmz.Net 3q科技 A8团队1 A8团队2 A8团队3 A8备