dimanche 1 mars 2015

I am getting an error -- "No qualifying bean of type [hello.MessagePrinter] is defined"

This is the error i get when i execute the sample spring application from spring website. I tried to find a solution but in vain. Kindly help. Its fairly simple i guess but not able to figure out.


Files Application.java



package hello;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
import org.springframework.context.support.AbstractApplicationContext;

@Configuration
@ComponentScan
public class Application {

@Bean
MessageService mockMessageService(){
return new MessageService() {
public String getMessage() {
return "Hello World!";
}
};
}

public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext();
((AbstractApplicationContext) context).refresh();
MessagePrinter printer = context.getBean(MessagePrinter.class);
printer.printMessage();

}

}


MessagePrinter.java



package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MessagePrinter {
final private MessageService service;

@Autowired
public MessagePrinter(MessageService service){
this.service = service;
}

public void printMessage(){
System.out.println(this.service.getMessage());
}

}


MessageService.java



package hello;

public interface MessageService {
String getMessage();

}


pom.xml



<project xmlns="http://ift.tt/IH78KX" xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="http://ift.tt/IH78KX http://ift.tt/VE5zRx">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>
<artifactId>gs-maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>gs-maven</name>
<url>http://ift.tt/19pvvEY;

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>hello.Application</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
<!-- tag::joda[] -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.2</version>
</dependency>
<!-- end::joda[] -->
</dependencies>
</project>


Error:



Mar 2, 2015 10:50:01 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@c1b531: startup date [Mon Mar 02 10:50:01 IST 2015]; root of context hierarchy
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [hello.MessagePrinter] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:371)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:968)
at hello.Application.main(Application.java:23)


Kindly let me know what i am missing


Aucun commentaire:

Enregistrer un commentaire