The problem is that when I try to create a bean by using the @Component annotation I receive the following error: "No bean named 'rockMusic' available". I believe that I set the path correctly, yet it seems that appcontext can not find the RockMusic class and create its bean. I am working on the Eclipse.
Packages:
- src:
- main:
- java:
- spring.study.my_spring:
- the classes from below
- spring.study.my_spring:
- resources:
- appcontext.xml
- java:
- main:
- pom.xml The appcontext
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="spring.study.my_spring" />
</beans>
package spring.webapp.my_spring;
import org.springframework.stereotype.Component;
@Component
public class RockMusic implements Music{
public String getSong() {
return "Rock Song";
}
}
Music Player class
package spring.webapp.my_spring;
import org.springframework.stereotype.Component;
@Component
public class MusicPlayer {
private Music music;
// IoC
public MusicPlayer(Music music) {
this.music = music;
}
public void playMusic() {
System.out.println("Playing: " music.getSong());
}
}
Music Interface
package spring.webapp.my_spring;
public interface Music {
public String getSong();
}
Test class
package spring.webapp.my_spring;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("appcontext.xml");
Music rockMusic = context.getBean("rockMusic", Music.class);
MusicPlayer musicPlayer = new MusicPlayer(rockMusic);
musicPlayer.playMusic();
context.close();
}
}
The Error
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'rockMusic' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:772)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1221)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089)
at spring.webapp.my_spring.Test.main(Test.java:9)
CodePudding user response:
Your default scan is the spring.study.my_spring package, but everything is in the spring.webapp.my_spring package. As the component scanning is looking at a different package, it cannot find your beans.
Further
Music rockMusic = context.getBean("rockMusic", Music.class);
MusicPlayer musicPlayer = new MusicPlayer(rockMusic);
What is it you're trying to test here? Spring will auto-inject rockMusic int the musicPlayer. You can either go to the context and get the musicPlayer bean, or autowire it into your test class, and use it. You don't need to instantiate it directly.