saurabh
saurabh

Reputation: 247

No bean named is defined Exception

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.

Upvotes: 2

Views: 3958

Answers (4)

Tobika
Tobika

Reputation: 1062

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.

Upvotes: 0

saurabh
saurabh

Reputation: 247

I have changed

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

to

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

and it worked.

Upvotes: 3

VanXD
VanXD

Reputation: 61

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

Upvotes: 0

user2026076
user2026076

Reputation:

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

Upvotes: 0

Related Questions