普通java类调用spring中的bean

使用ssm项目,很多service,dao等都使用了注解方式定义,普通java类要获取到这些bean,可这么写:

public class Common  {
    private UserService userService;   
    public Common() {
        @SuppressWarnings("resource")
        AbstractApplicationContext ctx 
            = new ClassPathXmlApplicationContext(new String []{"classpath:spring-mvc.xml","classpath:spring-mybatis.xml"});
        userService = (UserService) ctx.getBean("userService");
    } 
}

 

posted on 2017-06-30 12:01  耐旺旺  阅读(405)  评论(0编辑  收藏  举报

导航