2014-03-05 21 views
0

我試圖注入DAO到@Service組成部分,但我得到這個錯誤:顯示java.lang.NullPointerException上@Inject道

Exception in thread "main" java.lang.NullPointerException at it.cle.project.service.impl.TestEntityServiceImpl.getListTestEntity(TestEntityServiceImpl.java:24).

無法調用DAO儘管註釋這是空@自動裝配 下面我的代碼:

的context.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:c="http://www.springframework.org/schema/c" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:util="http://www.springframework.org/schema/util" 
    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-4.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd 
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee-4.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-4.0.xsd"> 

<!-- INIZIO IMPOSTAZIONI LEGATE ALLE ANNOTATIONS --> 
<tx:annotation-driven/> 
<context:property-placeholder location="classpath:hibernate.properties"/> 
<context:component-scan base-package="it.cle.project.service.impl" /> 
<context:component-scan base-package="it.cle.project.dao.hbn" /> 
<context:component-scan base-package="it.cle.project.dao.hibernate" /> 
<!-- FINE IMPOSTAZIONI LEGATE ALLE ANNOTATIONS --> 


<!-- INIZIO IMPOSTAZIONI LEGATE AD ALTRI FILE DI CONFIGURAZIONE --> 
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location" value="classpath:hibernate.properties"/> 
</bean> 
<!-- FINE IMPOSTAZIONI LEGATE AD ALTRI FILE DI CONFIGURAZIONE --> 


<!-- INIZIO IMPOSTAZIONI LEGATE ALLA CONNESSIONE --> 
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" /> 

<util:properties id="hibernateProperties"> 
    <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl_auto}</prop> 
</util: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}" /> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean " 
    p:dataSource-ref="dataSource" p:packagesToScan="it.cle.project.model" 
    p:hibernateProperties-ref="hibernateProperties" /> 
<!-- FINE IMPOSTAZIONI LEGATE ALLA CONNESSIONE --> 

App.java

package it.cle.project; 
import it.cle.project.model.TestEntity; 
import it.cle.project.service.impl.TestEntityServiceImpl; 
import java.util.List; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
public class App 
{ 
public static void main(String[] args) 
{ 
    ApplicationContext context = new ClassPathXmlApplicationContext("context.xml"); 


    System.out.println("Hello World!"); 
    TestEntity testEntity = new TestEntity(); 
    testEntity.setCampoUno("Campo Uno"); 
    testEntity.setCampoDue("Campo Due"); 
    testEntity.setEmail("[email protected]"); 



    TestEntityServiceImpl testEntityServiceImpl = new TestEntityServiceImpl(); 

    List<TestEntity> testEntitys = testEntityServiceImpl.getListTestEntity(); 


    } 
} 

DAO接口

package it.cle.project.dao; 
import java.io.Serializable; 
import java.util.List; 
public interface Dao<T extends Object> { 
void create(T t); 

T get(Serializable id); 

T load(Serializable id); 

List<T> getAll(); 

void update(T t); 

void delete(T t); 

void deleteById(Serializable id); 

void deleteAll(); 

long count(); 

boolean exists(Serializable id); 
} 

TestEntityDAO接口

package it.cle.project.dao; 
import it.cle.project.model.TestEntity; 
import java.util.List; 
public interface TestEntityDao extends Dao<TestEntity> { 
List<TestEntity> findByEmail(String email); 
} 

AbstractHbnDao抽象類:

package it.cle.project.dao.hibernate; 
import it.cle.project.dao.Dao; 
import java.io.Serializable; 
import java.lang.reflect.Method; 
import java.lang.reflect.ParameterizedType; 
import java.util.Date; 
import java.util.List; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
import org.springframework.util.ReflectionUtils; 
@Service 
public abstract class AbstractHbnDao<T extends Object> implements Dao<T> { 

@Autowired 
private SessionFactory sessionFactory; 

private Class<T> domainClass; 

protected Session getSession() { 
    return sessionFactory.getCurrentSession(); 
} 

@SuppressWarnings("unchecked") 
private Class<T> getDomainClass() { 
    if (domainClass == null) { 
    ParameterizedType thisType = 
    (ParameterizedType) getClass().getGenericSuperclass(); 
    this.domainClass = 
    (Class<T>) thisType.getActualTypeArguments()[0]; 
    } 
    return domainClass; 
} 

private String getDomainClassName() { 
    return getDomainClass().getName(); 
} 

public void create(T t) { 
    Method method = ReflectionUtils.findMethod(
    getDomainClass(), "setDataCreazione", 
    new Class[] { Date.class }); 
    if (method != null) { 
     try { 
     method.invoke(t, new Date()); 
     } catch (Exception e) { /* Ignore */ } 
    } 
    getSession().save(t); 
} 

@SuppressWarnings("unchecked") 
public T get(Serializable id) { 
    return (T) getSession().get(getDomainClass(), id); 
} 

@SuppressWarnings("unchecked") 
public T load(Serializable id) { 
    return (T) getSession().load(getDomainClass(), id); 
} 

@SuppressWarnings("unchecked") 
public List<T> getAll() { 
    return getSession() 
    .createQuery("from " + getDomainClassName()) 
    .list(); 
} 

public void update(T t) { getSession().update(t); } 

public void delete(T t) { getSession().delete(t); } 

public void deleteById(Serializable id) { delete(load(id)); } 

public void deleteAll() { 
    getSession() 
    .createQuery("delete " + getDomainClassName()) 
    .executeUpdate(); 
} 

public long count() { 
    return (Long) getSession() 
    .createQuery("select count(*) from " + getDomainClassName()) 
    .uniqueResult(); 
} 

public boolean exists(Serializable id) { return (get(id) != null); } 
} 

HbnTestEntityDao類DAO

package it.cle.project.dao.hbn; 
import it.cle.project.dao.TestEntityDao; 
import it.cle.project.dao.hibernate.AbstractHbnDao; 
import it.cle.project.model.TestEntity; 
import java.util.List; 
import org.springframework.stereotype.Repository; 
@Repository 
public class HbnTestEntityDao extends AbstractHbnDao<TestEntity> implements TestEntityDao { 

@SuppressWarnings("unchecked") 
public List<TestEntity> findByEmail(String email) { 
    return getSession() 
    .getNamedQuery("findContactsByEmail") 
    .setString("email", "%" + email + "%") 
    .list(); 
} 
} 

TestEntityService接口服務

package it.cle.project.service; 
import it.cle.project.model.TestEntity; 
import java.util.List; 
public interface TestEntityService { 

void createTestEntity(TestEntity testEntity); 

List<TestEntity> getListTestEntity(); 

List<TestEntity> getTestEntityByEmail(String email); 

TestEntity getTestEntity(Integer id); 

void updateTestEntity(TestEntity testEntity); 

void deleteTestEntity(Integer id); 
} 

TestEntityServiceImpl

package it.cle.project.service.impl; 
import it.cle.project.dao.TestEntityDao; 
import it.cle.project.model.TestEntity; 
import it.cle.project.service.TestEntityService; 
import java.util.List; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Transactional; 
@Service 
@Transactional 
public class TestEntityServiceImpl implements TestEntityService { 

@Autowired 
private TestEntityDao testEntityDao; 

public void createTestEntity(TestEntity testEntity) { 
    testEntityDao.create(testEntity); 
} 

public List<TestEntity> getListTestEntity() { 
    return testEntityDao.getAll(); 
} 

public List<TestEntity> getTestEntityByEmail(String email) { 
    return testEntityDao.findByEmail(email); 
} 

public TestEntity getTestEntity(Integer id) { 
    return testEntityDao.get(id); 
} 

public void updateTestEntity(TestEntity testEntity) { 
    testEntityDao.update(testEntity); 
} 

public void deleteTestEntity(Integer id) { 
    testEntityDao.deleteById(id); 
} 

} 

任何想法? 謝謝。

回答

0

你的TestServiceImpl應該是spring管理bean,應該從Spring應用程序上下文(通過注入或通過顯式詢問上下文)獲取。當組件掃描工作時,您的TestServiceImpl已經使用Spring自己提供的名稱進行管理(com ... TestServiceImpl變爲testServiceImpl)。你可以給你的名字就像

@Service("myTestServiceImpl") 

的,而不是自己創建豆你可以查詢從應用程序上下文這個命名bean並使用它。

+0

我已將@Service改爲@Service(「testServiceImpl」) 和我的電話 TestEntityServiceImpl testEntityServiceImpl =(TestEntityServiceImpl)context.getBean(「testServiceImpl」); 給出此錯誤:線程「main」中的異常java.lang.ClassCastException:com.sun.proxy。$ Proxy29無法在it.cle.project.App.main上強制轉換爲it.cle.project.service.impl.TestEntityServiceImpl (附錄。java:32) –

+0

默認情況下,只要TestServiceImpl實現接口TestEntityService,Spring就使用JDK動態代理爲目標類(本例中爲TestServiceImpl)生成代理。所以你會將查詢轉換爲TestEntityService而不是TestServiceImpl。順便說一下,在界面方面工作總是很好的做法 – Shailendra

+0

太棒了!它的工作原理!謝謝! –

0

這是一些文字牆。我停在:

TestEntityServiceImpl testEntityServiceImpl = new TestEntityServiceImpl(); 

您創建了一個非託管bean。春天無法控制。把TestEntityServiceImpl放到你的spring上下文中。

+0

我將TestEntityServiceImpl配置爲@Service。 錯了? –

+0

這沒有錯。你用'new'在spring容器外創建了一個實例是錯誤的。 –

相關問題