嗨朋友我正在创建一个使用Spring和JPA而不是Hibernate的简单应用程序,但是在运行应用程序时我得到一个Null指针异常,因为bean没有初始化.Below是我的application-context.xml
<beans>
<mvc:annotation-driven/>
<bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.sms.examination.entity.*" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/examination" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryBean" />
</bean>
<tx:annotation-driven/>
</beans>
NameDao界面: -
package com.sms.examination.dao;
import java.util.List;
import com.sms.examination.entity.Name;
public interface NameDao {
public List<Name> findAll();
}
实施
package com.sms.examination.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.sms.examination.entity.Name;
@Repository
@Transactional
@Component
public class NamedaoImpl implements NameDao
{
@PrsistenceContext
private EntityManager entityManagerFactoryBean;
public List<Name> findAll() {
if(entityManagerFactoryBean==null)
{
System.out.println("manager is null");
}
else
System.out.println("manager is not null");
List<Name> names = entityManagerFactoryBean.createQuery("select s from Name s",Name.class).getResultList();
return names;
}
}
控制器
package com.sms.examination.controller;
import java.util.*;
import com.sms.examination.dao.NameDao;
import com.sms.examination.dao.NamedaoImpl;
import com.sms.examination.entity.Name;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class NameController {
@Autowired
private NameDao namedao;
@Autowired
private NamedaoImpl name;
public void getNames(){
List<Name> namelist=new ArrayList<Name>();
if(namedao==null)
{
System.out.println("name doa is null");
}
namelist=namedao.findAll();
Iterator<Name> it=namelist.iterator();
while(it.hasNext())
{
System.out.println(it.hasNext());
}
}
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:application-context.xml");
NameController names=new NameController();
names.getNames();
}
}
在运行Controller类时,我得到以下输出。请帮助
名称doa是null
,这是因为NameDao
类尚未初始化。
答案 0 :(得分:0)
您需要在应用程序上下文中添加组件扫描
<context:component-scan base-package="com.sms.examination" />