Why is my @Autowired object null?

后端 未结 1 955
旧时难觅i
旧时难觅i 2021-01-23 10:53

I\'ve the below JSF managed bean:

package com.example;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import org.springframework.beans.factor         


        
1条回答
  •  一整个雨季
    2021-01-23 11:48

    You cannot mix annotations and contexts like you're attempting here.

    First, to inject a Spring bean (or really, any bean) into a JSF managed bean, use the @ManagedProperty annotation. So, if everything else is setup properly (Spring-wise, including the Spring Faces EL resolver), you should have:

    @ManagedProperty("#{someService}")
    private SomeService someService;
    

    When you declare a class as a @ManagedBean, it's outside the context of Spring. As a result, @Autowired has no power here; spring cannot inject anything into anything that's outside of its context.

    If you want to use the pair of @Component and @Autowired, you need to remove @ManagedBean so that it becomes a Spring managed bean instead of a JSF managed bean.

    0 讨论(0)
提交回复
热议问题