自动装配外部bean不起作用

时间:2013-07-16 05:48:12

标签: spring jsf autowired

我试着用注释将名为“userVO”的bean映射到安全控制器,当我通过xml配置映射它时它可以工作,但是当我使用注释时我不断收到以下错误

警告:/login.xhtml @ 22,37 value =“#{securityController.userVO.vc_name}”:目标无法缓存,'userVO'返回null

我必须说UserVO在我也拥有的不同项目中,并且它的依赖关系是用maven处理的

以下是我的spring-beans配置文件

 <?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"
     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">

<import resource="mapper-beans.xml" />


<bean name="userVO" id="userVO" class="rst.core.security.UserVO"
    scope="session" />

    <context:annotation-config />
    <context:component-scan base-package="rst.core.security" />
        <context:component-scan base-package="rst.controller" />

</beans>

以下是映射到userVO的控制器无法正常工作:

@ManagedBean 
@RequestScoped
@Controller
public class SecurityController {

 public static Logger log;

 private WebFacade webFacade;

@Autowired
private UserVO userVO;

public SecurityController() {
      log = LoggerFactory.getLogger(this.getClass());
      log.debug("Creating SecurityController.");
 }

 public String login() {

    UserVO user= webFacade.validateUser(getUserVO()); // method to search the db
    if (user!= null) {
        return "accessGranted";
    } else {
         return "accessDenied";
    }
}

 //getters and setters....

}

这也是触发错误的登录屏幕:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1      /DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

  <h:head>

  </h:head>

  <h:body>
     <h:form id="form">

    <p:panel id="panel" header="New Person">

        <h:panelGrid columns="3">
            <h:outputLabel for="name" value="Name: *" />
            <p:inputText id="name" value="#{securityController.userVO.vc_name}"
                label="name" required="true"/>

            <h:outputLabel for="password" value="Password: *" />
            <p:inputText id="password" value="#{securityController.userVO.vc_password}" 
               required="true" label="Password" type="password"/>

           </h:panelGrid>

            <p:commandButton id="btn" value="Login" update="panel"
            action="#{securityController.login}"/>
    </p:panel>

    </h:form>

    </h:body>
    </html>

以下是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee  /web-app_3_0.xsd">
  <display-name>rst-web</display-name>
  <!---+++++++++++++++++++++++++++++++++++++++++++++++++Spring++++++++++++++++++++++++++++++++++++++++++-->
  <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/spring-beans.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>
   <!-- ++++++++++++++++++++++++ JSF +++++++++++++++++++++++++++++++++++++++++++++++++++ -->
    <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
       <load-on-startup>1</load-on-startup>
    </servlet>
    <context-param>
       <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
       <param-value>.xhtml</param-value>
    </context-param>
    <servlet-mapping>
      <servlet-name>Faces Servlet</servlet-name>
      <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping> 

    <welcome-file-list>
      <welcome-file>/login.xhtml</welcome-file>
    </welcome-file-list>

  </web-app>

1 个答案:

答案 0 :(得分:0)

securityController是如何解决的?它标有@ManagedBean,使JSF可见,另一方面,JSF在实例化SecurityManager时无法识别spring @Autowired

如果你想坚持弹簧,摆脱@ManagedBean,将@RequestScoped改为@Scope(“request”)并确保你在faces-config中正确设置了ELResolver:

<faces-config>
  <application>
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    ...
  </application>
</faces-config>