使用工具类处理调用 直接调用就不会走代理了

@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);

 

相关文章:

  • 2021-08-07
  • 2022-02-07
  • 2022-12-23
  • 2021-07-26
  • 2022-12-23
  • 2022-12-23
  • 2021-10-17
  • 2022-12-23
猜你喜欢
  • 2021-12-19
  • 2021-11-20
  • 2021-07-23
  • 2022-12-23
  • 2023-03-14
  • 2021-10-10
相关资源
相似解决方案