普通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"); } }