Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 744
Gleeb Avatar asked Jul 10 '13 16:07

Gleeb


People also ask

How do you inject specific beans in a Spring boot?

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.

Can beans be Autowired?

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.

How do you Autowire a bean?

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.

What @autowired annotation uses for bean injection?

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.


2 Answers

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

like image 146
Peter Jurkovic Avatar answered Sep 28 '22 02:09

Peter Jurkovic


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(..));     } } 
like image 25
veiset Avatar answered Sep 28 '22 03:09

veiset