JPA @PostPersist如何访问HttpServletRequest?

时间:2019-03-27 07:37:56

标签: spring-boot jpa spring-data-jpa

我必须实现@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之外的类,所以我不能使用自动装配

0 个答案:

没有答案