0
我用eclipse編寫了這個應用程序,它的工作原理。但是當我將它作爲獨立/控制檯應用程序部署時,它找不到我注入的StartApp bean。 這裏的代碼:spring 3.1註釋了獨立應用程序的自動裝配
主要應用:
@Component
public class StartApp {
@Autowired
private Processor proc;
public StartApp() {
System.out.println("Starting App!");
}
private void say() {
proc.say();
}
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
StartApp app = ctx.getBean(StartApp.class);
app.say();
}
}
服務:
@Service
public class Processor {
public Processor() {
System.out.println("Processor initialized!");
}
public void say() {
System.out.println("hello!");
}
}
和applicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="test.spring.desktop"/>
</beans>
我沒有把所有的彈簧庫和記錄,包括SLF4J庫。 和控制檯命令我把這些:
java -cp lib/*:lib/spring-3.1/*:test-spring-desktop.jar test.spring.desktop.StartApp
然後我得到了這些錯誤消息:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [test.spring.desktop.StartApp] is defined: expected single bean but found 0:
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:271)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1101)
at test.spring.desktop.StartApp.main(StartApp.java:24)
哪裏又是怎樣的你的'StartApp' bean已定義? – bvulaj 2012-04-05 14:48:35
它在同一個文件夾上。 這裏的結構: /lib/spring-3.1/{all-spring3.1-lib} /lib目錄/ {SLF4J-庫} /test-spring-desktop.jar /啓動測試彈簧桌面.sh – 2012-04-09 03:04:30
抱歉我的評論亂七八糟,我仍然試圖在評論中獲得正確的格式。至於StartApp的 – 2012-04-09 03:07:24