madcolonel10
madcolonel10

Reputation: 743

How to inject dependencies in spring for objects created dynamically at run time?

public class PlatformEventFactory {

    public PlatformEvent createEvent(String eventType) {
        if (eventType.equals("deployment_activity")) {
            return new UdeployEvent();
        }


        return null;
    }
}

I have a factory class which creates PlatformEvent type objects based on the eventType.

UdeployEvent class has dependency on private RedisTemplate<String, Object> template on which I want to inject after the UdeployEvent object has been created.

@Component
public class UdeployEvent implements PlatformEvent {

    private RedisTemplate<String, Object> template;
    private UDeployMessage uDeployMessage;

    private static final Logger logger = LoggerFactory.getLogger(UdeployEvent.class);

    public UdeployEvent() {
        uDeployMessage = new UDeployMessage();
    }


    /*public void sendNotification() {

    }*/

    public RedisTemplate<String, Object> getTemplate() {
        return template;
    }

    @Autowired
    public void setTemplate(RedisTemplate<String, Object> template) {
        this.template = template;
        System.out.println("Injection done");
    }
}

When the new object is returned for UdeployEvent I get null pointer exception for template. I believe the reason for that is because it is not referring to the same bean which is created when spring boots up. How can I inject dependencides for newly created objects at run time.

Upvotes: 4

Views: 8634

Answers (3)

Oleg_Andreych
Oleg_Andreych

Reputation: 73

You should annotate your bean as SCOPE_PROTOTYPE, as @ken-bekov pointed out.

To obtain an instance of prototype bean you can use injected (autowired) org.springframework.beans.factory.ObjectProvider<T>, rather than ApplicationContext. Type parameter of this provider should be UdeployEvent.
Additionally you can provide arguments for class constructor or factory method in get method. One downside of this approach is that call to get won't be statically checked.

One more way to solve your issue is trying to use Google's AutoFactory.

Upvotes: 0

Ken Bekov
Ken Bekov

Reputation: 14015

You should not create components manually. Let Spring to do this. Use ApplicationContext to get instance of component. All fields will be automatically injected:

@Component
public class PlatformEventFactory {

    @Autowired
    private ApplicationContext context;

    public PlatformEvent createEvent(String eventType) {
        if (eventType.equals("deployment_activity")) {                
            return context.getBean(UdeployEvent.class);
        }

        return null;
    }
}

To make Spring create new instance of UdeployEvent component every time you request it, specify scope of component as SCOPE_PROTOTYPE:

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class UdeployEvent implements PlatformEvent {

    private RedisTemplate<String, Object> template;

    public RedisTemplate<String, Object> getTemplate() {
        return template;
    }

    @Autowired
    public void setTemplate(RedisTemplate<String, Object> template) {
        this.template = template;
        System.out.println("Injection done");
    }

    ...
}

Now every time you call context.getBean(UdeployEvent.class) Spring will create new instance of component with fully initialized dependencies.

Upvotes: 6

Rychu
Rychu

Reputation: 86

When you're creating objects by hand, dependency injection is not performed in created object, and the field is null.

The easy way would be to use AutowireCapableBeanFactory autowireBean() Example:

@Component
public class PlatformEventFactory {

    @Autowired  
    private AutowireCapableBeanFactory beanFactory;

    public PlatformEvent createEvent(String eventType) {
        if (eventType.equals("deployment_activity")) {
            PlatformEvent platformEvent = new UdeployEvent();
            beanFactory.autowireBean(platformEvent);
            return platformEvent;
        }

        return null;
    }
}

beanFactory.autowireBean(platformEvent) should inject your fields and it should work fine.

There are more extended solutions with @Configuration, but they produce a lot of boilerplate code and doesn't give to much in return.

Haven't seen cleaner solution in Spring (like @AssistedInject in Guice).

Source: http://www.kubrynski.com/2013/09/injecting-spring-dependencies-into-non.html

Upvotes: 1

Related Questions