2014-07-12 101 views
0

我已將我的項目分爲2個包。我正在使用Maven。使用Hibernate包從另一個包中訪問數據庫

package 1 它是一個hibernate包,意味着它包含POJO類,DAO類和接口,BO類和接口以及hibernate配置。它生成一個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方法應該從數據庫返回所有Bank的列表,返回類型是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,無論您需要的內容。

謝謝。

+0

ServiceFactory.getBankBankService()返回null,因爲bankBankService沒有初始化... – MGorgon

+0

@Morgonon是的,我在幾分鐘前意識到這一點,我應該如何初始化它?是不是在'faces-config.xml'中提到它足夠了? –

+0

@AliBassam bankBankBO是Spring Bean,而ServiceFactory是JSF Managed Bean,所以注入bankBankService不起作用。閱讀有關JSF-Spring集成的信息,例如:http://stackoverflow.com/questions/8925170/jsf-2-inject-spring-bean-service-with-managedproperty-and-no-xml – MGorgon

回答

0

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 bean中JSF豆注射。

相關問題