使用java main方法加载SpringContext,Annatation无法获取bean

时间:2016-05-08 09:43:54

标签: spring

使用Java主方法加载SpringContextAnnatation无法获得该bean。

Java方法代码:

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring-mybatis.xml"});

    context.start();
    UserMapper userMapper = (UserMapper)context.getBean("userMapper");
    System.out.println("userMapper----->:" + userMapper);   //this line can get the bean,and print the bean's Memory Address
    while(true){
        Thread.sleep(1000);
    }

但是当我使用Annatation获取bean时,无法获取它,返回NULL java代码:

@Resource

private UserMapper userMapper;

0 个答案:

没有答案