TP5.1:依赖注入、绑定一个类到容器里、绑定一个闭包到容器中

  依赖注入  

1、在application中创建一个文件夹,名字为commom,commom文件夹中创建被注入文件夹,在被注入文件夹中创建一个名为demo.php的文件

  

 

  

  2、在demo.php中输入:

  

  

   

 

  3、再从application中创建一个文件夹,名字为index,在index文件夹中创建一个controller控制器文件夹,在控制器文件夹中创建文件index.php

  

 

  4、在index.php中输入

  

 

  5、在URL中访问:localhost/框架根目录/think/public/index.php/index/index/hello,结果为:

  

 

  绑定一个类到容器:

  前提:接着上面依赖注入的案例继续编写

  1、在controller/index.php中编写

 

 

  2、访问连接:localhost/框架文件目录/think/public/index.php/index/index/setclass,结果为:

 

  绑定一个闭包到容器中

  1、在controller/index.php中增加一个setClosure方法:

 

  2、访问连接:localhost/框架文件目录/think/public/index.php/index/index/setclosure,结果为:

 

转载于:https://www.cnblogs.com/finalanddistance/p/8960669.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要从一个 Java 类找出所有包含跨行闭包的内容,你可以使用正则表达式和多行模式来实现。以下是一个示例代码,演示如何在 Java 类找出所有包含跨行闭包的内容: ```java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { String filePath = "YourJavaFile.java"; String pattern = "\\([^()]*?(?:(?>[^()]+)|(?R))*\\)|\\{[^{}]*?(?:(?>[^{}]+)|(?R))*\\}|\\[[^\\[\\]]*?(?:(?>[^\\[\\]]+)|(?R))*\\]"; List<String> closures = findClosuresInJavaFile(filePath, pattern); for (String closure : closures) { System.out.println(closure); } } private static List<String> findClosuresInJavaFile(String filePath, String pattern) { List<String> closures = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { StringBuilder builder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { builder.append(line).append("\n"); } String fileContent = builder.toString(); Pattern regex = Pattern.compile(pattern, Pattern.DOTALL | Pattern.MULTILINE); Matcher matcher = regex.matcher(fileContent); while (matcher.find()) { String match = matcher.group(); closures.add(match); } } catch (IOException e) { e.printStackTrace(); } return closures; } } ``` 上述代码,我们使用了一个较为复杂的正则表达式模式 `\\([^()]*?(?:(?>[^()]+)|(?R))*\\)|\\{[^{}]*?(?:(?>[^{}]+)|(?R))*\\}|\\[[^\\[\\]]*?(?:(?>[^\\[\\]]+)|(?R))*\\]`,它可以匹配包含跨行闭包的内容。我们还使用了 `Pattern.DOTALL` 和 `Pattern.MULTILINE` 标志,以使点号 `.` 能够匹配包括换行符在内的所有字符,同时使 `^` 和 `$` 可以匹配行的开始和结束位置。 在 `main` 方法,我们调用 `findClosuresInJavaFile` 方法来查找指定 Java 文件闭包。你需要将 `YourJavaFile.java` 替换为你实际的 Java 文件路径。运行代码后,将会输出匹配到的所有包含跨行闭包的内容。 请注意,这个示例假设 Java 文件的编码为 UTF-8,并且文件闭包不会跨越多行。如果你的实际需求有其他特殊情况,请根据需要进行相应的调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值