使用工具类处理调用 直接调用就不会走代理了
@Component public class SpringContextUtil implements ApplicationContextAware { // Spring应用上下文环境 private static ApplicationContext applicationContext; /** * 实现ApplicationContextAware接口的回调方法,设置上下文环境 */ public void setApplicationContext(ApplicationContext applicationContext) { SpringContextUtil.applicationContext = applicationContext; } /** * @return ApplicationContext */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * 获取对象 * @param name * @return Object */ public static Object getBean(String name) throws BeansException { return applicationContext.getBean(name); } /** * 通过类型获取对象 * @param t 对象类型 */ public static <T> T getBean(Class<T> t) throws BeansException { return applicationContext.getBean(t); } }
示例
User user = SpringContextUtil.getBean(UserService.class).getByUid(uid);