1
저지 (jersey) 인터셉터는 응용 프로그램 시작시 작성됩니다. 따라서 종속성 (이 경우 Cipher)이 요청 범위 밖으로 주입됩니다.싱글 톤 저지 인터셉터 내에서 RequestScoped 객체를 사용하는 방법은 무엇입니까?
암호는 stateful이므로 요청 범위에 삽입해야합니다. 그렇게하는 방법 ? 각각의 새로운 요청에 대한 새 암호를 기대
는@Provider
@Priority(Priorities.ENTITY_CODER + 1)
public class CryptInterceptor implements ReaderInterceptor, WriterInterceptor {
@Inject @Named("ENC_CIPHER")
private Cipher encryptionCipher;
@Inject @Named("DEC_CIPHER")
private Cipher decryptionCipher;
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
InputStream inputStream = context.getInputStream();
CipherInputStream cipherInputStream = new CipherInputStream(inputStream, decryptionCipher);
context.setInputStream(cipherInputStream);
return context.proceed();
}
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
OutputStream outputStream = context.getOutputStream();
CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, encryptionCipher);
context.setOutputStream(cipherOutputStream);
context.proceed();
}
}
RequestScope에서 그들을 주입 같다 -
은public class BootstrapBinder extends AbstractBinder {
@Override
protected void configure() {
bindFactory(EncCipherFactory.class).to(Cipher.class).named("ENC_CIPHER").in(RequestScoped.class);
bindFactory(DecCipherFactory.class).to(Cipher.class).named("DEC_CIPHER").in(RequestScoped.class);
}
}
이제 분명히 hk2은 (저지의 DI)는 싱글 요격 내부 RequestScoped 객체를 주입 캔트. 그 원인은 다음과 같습니다.
java.lang.IllegalStateException: Not inside a request scope.
내가 저지 문서에 프록시를 공부 어려움이 있었다. 어떤 힌트,이 코드가 장면 뒤에서 무엇을하고 있습니까? – Nilesh
'proxy'는 동적 프록시를 생성합니다. 따라서 서비스에 액세스 할 때마다 프록시가됩니다. proxyForSameScope은 부모가 동일한 범위, 즉 요청 범위에 있으면 프록시로 만들지 않고 실제 객체 만 사용한다고 말합니다. 예를 들어 이제 프록시 객체가되지만 리소스 클래스 (기본적으로 요청 범위)에 삽입하려고하면 실제 인스턴스가됩니다. –
내가 링크 된 기사에서 더 자세히 설명합니다. 또한 동적 프록시에 관한 기사에 링크 된 기사가 있습니다. 프록시가 장면 뒤에서 어떻게 작동하는지 배우려면 신경 써야합니다. –