Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

java - How to @autowire some bean into JsonSerializer?

I am using lazy loading with hibernate in my web app.

I would like to load some objects from the database at the parsing stage of the server response

@Component
public class DesignSerializer extends JsonSerializer<Design> {
@Autowired
IDesignService designService; <-- is null

}

Which is totally understandable because DesignSerializer is being instantiated with the "new" operator for each object.

I am sure there is a way to inject my bean into that serializer when ever it is created, I just don't know how.

Can you guys help me or point me in the right direction.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Solution is SpringBeanAutowiringSupport if you are using Spring Framework 2.5+.

public class DesignSerializer extends JsonSerializer<Design> {

    @Autowired
        IDesignService designService;
    }

    public DesignSerializer(){
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);    
    }

...

}

I Hope that help you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...