- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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/
因此,当使用“智能”时,当我想创建一个类时,它还会创建另外两个函数(不确定我是否正确调用了它们): class main { private: /* data */ public: m
我确实知道 C/C++ 和 Java 中使用的 main() 方法,但由于 main() 是用户定义的(因为 main() 中的代码是由用户定义的,它不能是预定义的方法) & 在 C/C++ 中,ma
这个问题在这里已经有了答案: What is a NullPointerException, and how do I fix it? (12 个答案) 关闭 7 年前。 我意识到这是一个常见错误,
您好,我是 jquery 和 javascript 的新手。我想做的是我有 3 个独立的 Main-Divs ex main-div1, main-div2, main-div-3 它们都是一个大盒子
我知道以前曾有人问过有关此错误的问题,但我的情况与其他人不同。我正在编写计算数据集的平均值、方差和标准差的代码。编译代码时我没有收到任何错误,但是当我尝试运行代码时,我收到如下错误: Exceptio
这个问题已经有答案了: What should main() return in C and C++? (19 个回答) Why is the type of the main function in
无效的输入流不起作用 - 每次我给出负的月份值时,它都会返回此异常。 代码: import java.util.Scanner; public class Main { public stat
我在 main() 中调用 main(),递归 10 次。现在,在使用 gdb (bt/backtrace) 进行调试时,我没有看到 main() 的多个帧。为什么? #include int mai
我有一个类 - A - 没有方法,只有主要方法。 在其他类(class) - B - 我需要调用那个 main.做什么最好?从使用的资源、时间和功耗以及效率来看? 从类 A 创建一个“a”对象并执行
鉴于 documentation以及对 earlier question 的评论,根据要求,我制作了一个最小的可重现示例,演示了这两个语句之间的区别: my %*SUB-MAIN-OPTS = :na
我有一个在 main 中声明并初始化的数组,名为 Edges。 我还在 main 中声明了一些访问名为 Edges 的数组的函数。 代码编译并运行。 为什么它有效?我认为 main 中声明的变量不是全
如果定义内容主要部分的最具语义性和可访问性的方式是标准,那么使用 ARIA 地标似乎是多余的元素。正在添加 role="main"到元素真的有必要吗? 最佳答案 并非所有现代浏览器都已经映射了 ari
我是 C 语言的新手(6 小时前开始),我知道有大量的在线引用资料,我应该(并且将会)详细查看,但现在,我有紧急情况需要帮助。我有一个包含以下文件的项目文件夹: boundary_val.c boun
我正在审查许多不同的 Java 程序,并试图弄清楚如何只更新一次而不是两次更新对程序名称的引用。有没有办法在单个终端命令中使用变量? :S 我试图改进的命令是这样的形式: javac Main.jav
我已经创建了一个工作线程, Thread thread= new Thread(runnable); thread.start(); 我在工作线程中打印这个; Log.d("SessionTh
import java.awt.*; import java.awt.event.*; import java.io.*; import java.lang.*; public class Main
这是我的 Main.java,它位于服务器套接字“get().logger().tag();”之后的部分我已经在实例中添加了所有这些,我真的不确定它出了什么问题。 public class Main
我在 http://www.hackerearth.com/problem/algorithm/roys-life-cycle/ 上测试了我的程序。但是,我总是收到错误:在类 ActivityTime
我想要一个脚本来运行从模块导出的子例程,导出的子程序在脚本中作为 MAIN 运行。该子例程做了我想做的所有事情,除了它返回结果而不是打印它。 RUN-MAIN 似乎实现了我的大部分目标,但我不确定如何
java中有什么具体原因吗,main方法应该是小写字母 是的“主要”和“主要” 编译完成 public class ManiMethod { public static void main(S
我是一名优秀的程序员,十分优秀!