使用AnnotationConfigApplicationContext的NoSuchBeanDefinitionException异常

时间:2017-02-09 05:06:14

标签: java spring

我得到" org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为'员工'已定义"错误,我不知道我做错了什么。请在下面找到代码。

  1. )AppConfig类:

    @ComponentScan("com.spring.annotation.propertyconfigurer")
    @Configuration
    @PropertySource("classpath:employee.properties")
    public class AppConfig {
    
    @Bean
    public static PropertySourcesPlaceholderConfigurer    propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
     }    
    } 
    
  2. 2)员工类

    @Component("employee")
    public class Employee {
    
        @Value("${emp.empId}")
        private int empId;
        @Value("${emp.name}")
        private String name;
        @Autowired
        private Address address;
    
        public int getEmpId() {
            return empId;
        }
    
        public void setEmpId(int empId) {
            this.empId = empId;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Address getAddress() {
            return address;
        }
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
        public void print() {
            System.out.println("Employee Id -> " + empId);
            System.out.println("Employee Name -> " + name);
            System.out.println("Employee Address -> [ " + address.getCity() + "," + getAddress().getCountry() + "]");
        }
    }
    

    3)地址类:

    @Component("address")
    public class Address {
    
        private String city;
        private String country;
    
        public String getCity() {
            return city;
        }
    
        public void setCity(String city) {
            this.city = city;
        }
    
        public String getCountry() {
            return country;
        }
    
        public void setCountry(String country) {
            this.country = country;
        }
    }
    

    4)主类

    public class PropertyConfigurerApp {
    
        public static void main(String[] args) {
            try (AbstractApplicationContext context = new AnnotationConfigApplicationContext("AppConfig.class")) {
                Employee employee = (Employee) context.getBean("employee");
                employee.print();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
    }
    

    请帮我解决此问题。

    谢谢, 哈拉

1 个答案:

答案 0 :(得分:0)

您可以将bean注册到配置中,例如在组件包上使用组件扫描。

有几种方法可以做到这一点

  • via xml config <context:component-scan base-package="org.example"/>
  • @ComponentScan({package1, .., package n})
  • 中使用@Configuration

您还可以在@Bean

中为@Components添加@Configuration

参见spring文档

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-annotation-config

如何更改您的代码

1-给你的课程一个包(如果你还没有)。我使用了test.component作为员工和地址

2-在AppConfig中更改

     @ComponentScan("com.spring.annotation.propertyconfigurer")

    @ComponentScan("test.component") 

    @ComponentScan(basePackages={"test.component"})

3-在propertyConfigurerApp中更改

            AbstractApplicationContext context = new AnnotationConfigApplicationContext("AppConfig.class")

  AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class)

它应该工作(将你的employee.properties放在classpath的根目录上)

但是,您并不需要制作所有这些组件扫描内容,因为您不会在示例中使用自动装配。