我总是得到null,我不知道为什么。我很想找到解决方案,但我找不到它。我正在使用Spring 5.0.1尝试使用jpa注入,所以我将放置部分代码。
Persistence.xml(在META-INF目录中)
<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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- para que reconozca los Servicios, DAOs, etc anotados -->
<!--<context:component-scan base-package="ttps.daosjpa" /> -->
<context:component-scan base-package="Controlador,DAO,Modelo" />
<!-- propiedades de la Base de Datos
<context:property-placeholder location="classpath:database.properties" />-->
<!-- DataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost/ttps2017" />
<property name="user" value="root" />
<property name="password" value="" />
<property name="acquireIncrement" value="2" />
<property name="minPoolSize" value="20" />
<property name="maxPoolSize" value="50" />
<property name="maxIdleTime" value="600 " />
</bean>
<!-- Configuración JPA -->
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- <property name="packagesToScan" value="ttps.springmvc.entities" /> -->
<property name="packagesToScan" value="Controlador,DAO,Modelo" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Manejador de Transacciones -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager ">
<property name="entityManagerFactory" ref="emf" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
这是我的通用DAOHibernate ........................................... .................................................. .................................................. .................................................. ..................................................
@Transactional
public class GenericDAOHibernate<T> implements GenericDAO<T> {
private Class<T> PersistentClass;
private EntityManager entityManager;
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.entityManager= em;
}
public EntityManager getEntityManager() {
return this.entityManager;
}
public GenericDAOHibernate(Class<T> entidad) {
this.PersistentClass = entidad;
}
@Override
public T persistir(T entity) {
System.out.println("");
System.out.println(this.getEntityManager());
System.out.println("");
this.getEntityManager().persist(entity);
return entity;
}
}
这是我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/META-INF/newPersistence.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Configuration for the DispatcherServlet
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> -->
答案 0 :(得分:0)
我认为这里应该是点而不是逗号。
<context:component-scan base-package="Controlador.DAO.Modelo" />