我必须实现@EntityListener
@Entity
@Table(name = "profile")
@Data
@EntityListeners(ProfileListener.class)
public class Profile implements Serializable {
...
}
和ProfileListener.class我想访问HttpServletRequest以获得一些要在@PostPersist内部使用的数据
@PostPersist
public void PostPersist(Object ob) throws IOException {
}
修改 我尝试使用
1。)RequestContextHolder
@PostPersist
public void PostPersist(Object ob) throws IOException {
HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
}
并且出现错误 java.lang.NullPointerException
2。)ApplicationContext
@PostPersist
public void PostPersist(Object ob) throws IOException {
HttpServletRequest httpServletRequest = ApplicationContextProvider.getApplicationContext().getBean(HttpServletRequest .class);
}
并且出现错误 java.lang.NullPointerException
3。)@Autowired private HttpServletRequest request;
AFAIK ProfileListener.class是Spring Application Context之外的类,所以我不能使用自动装配