Reputation: 1475
I am new to the annotation-flavoured Spring Framework and have a simple bean configured and cannot get the @ComponentScan to work. Both bean and application are in the same package.
package springdemo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Pet {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Bean
public Pet fido() {
Pet p = new Pet();
p.setName("fido");
return p;
}
}
And the Application:
package springdemo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class BeanApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.refresh();
ctx.start();
Pet dog = (Pet) ctx.getBean("fido");
System.out.println(dog.getName());
ctx.stop();
}
}
When I run this the application cannot find the bean (I assume that component scan also picks up beans as well...):
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'fido' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:772)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1212)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
at springmvc.bean.BeanApplication.main(BeanApplication.java:15)
I can get the component scan to work in code via the scan() method, but not via the annotation.
Upvotes: 0
Views: 144
Reputation: 238
You need pass a configuration class where you provide the beans that will be managed by the Spring container.
Classic Spring application example without spring boot
public class SpringApplication {
public static void main(String[] args){
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
Pet dog = (Pet)ctx.getBean("fido");
System.out.println(dog.getName());
}
}
@Configuration
class AppConfig {
@Bean
public Pet fido() {
Pet p = new Pet();
p.setName("fido");
return p;
}
}
class Pet {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Spring boot example:
@SpringBootApplication
public class SpringApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(SpringApplication.class, args);
Pet dog = (Pet) ctx.getBean("fido");
System.out.println(dog.getName());
}
}
@Configuration
class AppConfig {
@Bean
public Pet fido() {
Pet p = new Pet();
p.setName("fido");
return p;
}
}
class Pet {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Upvotes: 3