2015-05-09 191 views
-3

以下是我的代碼:的Spring Java配置

@EnableAutoConfiguration 
@ComponentScan(basePackages = "com.datastore") 
public class ABC { 

    @Autowired 
    B b; 

    public static void main(String[] args) { 
     ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class); 
     C c= ctx.getBean(C.class); 
     String s = b.getObject(); 
    } 
} 

上面代碼的問題是,我@Autowired B對象即將
空。但是c不是null。 任何人都可以告訴我如何獲得B b對象。因此,我可以擁有B對象。此外,我的 應用程序中有一個Config.java文件,該文件使用@Bean註釋進行配置。 請幫我一樣。

+1

你在哪裏爲'class B'定義了bean? applicationContext.xml在哪裏? – ersnh

回答

0

ABC也需要@Component註釋。