Spring的Task Scheduler中的EntityManager为null

时间:2015-08-11 19:35:21

标签: java spring spring-mvc spring-security spring-scheduled

我一直在尝试创建一个删除数据库中所有记录的作业。这项工作是通过我的webservice部署的。在webservice中,我可以轻松访问我的EntityManager,到目前为止它没有任何问题。但是,每当我尝试在我的调度程序中访问EntityManager时,它就会给我NullPointerException。

@Service
public class DeletionJob {

   @PersistenceContext(unitName = "my_pu")
    private EntityManager em;

    @Scheduled(fixedDelay=10000)
    public void run() {
        boolean flag = false;
        TypedQuery<Abc> query
                = em.createNamedQuery("deleteAll",ABC.class);
    }
}

这是我的课程,但它在第二行的运行方法上给了我一个例外。

@EnableWebMvc
@Configuration
@EnableScheduling
@ComponentScan({ "com.jobs.*" })
@javax.ws.rs.ApplicationPath("webresources")
public class AppConfig extends Application {
     @Bean
     public DeletionJob myDeletionJob() {
         return new DeletionJob();
     } 
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver 
                          = new InternalResourceViewResolver();
        return viewResolver;
    }
        @Bean
        public REST getService(){
            return new REST(); 
        }
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
        addRestResourceClasses(resources);
        return resources;
    }


    /**
     * Do not modify addRestResourceClasses() method.
     * It is automatically populated with
     * all resources defined in the project.
     * If required, comment out calling this method in getClasses().
     */
    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(com.service.REST.class);
        // Tried to include deletionjob class here but, no luck still the same.
    // resources.add(com.jobs.DeletionJob.class);
    }
}

我没有任何配置文件。因为我正在使用注释。但是,我不确定我做错了什么。由于我的所有服务都运行良好,没有与数据库相关的交易问题。

编辑:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="my_pu" transaction-type="JTA">
    <jta-data-source>ds</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="eclipselink.logging.level" value="FINE"/>
    </properties>
  </persistence-unit>
</persistence>

0 个答案:

没有答案