我在运行java spring应用程序时遇到一些问题:fr.sgcib.cva.accounting.Computation
内的主要组件是Accounting.jar
。
我正在尝试这个命令:
java -cp spring/*:./*:spring-core-3.1.1.RELEASE.jar:spring-context-3.1.1.RELEASE.jar:spring-beans-3.1.1.RELEASE.jar:log4j-1.2.16.jar:commons-lang3-3.1.jar:commons-logging-1.1.1.jar:spring-asm-3.1.1.RELEASE.jar:spring-expression-3.1.1.RELEASE.jar:.:Accounting.jar fr.sgcib.cva.accounting.Computation 3
applicationContext.xml文件位于spring/
,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:security="http://www.springframework.org/schema/security" xmlns:context="http://www.springframework.org/schema/context"
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-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">
<import resource="classpath:spring/persistence.xml"/>
<context:annotation-config />
<context:component-scan base-package="fr.aaa.accounting, com.bbb.access"/>
<util:properties id="jdbcProps" location="jdbc.properties" />
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:configuration.properties</value>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
主要班级Computation.java
:
package fr.aaa.accounting;
@Component
public class Computation {
static Logger logger = Logger.getLogger(Computation.class);
@Autowired
Runs runs;
public Computation() {
}
public Run initialize(Long runId) {
Run run = runs.getRun(runId);
return run;
}
public Runs getRuns() {
return runs;
}
public void setRuns(Runs runs) {
this.runs = runs;
}
/**
* @param args
*/
public static void main(String[] args) {
Long runId = Long.parseLong(args[0]);
logger.info("RUNNING RUN_ID " + runId);
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");
Computation computer = context.getBean(Computation.class);
/* etc. */
}
运行上述命令时得到的控制台输出:
10:44:41,745 INFO main fr.aaa.accounting.Computation:299 - RUNNING RUN_ID 3
10:44:41,872 INFO main org.springframework.context.support.ClassPathXmlApplicationContext:495 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@e0b6f5: startup date [Mon Mar 10 10:44:41 CET 2014]; root of context hierarchy
10:44:41,979 INFO main org.springframework.beans.factory.support.DefaultListableBeanFactory:557 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@ee22f7: defining beans []; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [fr.aaa.accounting.Computation] 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 fr.aaa.accounting.Computation.main(Computation.java:302)
为什么JVM找不到那个bean?
答案 0 :(得分:0)
更改此行:
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml);
到此:
("classpath:applicationContext.xml) // without the star
使用start时,基本上无法找到文件时创建一个空的应用程序上下文。