有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在MangedBean clsass中的任何@Autowired注入都不起作用,它总是等于NULL吗?

任何@Autowired注入都不起作用,它始终等于NULL

---每当我试图在我的mangedBaean中注入任何bean时,它都会返回NULL??? 我尝试了我找到的每一个解决方案,但没有结果

任何时候我尝试注射 客户服务,客户道-->&燃气轮机;它带有空值

这是我的护照。xml配置文件:

网络。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>

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

<!-- Add support for component scanning -->
<context:component-scan base-package="com.luv2MockingjayAndRoaa.springdemo"/>


<!-- Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>


<!-- Step 1: Define Database DataSource / connection pool -->
<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"/>

    <!-- these are connection pool properties for C3P0 -->
    <property name="initialPoolSize" value="5"/>
    <property name="minPoolSize" value="5"/>
    <property name="maxPoolSize" value="20"/>
    <property name="maxIdleTime" value="30000"/>
</bean>


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

<!-- Step 2: Setup 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>

<!-- Step 3: Setup Hibernate transaction manager -->
<bean id="myTransactionManager"
      class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- Step 4: Enable configuration of transactional behavior based on annotations -->    <!-- Detect @Transactional Annotation -->
<!-- Detect @Transactional Annotation -->
<tx:annotation-driven transaction-manager="myTransactionManager"/>

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

<mvc:resources location="resources/" mapping="/resources/**"></mvc:resources>

索引。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:outputLabel>
</p:column>
<p:column headerText="Last Name">
    <p:outputLabel value="#{customer.lastName}"></p:outputLabel>
</p:column>
<p:column headerText="Email">
    <p:outputLabel value="#{customer.email}"></p:outputLabel>
</p:column>
</p:dataTable>
</html>

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 = "demoMB")
@SessionScoped
public class DemoMB {

@Autowired
CustomerServiceImpl customerService;

@Autowired
CustomerDao customerDao;
private List<Customer> customers;
private List<Customer> customers2;


@PostConstruct
public void init() {
    setCustomers(new ArrayList<>());
    try {

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

public List<Customer> getCustomers() {
    return customers;
}

public void setCustomers(List<Customer> customers) {
    this.customers = customers;
}

}


共 (1) 个答案

  1. # 1 楼答案

    使用@Component而不是@ManagedBean