2

package com.mkyong.output; IOutputGenerator.java

public interface IOutputGenerator
{
    public void generateOutput();
}

package com.mkyong.output; OutputHelper.java

@Component
public class OutputHelper {

    @Autowired
    IOutputGenerator outputGenerator;

    public void generateOutput() {
        outputGenerator.generateOutput();
    }

    /*//DI via setter method
    public void setOutputGenerator(IOutputGenerator outputGenerator) {
        this.outputGenerator = outputGenerator;
    }*/
}

package com.mkyong.output.impl;

CsvOutputGenerator.java

@Component
public class CsvOutputGenerator implements IOutputGenerator {
    public void generateOutput() {
        System.out.println("This is Csv Output Generator");
    }
}

SpringBeans.xml

<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:component-scan base-package="com.mkyong" />

</beans>

i am getting this exception Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'OutputHelper' is defined

even though i have marked OutputHelper as component.

saurabh
  • 237
  • 6
  • 20

4 Answers4

3

I have changed

OutputHelper output = (OutputHelper) context.getBean("OutputHelper");

to

OutputHelper output = (OutputHelper) context.getBean("outputHelper");

and it worked.

Rufi
  • 2,529
  • 1
  • 20
  • 41
saurabh
  • 237
  • 6
  • 20
  • thats because by default, Spring will lower case the first character of components, so Outputhelper becomes outputHelper – Tobika Dec 28 '15 at 13:04
0

Hi i think you haven't added following in your Spring XML configuration

 xmlns:mvc="http://www.springframework.org/schema/mvc"

http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd

<mvc:annotation-driven/>
0

you need to see top exception and read the whole line. i guess there have a exception is nested exception just like @Autowired xxxxxx,meas autowired fail. i have notice this:

@Autowired
IOutputGenerator outputGenerator;

and

@Component
public class CsvOutputGenerator implements IOutputGenerator

so, in the default, class name is used to @Autowired,you can rewrite to

@Autowired
IOutputGenerator csvOutputGenerator;

notice: "csvOutputGenerator" first letter is lowercase

VanXD
  • 61
  • 2
  • Did you try this change? @Autowired IOutputGenerator csvOutputGenerator; – VanXD Dec 28 '15 at 09:41
  • i changed OutputHelper output = (OutputHelper)context.getBean("OutputHelper"); to OutputHelper output = (OutputHelper)context.getBean("outputHelper"); and it worked – saurabh Dec 28 '15 at 09:52
0

the easier option would be to enable annotations in beans already registered in the application context, means that you can just use @Autowired instead of getting manually all beans with context.getBean()

just add this line to your SpringBeans.xml
<context:annotation-config>

if you really want to understand what you are doing reading this could help.

Community
  • 1
  • 1
Tobika
  • 1,037
  • 1
  • 9
  • 18