I'm relative new to Java and also to Spring Framework. So this is my first Spring App I'm building and I'm experiencing problems with Autowiring my Beans.
My Application.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);
}
}
My config.xml in ressource folder:
<?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>
My 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>
The Error Message I get on Startup:
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)
So I don't understand why there is no qualified Bean cause I registered the bean in tray.xml. The CustomTray class is derived from TrayIcon class and creates a TrayIcon nothing special but for completion, my CustomTray:
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();
}
}
}
I got the App running before, without using Autowiring and Beans but now I want to refactor and setup a clean Spring App, I hope heres somebody who knows what I'm doing wrong.
edit: Ok so now if I'm Importing the xml-Files with the annotation mentioned by UUID I get another Error Message. The bean customTray got two properties, the SchedulerThread property shall be a Singleton Bean, too. It's declared in beans/scheduler.xml which itself also got a property which shall be a bean. like that:
scheduler.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>
Error:
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
What is the best way to wire these classes and create the Beans? As I sayed I'm new to Spring and I think I didn't understood the way it's working with beans and autowiring.
Aucun commentaire:
Enregistrer un commentaire