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.
In Spring Boot, we can use Spring Framework to define our beans and their dependency injection. The @ComponentScan annotation is used to find beans and the corresponding injected with @Autowired annotation. If you followed the Spring Boot typical layout, no need to specify any arguments for @ComponentScan annotation.
In Spring, you can use @Autowired annotation to auto-wire bean on the setter method, constructor , or a field . Moreover, it can autowire the property in a particular bean. We must first enable the annotation using below configuration in the configuration file.
Enabling @Autowired annotationBy declaring beans, you provide metadata to the Spring Container to return the required dependency object at runtime. This is called Spring Bean Autowiring. In java based configuration, all the bean methods are defined in the class with @configuration annotation.
The @Autowired annotation provides more fine-grained control over where and how autowiring should be accomplished. The @Autowired annotation can be used to autowire bean on the setter method just like @Required annotation, constructor, a property or methods with arbitrary names and/or multiple arguments.
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
We had the same problem with JsonSerializer and Spring autowiring. The solution that worked for us was to make two constructors. One for Spring which sets the dependency as a static field, and another one that is used by the Jackson initialisation.
This works because the Spring dependency injection (autowiring) happens before Jackson initialises the serializer.
@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(..)); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With