我正在尝试在启动SpringApplication时登记我编写的所有bean。
获取所有列出的bean已完成。这段代码做到了。
String[] beanNames = appContext.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
我尝试使用基本过滤器来查找我的东西。
String[] beanNames = appContext.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
if (beanName.matches("(.*)Controller"))
System.out.println(beanName);
}
但这不起作用,因为当加载类时,它实际上是加载的bean名称而不是类的确切名称。因此,如果我写了一个 my.personal.package.structure.MyController ,它很可能会显示为 myController 。包结构信息丢失,呈现这种技术,并不十分有效。
有没有人尝试过其他有效的方法。我希望这对于开发人员进行调试非常方便。
答案 0 :(得分:4)
使用@Autowired
ConfigurableApplicationContext context;
.....
ConfigurableListableBeanFactory beansFactory = context.getBeanFactory();
String[] beansNames = beansFactory.getBeanDefinitionNames();
Set<String> beansType = new HashSet<>();
for(String beanName : beansNames){
if (beanName.matches("(.*)Controller")){
beansType.add(beansFactory.getType(beanName).toString());
}
}
可以获得所有bean类型。例如:
Spring boot actuator
显示所有bean的其他更简单的选项是
在您使用spring-web的情况下您可以使用pom.xml
。在/bean
中添加下一个dependency,然后转到#include <iostream>
using namespace std;
int getSmallest(int first, int second, int third, int fourth, int fifth);
int getLargest(int first, int second, int third, int fourth, int fifth);
double calculateAverage(int largest, int smallest, int sum);
int main()
{
int first, second, third, fourth, fifth;
int smallest, largest, sum;
//double ave;
//read input of 5 scores from judges
cin >> first;
cin >> second;
cin >> third;
cin >> fourth;
cin >> fifth;
smallest = getSmallest ( first, second, third, fourth, fifth );
largest = getLargest ( first, second, third, fourth, fifth );
sum = (first + second + third + fourth + fifth);
//ave = calculateAverage(largest, smallest, sum);
//cout << ave << endl;
cout << "The average is " << (double)calculateAverage(largest, smallest, sum) << endl;
return 0;
}
int getSmallest(int first, int second, int third, int fourth, int fifth)
{
int smallest = 0;
if ( first <= smallest )
{
smallest = first;
}
if ( second <= smallest )
{
smallest = second;
}
if ( third <= smallest )
{
smallest = third;
}
if ( fourth <= smallest )
{
smallest = fourth;
}
if ( fifth <= smallest )
{
smallest = fifth;
}
return smallest;
}
int getLargest(int first, int second, int third, int fourth, int fifth)
{
int largest = 0;
if ( first >= largest )
{
largest = first;
}
if ( second >= largest )
{
largest = second;
}
if ( third >= largest )
{
largest = third;
}
if ( fourth >= largest )
{
largest = fourth;
}
if ( fifth >= largest )
{
largest = fifth;
}
return largest;
}
double calculateAverage(int largest, int smallest, int sum)
{
return (((double)sum) - ((double)largest + (double)smallest)) / 3.0;
}
端点以显示所有bean,它将显示bean的名称和类。