java - 使用BeanPostProcessor创建代理,但peoxy类字段为空
问题描述
有服务
@Service
public class TestService {
public void test(){
System.out.println("test");
}
}
这是 BeanPostProcessor
@Component
public class Demo2 implements BeanPostProcessor {
Enhancer enhancer=new Enhancer();
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
Class clazz=bean.getClass();
Annotation annotation=clazz.getAnnotation(HttpAnnotion.class);
if(annotation!=null){
if(clazz.isInterface()){
throw new BeanCreationException("can not use for interface");
}
enhancer.setSuperclass(clazz);
enhancer.setCallback(new MyMethodInterceptor());
Object proxy =enhancer.create();
return proxy;
}
return bean;
}
}
这是方法拦截器
public class MyMethodInterceptor implements MethodInterceptor {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
return proxy.invokeSuper(obj,args);
}
}
这是控制器
@RestController
@RequestMapping("/test")
@Slf4j
@HttpAnnotion
public class HelloController {
@Autowired
private TestService testService;
@PostMapping
public void tran(@RequestBody JSONObject j){
System.out.println("aaa");
}
}
当控制器收到请求时,testService 字段为空
我测试发现,在BeanPostProcessor中,在创建代理之前,bean的testService字段不为空,创建代理后,该字段为空
并且我尝试不是通过 BeanPostProcessor 创建代理,testService 字段不为空,所以 reson 与 BeanPostProcessor 有关。
谁能帮我
解决方案
推荐阅读
- postgresql - 如何使用 PostgreSQL 的 UTC 偏移量获取 ISO 格式的时间?
- c++11 - 在虚拟方法中推断“this”的类型名
- php - 为什么mysql查询返回零值?
- sql - 用于替换特定列中部分字符串的 REPLACE 函数
- r - 将数据框与一列中的列表保存为制表符分隔的单个条目“.txt”文件
- php - 从 localhost 连接到在线数据库时,用户 'totallig_root'@'197.210.64.73' 的访问被拒绝(使用密码:YES)
- r - R 在地图函数中继续 t.test,尽管没有足够的观察值
- c++ - 如何在 NGINX 服务器上运行 C++ CGI 脚本
- java - 为什么我的拦截器的 Spring 处理程序在配置登录身份验证时工作错误?感到不解
- php - 将特色图片批量添加到现有的 Wordpress 帖子中