[SPRING] JsonSerializer에 @ bean을 @autowire하는 방법?
SPRINGJsonSerializer에 @ bean을 @autowire하는 방법?
내 웹 애플 리케이션에서 최대 절전 모드로 게으른 로딩을 사용하고있다.
서버 응답의 구문 분석 단계에서 데이터베이스의 일부 개체를로드하고 싶습니다.
이는 DesignSerializer가 각 객체에 대해 "new"연산자로 인스턴스화되기 때문에 완전히 이해할 수 있습니다.
내가 만든 시리얼을 시리얼 라이저에 삽입하는 방법이 있다고 확신한다.
너희들이 날 도와 주거나 올바른 방향으로 나를 가르 칠 수 있니?
해결법
-
==============================
1.Spring Framework 2.5+를 사용하고 있다면 해결책은 SpringBeanAutowiringSupport입니다.
Spring Framework 2.5+를 사용하고 있다면 해결책은 SpringBeanAutowiringSupport입니다.
public class DesignSerializer extends JsonSerializer<Design> { @Autowired IDesignService designService; } public DesignSerializer(){ SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); } ... }
나는 너를 돕기를 희망한다.
-
==============================
2.우리는 JsonSerializer와 Spring autowiring에 동일한 문제가있었습니다. 우리에게 도움이 된 솔루션은 두 개의 생성자를 만드는 것이 었습니다. 종속성을 정적 필드로 설정하는 Spring 용과 Jackson 초기화에 사용되는 다른 하나.
우리는 JsonSerializer와 Spring autowiring에 동일한 문제가있었습니다. 우리에게 도움이 된 솔루션은 두 개의 생성자를 만드는 것이 었습니다. 종속성을 정적 필드로 설정하는 Spring 용과 Jackson 초기화에 사용되는 다른 하나.
이것은 Jackson이 serializer를 초기화하기 전에 Spring 종속성 삽입 (autowiring)이 발생하기 때문에 작동합니다.
@Component public class MyCustomSerializer extends JsonSerializer<String> { private static IDesignService designService; // Required by Jackson annotation to instantiate the serializer public MyCustomSerializer() { } @Autowired public MyCustomSerializer(IDesignService designService) { this.designService = designService; } @Override public void serialize(String m, JsonGenerator gen, SerializerProvider s) { gen.writeObject(MyCustomSerializer.designService.method(..)); } }
-
==============================
3.다른 빈에서 정적 필드를 생성 한 다음 @Autowire setter 메서드를 사용하여 문제를 해결했습니다.
다른 빈에서 정적 필드를 생성 한 다음 @Autowire setter 메서드를 사용하여 문제를 해결했습니다.
@Service("ToolBox") @Transactional public class ToolBox { static Logger logger = Logger.getLogger(ToolBox.class); private static IService service; @Autowired public void setService(IService service) { ToolBox.service = service; } public static IService getService() { return ToolBox.service; }}
이 스레드에 표시된 것처럼 : 정적 필드에 @Autowired를 사용할 수 있습니까?
from https://stackoverflow.com/questions/17576098/how-to-autowire-some-bean-into-jsonserializer by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] 봄 - jsp 파일에 이미지 표시 (0) | 2019.01.13 |
---|---|
[SPRING] 왜 Spring의 jdbcTemplate.batchUpdate ()가 느린가? (0) | 2019.01.13 |
[SPRING] 후행 슬래시가있는 URL을 해당 URL없이 해당 URL로 리디렉션하는 방법은 무엇입니까? (0) | 2019.01.13 |
[SPRING] Spring 보안 - 로그인으로 리다이렉션시 URL 매개 변수 유지하기 (0) | 2019.01.13 |
[SPRING] 자식 컨텍스트에서 부모 컨텍스트에 정의 된 Bean 무시 (0) | 2019.01.13 |