如何知道某些 bean 配置被加载到哪里?
How to know where some bean configuration was loaded?
我正在编写一个程序来检查学生提交的源代码是否满足练习描述中给出的所有要求。
现在如果某些 bean 需要从 XML 文件配置,我如何在不解析 XML 配置文件的情况下检查它?我可以在运行时从 spring 容器中获取相关信息吗?
谢谢!
找到bean的来源如下:
BeanDefinition beanDefinition = context.getBeanDefinition("someBean");
beanDefinition.getResourceDescription(); // Will describe if the XML file is the source
示例:
名为 app-config.xml:
的 XML 文件(为清楚起见已删除)
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<bean name="someBean" class="com.Whosebug.aopdemo.service.StudentImpl"
/>
</beans>
显示用法的骨架程序:
@SpringBootApplication
@ImportResource("classpath:app-config.xml")
public class DemoApplication {
@Autowired
private AnnotationConfigApplicationContext context;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@PostConstruct
public void init() {
BeanDefinition beanDefinition = context.getBeanDefinition("someBean");
String resourceDescription = beanDefinition.getResourceDescription();
System.out.println("Resource Description = "+resourceDescription);
}
}
产生以下输出(显示 xml 文件名):
Resource Description = class path resource [app-config.xml]
我正在编写一个程序来检查学生提交的源代码是否满足练习描述中给出的所有要求。
现在如果某些 bean 需要从 XML 文件配置,我如何在不解析 XML 配置文件的情况下检查它?我可以在运行时从 spring 容器中获取相关信息吗?
谢谢!
找到bean的来源如下:
BeanDefinition beanDefinition = context.getBeanDefinition("someBean");
beanDefinition.getResourceDescription(); // Will describe if the XML file is the source
示例:
名为 app-config.xml:
的 XML 文件(为清楚起见已删除)<?xml version="1.0" encoding="UTF-8"?> <beans ...> <bean name="someBean" class="com.Whosebug.aopdemo.service.StudentImpl" /> </beans>
显示用法的骨架程序:
@SpringBootApplication @ImportResource("classpath:app-config.xml") public class DemoApplication { @Autowired private AnnotationConfigApplicationContext context; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @PostConstruct public void init() { BeanDefinition beanDefinition = context.getBeanDefinition("someBean"); String resourceDescription = beanDefinition.getResourceDescription(); System.out.println("Resource Description = "+resourceDescription); } }
产生以下输出(显示 xml 文件名):
Resource Description = class path resource [app-config.xml]