当普通JAVA类或者JSP需要使用service中的方法或者执行sql时使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class DBAccess extends SqlSessionDaoSupport implements ApplicationContextAware { private static ApplicationContext applicationContext; private static SqlSession sqlSession; @Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException { this .applicationContext = applicationContext; } @Override @Autowired public void setSqlSessionTemplate (SqlSessionTemplate sqlSessionTemplate) { super .setSqlSessionTemplate(sqlSessionTemplate); this .sqlSession = sqlSessionTemplate; } public static ApplicationContext getContext () { return applicationContext; } public static SqlSession getSqlSessions () { return sqlSession; } }
applicationContext.xml中配置此bean,以便让Spring启动时自动为我们注入ApplicationContext对象
1 <bean class ="com.xizirumeng.util.DBAccess" > </bean >
使用:
1 2 DBAccess.getContext().getBean("WxUserServiceImpl"); DBAccess.getSqlSessions();