任何在ManagedBean类中的@Autowired注入都无法工作,它始终为NULL?

huangapple go评论80阅读模式
英文:

any @Autowired injection within the MangedBean clsass not working , it always equal NULL?

问题

任何@Autowired注入都不起作用,它总是等于NULL ????

--- 每次我尝试在我的mangedBean内注入任何bean,它都返回NULL ???
我尝试了我找到的每个解决方案,但没有结果!!!!!!!!!!!!!!!

每次我尝试注入
customerService,customerDao -- >> 它都是NULL

以下是我的.xml配置文件:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
     version="3.1">

<display-name>spring-mvc-crud-demo</display-name>

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

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<!--servlet-mapping-->
    <!--servlet-name>Faces Servlet</servlet-name-->
    <!--url-pattern>/faces/*</url-pattern-->
<!--/servlet-mapping-->

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>

</web-app>

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:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="
	    http://www.springframework.org/schema/beans
	    http://www.springframework.org/schema/beans/spring-beans.xsd
	    http://www.springframework.org/schema/context
	    http://www.springframework.org/schema/context/spring-context.xsd
	    http://www.springframework.org/schema/mvc
	    http://www.springframework.org/schema/mvc/spring-mvc.xsd
	    http://www.springframework.org/schema/tx
	    http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- 添加组件扫描支持 -->
<context:component-scan base-package="com.luv2MockingjayAndRoaa.springdemo"/>

<!-- 添加转换、格式化和验证支持 -->
<mvc:annotation-driven/>

<!-- 步骤 1:定义数据库 DataSource / 连接池 -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close">
    <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false"/>
    <property name="user" value="springstudent"/>
    <property name="password" value="springstudent"/>
    <property name="initialPoolSize" value="5"/>
    <property name="minPoolSize" value="5"/>
    <property name="maxPoolSize" value="20"/>
    <property name="maxIdleTime" value="30000"/>
</bean>

<!-- 步骤 2:设置 Hibernate session factory -->
<bean id="sessionFactory"
      class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="packagesToScan" value="com.luv2MockingjayAndRoaa.springdemo.entity"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
    <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>

<!-- 步骤 3:设置 Hibernate 事务管理器 -->
<bean id="myTransactionManager"
      class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 步骤 4:基于注解启用基于注解的事务行为 -->
<tx:annotation-driven transaction-manager="myTransactionManager"/>

<!-- 步骤 5:添加对读取 Web 资源的支持:css、图像、js、pdf等... -->
<mvc:resources location="resources/" mapping="/resources/**"/>
</beans>

index.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!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:ui="http://xmlns.jcp.org/jsf/facelets"
   xmlns:h="http://xmlns.jcp.org/jsf/html"
   xmlns:f="http://xmlns.jcp.org/jsf/core"
   xmlns:p="http://primefaces.org/ui">

<f:view>
    <h:outputLabel value="Hello, world"/>
</f:view>

<p:dataTable style="border: 1px;" value="#{demoMB.customers}" var="customer">

<p:column headerText="First Name">
    <p:outputLabel value="#{customer.firstName}"/>
</p:column>
<p:column headerText="Last Name">
    <p:outputLabel value="#{customer.lastName}"/>
</p:column>
<p:column headerText="Email">
    <p:outputLabel value="#{customer.email}"/>
</p:column>
</p:dataTable>
</html>

DemoMB.java

package com.luv2MockingjayAndRoaa.springdemo.mb;

import com.luv2MockingjayAndRoaa.springdemo.dao.base.CustomerDao;
import com.luv2MockingjayAndRoaa.springdemo.entity.Customer;
import com.luv2MockingjayAndRoaa.springdemo.service.impl.CustomerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired

<details>
<summary>英文:</summary>

any @Autowired injection not working , it always equal NULL ????

--- any time i am trying to inject any bean inside my mangedBaean it return with NULL ???
i tried every solution i found but no result !!!!!!!!!!!!!!!!!!!


any time i try to inject 
customerService , customerDao -- &gt;&gt; it comes with NULL



here are my .xml config files :

**web.xml**

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &lt;web-app xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web- 
         app_3_1.xsd&quot;
         version=&quot;3.1&quot;&gt;


    &lt;display-name&gt;spring-mvc-crud-demo&lt;/display-name&gt;


    &lt;welcome-file-list&gt;
        &lt;welcome-file&gt;index.xhtml&lt;/welcome-file&gt;
    &lt;/welcome-file-list&gt;

    &lt;servlet&gt;
        &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
        &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt;
        &lt;init-param&gt;
            &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
            &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;
        &lt;/init-param&gt;
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;


    &lt;context-param&gt;
        &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
        &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;
    &lt;/context-param&gt;


    &lt;!--&lt;servlet-mapping&gt;--&gt;
        &lt;!--&lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;--&gt;
        &lt;!--&lt;url-pattern&gt;/faces/*&lt;/url-pattern&gt;--&gt;
    &lt;!--&lt;/servlet-mapping&gt;--&gt;

    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
        &lt;url-pattern&gt;*.xhtml&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;


    &lt;listener&gt;
        &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
    &lt;/listener&gt;
    &lt;listener&gt;
        &lt;listener-class&gt;com.sun.faces.config.ConfigureListener&lt;/listener-class&gt;
    &lt;/listener&gt;

&lt;/web-app&gt;


**applicationContext.xml**


     &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

      &lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
          xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
          xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
          xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
          xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
          xsi:schemaLocation=&quot;
	    	http://www.springframework.org/schema/beans
	     	http://www.springframework.org/schema/beans/spring-beans.xsd
		   http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd&quot;&gt;

    &lt;!-- Add support for component scanning --&gt;
    &lt;context:component-scan base-package=&quot;com.luv2MockingjayAndRoaa.springdemo&quot;/&gt;


    &lt;!-- Add support for conversion, formatting and validation support --&gt;
    &lt;mvc:annotation-driven/&gt;


    &lt;!-- Step 1: Define Database DataSource / connection pool --&gt;
    &lt;bean id=&quot;myDataSource&quot; class=&quot;com.mchange.v2.c3p0.ComboPooledDataSource&quot;
          destroy-method=&quot;close&quot;&gt;
        &lt;property name=&quot;driverClass&quot; value=&quot;com.mysql.cj.jdbc.Driver&quot;/&gt;
        &lt;property name=&quot;jdbcUrl&quot; value=&quot;jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&quot;/&gt;
        &lt;property name=&quot;user&quot; value=&quot;springstudent&quot;/&gt;
        &lt;property name=&quot;password&quot; value=&quot;springstudent&quot;/&gt;

        &lt;!-- these are connection pool properties for C3P0 --&gt;
        &lt;property name=&quot;initialPoolSize&quot; value=&quot;5&quot;/&gt;
        &lt;property name=&quot;minPoolSize&quot; value=&quot;5&quot;/&gt;
        &lt;property name=&quot;maxPoolSize&quot; value=&quot;20&quot;/&gt;
        &lt;property name=&quot;maxIdleTime&quot; value=&quot;30000&quot;/&gt;
    &lt;/bean&gt;


    &lt;!--&amp;lt;!&amp;ndash; Define reference to the connection pool &amp;ndash;&amp;gt;--&gt;
    &lt;!--&lt;resource-ref&gt;--&gt;
    &lt;!--&lt;description&gt;web customer tracker DataSource&lt;/description&gt;--&gt;
    &lt;!--&lt;res-ref-name&gt;jdbc/web_customer_tracker&lt;/res-ref-name&gt;--&gt;
    &lt;!--&lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;--&gt;
    &lt;!--&lt;res-auth&gt;Container&lt;/res-auth&gt;--&gt;
    &lt;!--&lt;/resource-ref&gt;--&gt;

    &lt;!-- Step 2: Setup Hibernate session factory --&gt;
    &lt;bean id=&quot;sessionFactory&quot;
          class=&quot;org.springframework.orm.hibernate5.LocalSessionFactoryBean&quot;&gt;
        &lt;property name=&quot;dataSource&quot; ref=&quot;myDataSource&quot;/&gt;
        &lt;property name=&quot;packagesToScan&quot; value=&quot;com.luv2MockingjayAndRoaa.springdemo.entity&quot;/&gt;
        &lt;property name=&quot;hibernateProperties&quot;&gt;
            &lt;props&gt;
                &lt;prop key=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
                &lt;prop key=&quot;hibernate.show_sql&quot;&gt;true&lt;/prop&gt;
            &lt;/props&gt;
        &lt;/property&gt;
        &lt;property name=&quot;configLocation&quot; value=&quot;classpath:hibernate.cfg.xml&quot;/&gt;
    &lt;/bean&gt;

    &lt;!-- Step 3: Setup Hibernate transaction manager --&gt;
    &lt;bean id=&quot;myTransactionManager&quot;
          class=&quot;org.springframework.orm.hibernate5.HibernateTransactionManager&quot;&gt;
        &lt;property name=&quot;sessionFactory&quot; ref=&quot;sessionFactory&quot;/&gt;
    &lt;/bean&gt;

    &lt;!-- Step 4: Enable configuration of transactional behavior based on annotations --&gt;	&lt;!-- Detect @Transactional Annotation --&gt;
    &lt;!-- Detect @Transactional Annotation --&gt;
    &lt;tx:annotation-driven transaction-manager=&quot;myTransactionManager&quot;/&gt;

    &lt;!-- step 5: Add support for reading web resources: css, images, js, pdf, etc ... --&gt;
    &lt;!--&lt;mvc:resources location=&quot;physicalDirectoryName/&quot; mapping=&quot;/UrlMappingToThisResource/**&quot;&gt;&lt;/mvc:resources&gt;--&gt;
    &lt;!--/UrlMappingToThisResource/** :-&gt;&gt; ( 2 stars to recurse all subdirectories (images,js,pdf,etc....) --&gt;

    &lt;mvc:resources location=&quot;resources/&quot; mapping=&quot;/resources/**&quot;&gt;&lt;/mvc:resources&gt;
&lt;/beans&gt;



**index.xhtml**


      ?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
      &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
        &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
        &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;
           xmlns:ui=&quot;http://xmlns.jcp.org/jsf/facelets&quot;
           xmlns:h=&quot;http://xmlns.jcp.org/jsf/html&quot;
           xmlns:f=&quot;http://xmlns.jcp.org/jsf/core&quot;
           xmlns:p=&quot;http://primefaces.org/ui&quot;&gt;
  
        &lt;f:view&gt;
            &lt;h:outputLabel value=&quot;Hello, world&quot;/&gt;
        &lt;/f:view&gt;
  
        &lt;p:dataTable style=&quot;border: 1px;&quot; value=&quot;#{demoMB.customers}&quot; var=&quot;customer&quot;&gt;

    &lt;p:column headerText=&quot;First Name&quot;&gt;
        &lt;p:outputLabel value=&quot;#{customer.firstName}&quot;&gt;&lt;/p:outputLabel&gt;
    &lt;/p:column&gt;
    &lt;p:column headerText=&quot;Last Name&quot;&gt;
        &lt;p:outputLabel value=&quot;#{customer.lastName}&quot;&gt;&lt;/p:outputLabel&gt;
    &lt;/p:column&gt;
    &lt;p:column headerText=&quot;Email&quot;&gt;
        &lt;p:outputLabel value=&quot;#{customer.email}&quot;&gt;&lt;/p:outputLabel&gt;
    &lt;/p:column&gt;
    &lt;/p:dataTable&gt;
    &lt;/html&gt;

**DemoMB.Cs**

      package com.luv2MockingjayAndRoaa.springdemo.mb;

      import com.luv2MockingjayAndRoaa.springdemo.dao.base.CustomerDao;
      import com.luv2MockingjayAndRoaa.springdemo.entity.Customer;
      import com.luv2MockingjayAndRoaa.springdemo.service.impl.CustomerServiceImpl;
      import org.springframework.beans.factory.annotation.Autowired;

      import javax.annotation.PostConstruct;
      import javax.faces.bean.ManagedBean;
      import javax.faces.bean.SessionScoped;
      import java.util.ArrayList;
      import java.util.List;


    @ManagedBean(name = &quot;demoMB&quot;)
    @SessionScoped
    public class DemoMB {

    @Autowired
    CustomerServiceImpl customerService;

    @Autowired
    CustomerDao customerDao;
    private List&lt;Customer&gt; customers;
    private List&lt;Customer&gt; customers2;


    @PostConstruct
    public void init() {
        setCustomers(new ArrayList&lt;&gt;());
        try {

            customers = customerService.getAllCustomers();
            customers2 = customerDao.getAllCustomers();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List&lt;Customer&gt; getCustomers() {
        return customers;
    }

    public void setCustomers(List&lt;Customer&gt; customers) {
        this.customers = customers;
    }


}


</details>


# 答案1
**得分**: 1

使用 @Component 替代 @ManagedBean

<details>
<summary>英文:</summary>

Use @Component instead of @ManagedBean

</details>



huangapple
  • 本文由 发表于 2020年4月4日 04:40:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/61020079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定