【问题标题】:Using Hibernate package to access database from another package使用 Hibernate 包从另一个包访问数据库
【发布时间】:2014-09-03 05:28:48
【问题描述】:

我已将我的项目分为 2 个包。我正在使用 Maven。

包 1 它是一个休眠包,意味着它包含 POJO 类、DAO 类和接口、BO 类和接口以及休眠配置。它会生成一个 JAR 文件。

包 2 这是一个 JSF 包,只包含网页和托管 bean,我也在使用 ICEfaces。

我的目标是将第一个包的 JAR 文件包含在第二个包的构建路径中,并使用 BO 类的方法从网页访问数据库。

首先我将列出第一个包的所有配置文件:

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

    <bean id="bankBankBO" 
         class="bdl.cdr.core.bo.impl.BankBankBOImpl" >
        <property name="bankBankDAO" ref="bankBankDAO" />
    </bean>

    <bean id="bankBankDAO" 
         class="bdl.cdr.core.dao.impl.BankBankDAOImpl" >
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

DataSource.xml

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

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>jdbc.properties</value>
        </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
</beans>

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

<!-- Hibernate session factory -->
<bean id="sessionFactory" 
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource">
      <ref bean="dataSource"/>
    </property>

    <property name="hibernateProperties">
       <props>
         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
         <prop key="hibernate.show_sql">true</prop>
         <prop key="current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</prop>
       </props>
    </property>

    <property name="annotatedClasses">
    <list>
        <value>bdl.cdr.core.pojo.BankBank</value>
    </list>
    </property>

    </bean>
</beans>

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db2inst1
jdbc.username=root
jdbc.password=root

BeanLocations.xml

<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <!-- Database Configuration -->
    <import resource="DataSource.xml"/>
    <import resource="HibernateSessionFactory.xml"/>
    <import resource="AppContext.xml"/>

</beans>

我正在使用 JUnit 进行测试,一切正常。

现在我所做的是获取 Maven 构建的 JAR 文件,并将其包含在第二个包的构建路径中。

这就是我试图做的:

我创建了一个服务工厂(或 BO 工厂)类:

public class ServiceFactory {

    public static BankBankBO bankBankService;

    public static BankBankBO getBankBankService() {
        return bankBankService;
    }
}

faces-config.xml我添加了以下内容:

<managed-bean>
        <managed-bean-name>serviceFactory</managed-bean-name>
        <managed-bean-class>bdl.cdr.portlet.utils.ServiceFactory</managed-bean-class>
        <managed-bean-scope>application</managed-bean-scope>
        <managed-property>
            <property-name>bankBankService</property-name>
            <property-class>bdl.cdr.core.bo.BankBankBO</property-class>
            <value>#{bankBankBO}</value>
        </managed-property>
    </managed-bean>

在另一个链接到网页的 bean 中,我在按钮上创建了一个点击事件:

public void print(ActionEvent ae) {
        ServiceFactory.getBankBankService().findAll();
    }

findAll方法,应该返回数据库中所有银行的列表,返回类型是hibernate包中的POJO类,叫做BankBank

单击按钮后,我收到以下异常:

SEVERE: Received 'java.lang.NullPointerException' when invoking action listener '#{jasperBean.print}' for component '_t38'
Jul 12, 2014 11:42:29 PM javax.faces.event.MethodExpressionActionListener processAction
SEVERE: java.lang.NullPointerException
    at bdl.cdr.portlet.managedbeans.main.interfacemodule.JasperBean.print(JasperBean.java:75)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:775)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:786)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1251)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:744)

Jul 12, 2014 11:42:29 PM org.icefaces.impl.application.ExtendedExceptionHandler handle
WARNING: queued exception
javax.faces.event.AbortProcessingException: /index.xhtml @23,74 actionListener="#{jasperBean.print}": java.lang.NullPointerException
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:182)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:775)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:786)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1251)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
    at bdl.cdr.portlet.managedbeans.main.interfacemodule.JasperBean.print(JasperBean.java:75)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148)
    ... 21 more

Jul 12, 2014 11:42:29 PM com.sun.faces.context.AjaxExceptionHandlerImpl log
SEVERE: JSF1073: javax.faces.event.AbortProcessingException caught during processing of INVOKE_APPLICATION 5 : UIComponent-ClientId=j_idt37:_t38, Message=/index.xhtml @23,74 actionListener="#{jasperBean.print}": java.lang.NullPointerException
Jul 12, 2014 11:42:29 PM com.sun.faces.context.AjaxExceptionHandlerImpl log
SEVERE: /index.xhtml @23,74 actionListener="#{jasperBean.print}": java.lang.NullPointerException
javax.faces.event.AbortProcessingException: /index.xhtml @23,74 actionListener="#{jasperBean.print}": java.lang.NullPointerException
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:182)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:775)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:786)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1251)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
    at bdl.cdr.portlet.managedbeans.main.interfacemodule.JasperBean.print(JasperBean.java:75)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148)
    ... 21 more

我可以提供所有其他类和文件的内容,web.xmlfaces-config.xml 以及您需要的任何内容。

谢谢。

【问题讨论】:

  • ServiceFactory.getBankBankService() 返回 null,因为 bankBankService 未初始化...
  • @MGorgon 是的,我几分钟前就意识到了这一点,我应该如何初始化它?在faces-config.xml 中提到它还不够吗?
  • @AliBassam bankBankBO 是 Spring Bean,而 ServiceFactory 是 JSF Managed Bean,因此注入 bankBankService 不起作用。阅读 JSF-Spring 集成,例如:stackoverflow.com/questions/8925170/…

标签: java spring hibernate maven jsf


【解决方案1】:

bankBankBO 是 Spring Bean,而 ServiceFactory 是 JSF Managed Bean,因此注入 bankBankService 不起作用。

你必须添加这个:

<application>
    <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
    <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
</application>

到你的 faces-config.xml 中,以启用 spring beans 到 jsf beans 注入。

【讨论】:

    猜你喜欢
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    相关资源
    最近更新 更多