[英]Spring Hibernate NullPointerException Dependency Injection
I have a maven project.It has a dao class,an entity class and a junit test case.I am using dependency injection for the dao class but it gives a null pointer exception.I know this is a common error faced in spring but they say this happens when we use the new operator to create an instance of a spring component but I haven't used the new operator for the dao class which is the only class with the @Component annotation. I have a maven project.It has a dao class,an entity class and a junit test case.I am using dependency injection for the dao class but it gives a null pointer exception.I know this is a common error faced in spring but they假设当我们使用 new 运算符创建 spring 组件的实例时会发生这种情况,但我没有为 dao class 使用 new 运算符,这是唯一带有 @Component 注释的 class。
Entity Class实体 Class
package com.lti.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Sport {
@Id
@GeneratedValue
private int sportId;
private String sportName;
public int getSportId() {
return sportId;
}
public void setSportId(int sportId) {
this.sportId = sportId;
}
public String getSportName() {
return sportName;
}
public void setSportName(String sportName) {
this.sportName = sportName;
}
}
Dao Class刀Class
package com.lti.dao;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import org.springframework.stereotype.Component;
@Component
public class GenericDao {
@PersistenceContext
private EntityManager em;
@Transactional
public Object save(Object e) {
Object obj=em.merge(e);
return obj;
}
public Object fetchById(Class c1,Object id) {
return em.find(c1,id);
}
}
SportTest运动测试
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.lti.dao.GenericDao;
import com.lti.entity.Sport;
public class SportTest {
@Autowired
private GenericDao dao;
@Test
public void add() {
Sport s=new Sport();
s.setSportName("Football");
dao.save(s);
}
}
app-config.xml应用程序配置.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"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Loading annotated beans -->
<context:component-scan base-package="com.lti.dao" />
<!-- DataSource configuration -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" init-method="createDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" />
<property name="username" value="hr" />
<property name="password" value="hr" />
<property name="initialSize" value="1" />
</bean>
<!-- Spring/Hibernate Integration Settings -->
<!-- This bean entry below will create the EntityManagerFactory
& EntityManager object for us. Also we don't need a separate
META-INF/persistence.xml file as all the required ORM settings
can be done within this bean configuration itself -->
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.lti.entity" /> <!-- Location of @Entity classes -->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- This bean entry will take of managing the transactions(begin/commit/rollback) in our code -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf" />
</bean>
<!-- Enabling support for @Transactional annotation -->
<tx:annotation-driven />
</beans>
pom.xml pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lti.sample</groupId>
<artifactId>spring-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.5</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.30.Final</version>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>18.3.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
</project>
Annotate SportTest class with @SpringBootTest使用 @SpringBootTest 注释 SportTest class
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.lti.dao.GenericDao;
import com.lti.entity.Sport;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class SportTest {
@Autowired
private GenericDao dao;
@Test
public void add() {
Sport s=new Sport();
s.setSportName("Football");
dao.save(s);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.