简体   繁体   中英

Failure running spring application : NoSuchBeanDefinitionException

I have some trouble running a java spring application : main component is fr.sgcib.cva.accounting.Computation inside Accounting.jar .

I am trying this command :

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

The applicationContext.xml file is located in spring/ , and looks like this :

<?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>

The main class 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. */

}

The console output I get when running the above command :

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)

Why can't the JVM find that bean ?

Change this line:

 ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml);

to this:

  ("classpath:applicationContext.xml)  // without the star

When using the start you are basically creating an empty application context when the file could not be found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM