Failure in dependency injection

Asked

Viewed 162 times

0

I’m having a problem in a JSF test, basically I have the entity, the service and the view, I couldn’t find anything that would help me solve.

In view, I want to call my service by Managedproperty so:

@ManagedBean
@ViewScoped
@Getter
@Setter
public class EpiView implements Serializable {

    @ManagedProperty(value = "#{epiService}")
    private EpiService epiService;

    private Epi epi;

    @PostConstruct
    public void inicia() {
        epi = epiService.epiById();
    }

}

and my service:

@Service
public class EpiService {

    public Epi epiById() {
        Epi epi = new Epi();
        epi.setId(new EpiPK());
        epi.getId().setCenCust("13.02");
        epi.getId().setProduto("Bomba");
        epi.setQuantidadeDias(15);
        epi.setQuantidadeRetirada(1000);
        epi.setCategoria("Veneno");
        epi.setQntTroca(200);

        return epi;
    }

}

I’m not using bank is just a test, but when the page goes up I have one NullPointerException, the service is not being instantiated, if I create the instance in hand epiService = new EpiService(); it works and is displayed on the page normally, I believe it is a problem in dependency injection, I tried also to use the @Autowired, but the service comes null likewise.

The project is in Eclipse Version: 2019-03 (4.11.0) I am using Maven with the following dependencies.

jsf-api-2.2.12.jar jsf-impl-2.2.12.jar javax.Servlet-api-3. 0.1.jar spring-web-4.3.9.RELEASE. jar spring-aop-4.3.9.RELEASE. jar spring-Beans-4.3.9.RELEASE. jar spring-context-4.3.9.RELEASE. jar spring-Expression-4.3.9.RELEASE. jar spring-core-4.3.9.RELEASE. jar Commons-logging-1.2. jar Lombok-1.18.8.jar cdi-api-1.2. jar javax.el-api-3. 0.0.jar javax.Interceptor-api-1. 2.jar

  • What is your configuration of el-resolver?

  • What if you write down the Episervice service with: "@Managedbean(name="epiService")?. To use managedProperty I believe you have to write it down anyway.

  • Anything, take a look at this guide to see its dependencies as well. https://www.baeldung.com/spring-jsf

2 answers

0


Due to integration with Spring, you need to make some changes to your code:

  • Utilise @Component instead of @ManagedBean
  • Utilise @Scope("prototype") instead of @ViewScoped
  • You can use @Autowired instead of @ManagedProperty

Since JSF annotations are not interpreted by Spring, their properties will not be injected (since they have never been recognized by Spring)

Or (more simply):

  • Leave their @ManagedBean as it is today, but carry out the following change:
    @ManagedBean
    @ViewScoped
    public class EpiView extends SpringBeanAutowiringSupport {

        @Autowired
        private EpiService epiService;

        ...
    }

The class Springbeanautowiringsupport will cause your bean to be recognized in the context of Spring, performing the injections properly.


As a complement I will leave some links to this thread Stackoverflow in English where there is much more detail than my answer.

0

I managed to solve the problem including 2 class in my project.

@Configuration
@ComponentScan
public class Config {

}

and

public class Initializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext sc) throws ServletException {
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(Config.class);
        sc.addListener(new ContextLoaderListener(rootContext));

    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.