我正在尝试使用@Autowire注入DAO。将日志级别设置为debug,我可以看到bean已创建并连接到服务类,但是,当我尝试使用注入的对象时,会抛出nullpointerexecption。
英语不是我的主要语言,但是查看下面的日志,似乎DAO已创建并正确连接到服务:
DefaultListableBeanFactory:221 - Creating shared instance of singleton bean 'ticketServiceImpl'
DefaultListableBeanFactory:450 - Creating instance of bean 'ticketServiceImpl'
InjectionMetadata:72 - Registered injected element on class [br.com.xpto.sys.business.impl.TicketServiceImpl]: AutowiredFieldElement for private br.com.xpto.sys.dao.api.TicketDAO br.com.xpto.sys.business.impl.TicketServiceImpl.dao
DefaultListableBeanFactory:523 - Eagerly caching bean 'ticketServiceImpl' to allow for resolving potential circular references
InjectionMetadata:86 - Processing injected element of bean 'ticketServiceImpl': AutowiredFieldElement for private br.com.xpto.sys.dao.api.TicketDAO br.com.xpto.sys.business.impl.TicketServiceImpl.dao
DefaultListableBeanFactory:221 - Creating shared instance of singleton bean 'ticketDAOImpl'
DefaultListableBeanFactory:450 - Creating instance of bean 'ticketDAOImpl'
AnnotationTransactionAttributeSource:108 - Adding transactional method 'TicketDAOImpl.findByTicketNumber' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; ''
InfrastructureAdvisorAutoProxyCreator:538 - Creating implicit proxy for bean 'ticketDAOImpl' with 0 common interceptors and 1 specific interceptors
JdkDynamicAopProxy:115 - Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [br.com.xpto.sys.dao.impl.TicketDAOImpl@2242b3b8]
DefaultListableBeanFactory:478 - Finished creating instance of bean 'ticketDAOImpl'
AutowiredAnnotationBeanPostProcessor:480 - Autowiring by type from bean name 'ticketServiceImpl' to bean named 'ticketDAOImpl'
DefaultListableBeanFactory:478 - Finished creating instance of bean 'ticketServiceImpl'
NPE进入dao.findByTicketId(ticketId)调用。由于dao为null,因此抛出异常。
在我阅读this和this帖后,我尝试做了一些小修改,但没有幸运。
下面我发布了代码的主要部分。欢迎任何建议。
更新
根据要求,我还发布了如何实例化TicketServiceImpl类和堆栈跟踪。
该类在ItemProcessor bean(spring批处理)
中实例化public class TicketItemProcessor implements ItemProcessor<Ticket, TicketLog> {
@Override
public TicketLog process(Ticket item) throws Exception {
TicketServiceImpl srv = new TicketServiceImpl();
Ticket t = srv.findByTicketId(item.getIdTicket());
return new TicketLog();
}
}
TicketServiceImpl
@Service
public class TicketServiceImpl implements TicketService {
@Autowired
private TicketDAO dao;
@Override
public Ticket findByTicketId(int ticketId) {
return dao.findByTicketId(ticketId);
}
}
TicketDAOImpl
@Repository
public class TicketDAOImpl extends implements TicketDAO {
@Override
public Ticket findByTicketId(int ticketId) {
String jpql = "SELECT t FROM Ticket t WHERE t.id = :ticketId";
Query query = getSession().createQuery(jpql);
query.setParameter("ticketId", ticketId);
query.setMaxResults(1);
Object obj = query.uniqueResult();
return (Ticket) obj;
}
}
TicketDAO
public interface TicketDAO extends DAO<Ticket> {
Ticket findByTicketId(int ticketId);
}
DAO
public interface DAO<E> extends Serializable {
List<E> listAll();
}
context.xml中
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:batch="http://www.springframework.org/schema/batch" 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/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-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/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd">
<context:annotation-config />
<context:component-scan base-package="br.com.xpto.sys" />
<bean id="dbDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://server;databaseName=db" />
<property name="username" value="usr" />
<property name="password" value="pwd" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dbDataSource" />
<property name="packagesToScan" value="br.com.xpto.sys.model.entities" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">br.com.xpto.sys.dao.utils.SQLServerDialectOverrider</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">false</prop>
<prop key="hibernate.connection.release_mode">after_transaction</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" mode="proxy"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
</beans>
堆栈跟踪
AbstractStep:222 - Encountered an error executing step step1 in job job-ProcessTickets
java.lang.NullPointerException
at br.com.xpto.sys.business.impl.TicketServiceImpl.findByTicketId(TicketServiceImpl.java:83)
at br.com.xpto.sys.jobs.spring.TicketItemProcessor.process(TicketItemProcessor.java:24)
at br.com.xpto.sys.jobs.spring.TicketItemProcessor.process(TicketItemProcessor.java:1)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.doProcess(SimpleChunkProcessor.java:126)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.transform(SimpleChunkProcessor.java:293)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:192)
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:75)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:395)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:267)
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:77)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:368)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:253)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:195)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:141)
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:60)
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:151)
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:130)
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:135)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:301)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:134)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:127)
at br.com.xpto.sys.jobs.AppStandalone.run(AppStandalone.java:111)
at br.com.xpto.sys.jobs.AppStandalone.main(AppStandalone.java:75)