java - interfacename.class 返回什么?
问题描述
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext.xml");
//retrieve bean from the spring container
Coach theCoach = context.getBean("myCoach",Coach.class);
//call methods on the bean
System.out.println(theCoach.getDailyWorkout());
System.out.println(theCoach.getDailyFortune());
System.out.println(theCoach.getEmailAddress());
System.out.println(theCoach.getTeam());
//close the context
context.close();
在上面的代码中,Coach.class 返回了什么
教练是一个界面。
解决方案
Coach.class
返回描述 Coach 接口结构的Class对象的实例。
假设我们有这个例子:
public interface Coach {
int getName();
}
如果您将执行这些行:
Class<Coach> coachClass = Coach.class;
System.out.println(coachClass.getName());
for(Method m:coachClass.getMethods()) {
System.out.println(m.getName());
}
您将获得接口的全名和声明的方法的名称。
简而言之Coach.class
,返回界面的元数据。如果您想了解有关此主题的更多信息,那么我建议您阅读Java 反射
在您的情况下,这行代码:
Coach theCoach = context.getBean("myCoach",Coach.class);
返回实现 Coach 接口的对象的实例。您必须传递Coach
接口的元数据,以便 Spring 知道您要查找的数据类型。
推荐阅读
- python - 卤化物非连续内存布局
- c# - 在 PowerShell 中提供 C# 抽象类的实现
- django - 在 django admin 中将所有 list_display 字段导出到 csv
- docker - 用于 wss 的 jitsi docker desktop wsl2 nginx 代理
- python - 这个可以去掉吗?“索引:[],空数据帧”并正确读入
- javascript - 如何重新创建可点击的图像轮播?
- python - 访问 DataFrame 元素的速度出奇的慢
- asp.net-core - Asp.net core,调试时绕过授权
- laravel - 使用 gmail smtp-relay(数据库队列驱动程序)的 laravel swift-mailer 异常“预期响应代码 250,但得到一个空响应”
- android - 在创建活动 Android 之前启动协程