英文:
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 -- >> it comes with NULL
here are my .xml config files :
**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">
<!-- 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>
</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: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;
}
}
</details>
# 答案1
**得分**: 1
使用 @Component 替代 @ManagedBean。
<details>
<summary>英文:</summary>
Use @Component instead of @ManagedBean
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论