0

i wrote this app with eclipse and it works. But when i deploy it as standalone/console application, it cannot find StartApp bean i've injected. here's the codes:

main app:

@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:

@Service
public class Processor {
    public Processor() {
        System.out.println("Processor initialized!");
    }
    public void say() {
        System.out.println("hello!");
    }
}

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

i did put all spring libraries and logger including slf4j libraries. and for console command i put these:

java -cp lib/*:lib/spring-3.1/*:test-spring-desktop.jar test.spring.desktop.StartApp

then i got these error message:

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)
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
  • Where and how is your `StartApp` bean defined? – bvulaj Apr 05 '12 at 14:48
  • its on the same folder. here's the structure: /lib/spring-3.1/{all-spring3.1-lib} /lib/{slf4j-libs} /test-spring-desktop.jar /start-test-spring-desktop.sh – nova saputra Apr 09 '12 at 03:04
  • sorry for the mess on my comment, im still trying to get the right formatting in comments. – nova saputra Apr 09 '12 at 03:07
  • as for StartApp is in test-spring-desktop.jar. for full class name is: test.spring.desktop.StartApp – nova saputra Apr 09 '12 at 03:10
  • hi @BrandonV, could you help me please? – nova saputra Apr 12 '12 at 07:54
  • You don't define a StartApp bean inside of your applicationContext.xml. – bvulaj Apr 12 '12 at 13:14
  • indeed, i didnt define it in my applicationContext because i put context:component-scan tag. so even i want to use annotation based autowiring, do i still have to define all the beans i use? – nova saputra Apr 13 '12 at 02:26
  • I also just noticed, you are trying to use @Autowired without `` – bvulaj Apr 13 '12 at 13:43
  • This question may also help you - http://stackoverflow.com/questions/3659720/spring-3-autowire-in-standalone-application – bvulaj Apr 13 '12 at 13:44
  • yes i didnt put '' because i've already put '' which do the same thing. here's the info i got from other page (http://stackoverflow.com/questions/7414794/difference-between-contextannotation-config-vs-contextcomponent-scan) – nova saputra Apr 15 '12 at 17:51
  • i had read the page you given and i've followed the sample. but still.. it can work in eclipse but it cannot work once i deploy it on console. – nova saputra Apr 15 '12 at 17:54
  • ok, its getting wierd. i've built, packaged and deployed it using ant, and it works. so, i suspect the problem caused by eclipse built-in packager. – nova saputra May 01 '12 at 07:09

1 Answers1

0

Try to put in applicationCntext.xml this configuration:

<context:annotation-config />