2

I can't resolve my problem for getting null with my @Autowired service. Here's my code.

My configuration file

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <tx:annotation-driven transaction-manager="transactionManager"/>
    <context:annotation-config/>
    <context:component-scan base-package="com.vulcan.controller.login" />
    <context:component-scan base-package="com.vulcan.service.login" />
    <context:component-scan base-package="com.vulcan.dao.tenant" /> ........

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/applicationContext.xml</param-value>
     </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>....

faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    <application>
        <variable-resolver>
            org.springframework.web.jsf.DelegatingVariableResolver
        </variable-resolver>
    </application>
    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>........

My Code

LoginServiceImpl.java

@Service
public class LoginServiceImpl implements LoginService{

    @Autowired
    TenantDao tenantDao;

    @Transactional
    @Override
    public String getTenantIdentifier(String email, String password) { ....

LoginController.java

@Component
@Scope("session")
@Qualifier("login")
public class LoginController  {

    private String email;
    private String password;
    private String tenantId;
    
    @Autowired 
    LoginService loginService;

    public void doLogin() {
        this.tenantId = loginService.getTenantIdentifier(email, password); ..

login.xhtml

<ui:define name="content">
    <h:form id="form">   
        <p:growl id="growl" showDetail="true" life="3000" />  
        <h:panelGrid columns="2" cellpadding="5">  
            <h:outputLabel for="username" value="Username:" />  
            <p:inputText value="#{login.email}" id="username" required="true" label="username"/>
            <h:outputLabel for="password" value="Password:" />  
            <p:inputText value="#{login.password}" id="password" required="true" label="password" type="password" />  
            <f:facet name="footer">  
                <p:commandButton id="loginButton" value="Login" update="growl" action="#{login.doLogin()}"/>  
            </f:facet>  
        </h:panelGrid>  
    </h:form>           
</ui:define>

So I choose to use Spring to manage all of my beans. Here's what I've done to resolve my problem (but still no result)

  1. I tried to use inject JSF @ManagedBean to LoginController, and @ManagedProperty for LoginService. I got null pointer after invoke doLogin(). It said LoginService has null pointer.

  2. After that I tried to use Spring injection to LoginController as you can see above, than I got this error.

    /login.xhtml @21,109 value="#{login.email}": Target Unreachable, identifier 'login' resolved to null

Anyone can help me ? I already follow and try many suggestion in this forum, but nothing's work.

Community
  • 1
  • 1
Aldo Suwandi
  • 352
  • 1
  • 6
  • 20

1 Answers1

1

Finally i figured it out, i just need to remove @Qualifier annotation and edit @Component annotation to @Component("login").

What a silly mistakes...

Aldo Suwandi
  • 352
  • 1
  • 6
  • 20