使用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"); } }
时间: 2024-10-09 10:27:41