2012-01-15 175 views
0

我正在尝试第一次做一个web应用程序。弹簧配置问题

我尝试没有成功Hibernate和Spring集成,也许你能告诉我什么我做错了

这是我的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:faces="http://www.springframework.org/schema/faces" 
     xmlns:int-security="http://www.springframework.org/schema/integration/security" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:sec="http://www.springframework.org/schema/security" 
     xsi:schemaLocation="http://www.springframework.org/schema/integration/security http://www.springframework.org/schema/integration/security/spring-integration-security-2.0.xsd 
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
          http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd 
          http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd 
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
          http://www.springframework.org/schema/faces http://www.springframework.org/schema/faces/spring-faces-2.0.xsd 
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" 
> 


    <bean id="propertyConfigurer" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
      p:location="WEB-INF/jdbc.properties" /> 

    <bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
      p:driverClassName="${jdbc.driverClassName}" 
      p:url="${jdbc.url}" 
      p:username="${jdbc.username}" 
      p:password="${jdbc.password}" /> 

    <context:component-scan base-package="com.mac" /> 

    <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" > 
     <property name="driverClass" value="${jdbc.driverClassName}" /> 
     <property name="jdbcUrl" value="${jdbc.url}"/> 
     <property name="user" value="${jdbc.username}"/> 
     <property name="password" value="${jdbc.password}"/>  
     <property name="maxPoolSize" value="10" /> 
     <property name="maxStatements" value="0" /> 
     <property name="minPoolSize" value="5" /> 
    </bean> 

    <bean id="entityManagerFactory" 
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" > 
      </bean> 
     </property> 
     <property name="dataSource" ref="pooledDataSource" /> 
     <property name="persistenceUnitName" value="macPU"/> 
    </bean> 

    <bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" />  
    </bean> 

    <tx:annotation-driven /> 

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) --> 

</beans> 

这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
    <context-param> 
     <param-name>javax.faces.PROJECT_STAGE</param-name> 
     <param-value>Development</param-value> 
    </context-param> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>Faces Servlet</servlet-name> 
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>2</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>/faces/*</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>*.htm</url-pattern> 
    </servlet-mapping> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>faces/index.xhtml</welcome-file> 
    </welcome-file-list> 
</web-app> 

最后persistence.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> 
    <persistence-unit name="macPU" transaction-type="RESOURCE_LOCAL"> 
     <!--<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>--> 
     <class>com.mac.htest.map.TipoHabitacion</class> 
     <class>com.mac.htest.map.TipoIdentificacion</class> 
     <class>com.mac.htest.map.Habitacion</class> 
     <class>com.mac.htest.map.Cliente</class> 
     <class>com.mac.htest.map.Reserva</class> 
     <class>com.mac.htest.map.Nacionalidad</class> 
     <exclude-unlisted-classes>false</exclude-unlisted-classes> 
     <properties> 
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hotel"/> 
      <property name="javax.persistence.jdbc.password" value="$far374"/> 
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> 
      <property name="javax.persistence.jdbc.user" value="root"/> 
     </properties> 
    </persistence-unit> 
</persistence> 

我的主要问题(我不知道是否唯一)是我不能@Inject类。

我提前感谢这个(也许更多的建议)你的帮助

+0

我不确定你是否已经使用它。但是,如果您需要使用spring bean,是不是应该在faces-context.xml中使用委托变量解析​​器? ' <可变解析器> org.springframework.web.jsf.DelegatingVariableResolver ' – 2012-01-15 02:59:11

回答

0

如果你想通过Spring使用@Inject之类的东西@Autowire你需要听从directions in the Spring docs (3.0)(和current version)。你既没有包含Spring版本,也没有包含你的pom文件,也没有包含你想要如何使用@Inject,但我怀疑你是否将JSR-330 jar包含为依赖项。