gpt4 book ai didi

java - Spring:在main方法中获取Bean失败

转载 作者:行者123 更新时间:2023-11-30 10:42:40 25 4
gpt4 key购买 nike

我是 Java 和 Spring Framework 的新手。所以这是我正在构建的第一个 Spring 应用程序,我在 Autowiring 我的 Bean 时遇到了问题。

我的应用程序.java:

@SpringBootApplication
public class Application {

public static void main(String[] args) throws Exception {
SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
builder.headless(false);
ConfigurableApplicationContext ctx = builder.run(args);
AppRepository oAppRepo = ctx.getBean(AppRepository.class);
// Check the SystemTray is supported
if (!SystemTray.isSupported()) {
System.out.println("SystemTray is not supported");
return;
}
CustomTray oTray = ctx.getBean(CustomTray.class);
}
}

资源文件夹中我的config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">

<context:property-placeholder location="classpath:/application.properties"/>
<context:component-scan base-package="app"/>
<import resource="beans/*.xml" />
</beans>

我的 beans/tray.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="customTray" class="app.CustomTray">
<property name="schedulerThread" value="app.SchedulerThread"></property>
<property name="commandControlInterface" value="app.CommandControlInterface"></property>
</bean>
</beans>

我在启动时收到的错误消息:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [app.CustomTray] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:371)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:968)
at app.Application.main(Application.java:46)

所以我不明白为什么没有合格的Bean,因为我在tray.xml中注册了bean。 CustomTray 类派生自 TrayIcon 类并创建了一个 TrayIcon,除了完成之外没有什么特别之处,我的自定义托盘:

public class CustomTray extends TrayIcon {

private static final String IMAGE_PATH = "/images/icon.png";
private static final String TOOLTIP = "Test";

private PopupMenu popup;
private SystemTray tray;
@Autowired private AppRepository oAppRepo;

public CustomTray() {
super(createImage(IMAGE_PATH, TOOLTIP), TOOLTIP);
popup = new PopupMenu();
tray = SystemTray.getSystemTray();
try {
this.setup();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@PostConstruct
private void setup() throws AWTException {

}

protected static Image createImage(String path, String description) {
URL imageURL = CustomTray.class.getResource(path);
if (imageURL == null) {
System.err.println("Failed Creating Image. Resource not found: " + path);
return null;
} else {
return new ImageIcon(imageURL, description).getImage();
}
}
}

我之前让应用程序运行,但没有使用 Autowiring 和 Beans,但现在我想重构并设置一个干净的 Spring 应用程序,我希望有人知道我做错了什么。

编辑:好的,现在如果我使用 UUID 提到的注释导入 xml 文件,我会收到另一条错误消息。 bean customTray 有两个属性,SchedulerThread 属性也应该是一个 Singleton Bean。它在 beans/scheduler.xml 中声明,它本身也有一个应该是 bean 的属性。像那样:

调度器.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="schedulerThread" class="app.SchedulerThread">
<property name="processManager" value="app.ProcessManager"></property>
</bean>
</beans>

错误:

Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'app.ProcessManager' for property 'processManager'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [app.ProcessManager] for property 'processManager': no matching editors or conversion strategy found

连接这些类和创建 Bean 的最佳方法是什么?正如我所说,我是 Spring 的新手,我认为我不了解它使用 bean 和 Autowiring 的方式。

最佳答案

在SpringBoot应用中,需要使用ImportResource注解来指定资源

@ImportResource(locations = { "classpath:config.xml", "beans/tray.xml" })

Spring Boot documentation - XML configuration

你可以使用

获取 bean 名称
Arrays.stream(ctx.getBeanDefinitionNames()).forEach(System.out::println);

关于java - Spring:在main方法中获取Bean失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38029112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com