2017-02-09 53 views
0

我得到「org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有名爲'employee'的bean被定義」錯誤,我不確定我做錯了什麼。請在代碼下方找到。使用AnnotationConfigApplicationContext的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)Employee類

@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)麥類n級

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(); 
     } 

    } 

} 

請幫我解決這個問題。

感謝, 卡邁勒

回答

0

您對豆類註冊的配置,例如使用您的組件封裝組件掃描。

有幾種方法你@Configuration

你也可以在你的@Configuration

添加 @Bean@Components做到這一點

    通過xml配置使用 @ComponentScan({package1, .., package n})
  • <context:component-scan base-package="org.example"/>
  • 查看春季文檔

    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給你的類的包中(如果你沒有它的話)。我用過測試。組件,用於在變化的AppConfig

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

    @ComponentScan("test.component") 
    

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

    3-在propertyConfigurerApp變化的僱員和地址

    2-

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

    AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class) 
    

    它應該工作(把你的employee.properties類路徑的根目錄)

    但是你不需要做這一切componentscanning東西,你不使用自動裝配你的榜樣。

+0

@Massino我的AppCongif類已經有了ComponentScan。所以它應該工作。 –

+0

另外,我使用@Component註冊了Employee和Address類的spring bean。但是它仍然不起作用 –

+0

你沒有設置@ComponentScan({package-where-your-components-are})。你也不需要@ComponentScan(「com.spring.annotation.propertyconfigurer)豆 – Massimo

相關問題