Spring服务层对象为null

时间:2013-07-15 14:19:38

标签: spring hibernate quartz-scheduler

您好我正在学习Spring并且遇到了问题。 我的结构如下:

package com.edfx.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "CUSTOMER")
public class Customer {

private long customer_id;
private String name;
private String address;
private Date created_date;


@Id
@Column(name = "CUSTOMER_ID")
public long getCustomer_id() {
    return customer_id;
}

public void setCustomer_id(long customer_id) {
    this.customer_id = customer_id;
}


@Column(name = "NAME")
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}


@Column(name = "ADDRESS")
public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}


@Column(name = "CREATED_DATE")
public Date getCreated_date() {
    return created_date;
}

public void setCreated_date(Date created_date) {
    this.created_date = created_date;
}

@Override
public String toString(){

    StringBuffer strBuffer = new StringBuffer();
    strBuffer.append("customer_id:").append(getCustomer_id());
    strBuffer.append(", name : ").append(getName());
    strBuffer.append(", address: ").append(getAddress());
    strBuffer.append(", created_date: ").append(getCreated_date());
    return strBuffer.toString();
}

}

Managed Bean是:

package com.edfx.managedbean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import org.springframework.dao.DataAccessException;
import com.edfx.customer.service.ICustomerService;
import com.edfx.model.Customer;

@ManagedBean(name = "customerMB")
@SessionScoped
public class CustomerManagedBean implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 8839370045113377019L;

private static final String SUCCESS = "success";
private static final String ERROR = "error";

// Spring Customer Service Injection
ICustomerService customerService;

public ICustomerService getCustomerService() {
    System.out.println("customerService -- >"+customerService);
    return customerService;
}

public void setCustomerService(ICustomerService customerService) {
    this.customerService = customerService;
}

List<Customer> userList;
private long customer_id;
private String name;
private String address;
private Date created_date;

public String addUser(){

    System.out.println("Managed Bean Add User..");
    try {
        Customer customer = new Customer();
        System.out.println("ID :: "+getCustomer_id());
        customer.setCustomer_id(getCustomer_id());
        System.out.println("Name :: "+getName());
        customer.setName(getName());
        System.out.println("Address :: "+getAddress());
        customer.setAddress(getAddress());
        System.out.println("Created Date :: "+new Date());
        customer.setCreated_date(new Date());

        //Service Provider
        getCustomerService().addUser(customer);
        return SUCCESS;

    } catch (DataAccessException e) {
        System.err.println("Error Occured :: "+e.getMessage());
    }
    return ERROR;
}

public void reset(){
    this.setCustomer_id(0);
    this.setName("");
    this.setAddress("");
    this.setCreated_date(null);
}

public List<Customer> getUserList(){

    userList = new ArrayList<Customer>();
    System.out.println("BEAN :: "+getCustomerService().getUsers());
    userList.addAll(getCustomerService().getUsers());
    return userList;
}


public void setUserList(List<Customer> userList) {
    this.userList = userList;
}

public long getCustomer_id() {
    return customer_id;
}

public void setCustomer_id(long customer_id) {
    this.customer_id = customer_id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public Date getCreated_date() {
    return created_date;
}

public void setCreated_date(Date created_date) {
    this.created_date = created_date;
}

}

applicationContextFile是:

<beans  xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <context:component-scan base-package="com.edfx.managedbean"/>
    <context:annotation-config/>

    <!-- Bean Declaration -->
    <bean id="Customer" class="com.edfx.model.Customer"/>

    <!-- Customer Service Declaration -->
    <bean id="customerService" class="com.edfx.customer.service.CustomerService" scope="prototype">
        <property name="customerDAO" ref="CustomerDAO"/>
    </bean>

    <!-- Customer DAO Declaration -->
    <bean id="CustomerDAO" class="com.edfx.customer.dao.CustomerDAO">
        <property name="sessionFactory" ref="SessionFactory"/>
    </bean>

    <!-- Data Source Declaration -->
    <bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mydatabase"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
        <property name="maxPoolSize" value="10"/>
        <property name="maxStatements" value="0"/>
        <property name="minPoolSize" value="5"/>
    </bean>

    <!-- Session Factory Declaration -->
    <bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="DataSource"/>
        <property name="annotatedClasses">
            <list>
                <value>com.edfx.model.Customer</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <!-- Enable the configuration of transactional behaviour based on annotation -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!-- Transaction Manager is defined -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="SessionFactory"/>
    </bean>

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
            <list>
                <ref bean="runMeJob"/>
            </list>
        </property>

        <property name="triggers">
            <list>
                <ref bean="cronTrigger"/>
            </list>
        </property>
    </bean>

    <!-- Spring Configuration Manager -->
    <bean id="runMeTask" class="com.edfx.customer.schedular.RunMeTask"/>

    <!-- Spring Quartz -->
    <bean name="runMeJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.edfx.customer.schedular.RunMeJob"/>

        <property name="jobDataAsMap">
            <map>
                <entry key="runMeTask" value-ref="runMeTask"/>
            </map>
        </property>
    </bean>

    <!-- Simple Trigger every 5 secs
    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="runMeJob"/>
        <property name="repeatInterval" value="5000"/>
        <property name="stratDelay" value="1000"/>
    </bean> -->


    <!-- CronTrigger runs every 5 secs-->
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="runMeJob"/>
        <property name="misfireInstructionName" value="MISFIRE_INSTRUCTION_FIRE_ONCE_NOW" />
        <property name="cronExpression" value="0 30 19 * * ?"/>
    </bean>

我在这里设置了一个计划表。调度程序工作正常,因为它在特定时间正确触发。

以下是计划运行的作业详细信息:

package com.edfx.customer.schedular;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.naming.directory.Attributes;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.web.context.ContextLoader;
import com.edfx.customer.ldap.mngr.LDAPManager;
import com.edfx.customer.service.CustomerService;
import com.edfx.customer.service.ICustomerLdapSyncService;
import com.edfx.customer.service.ICustomerService;
import com.edfx.managedbean.CustomerManagedBean;
import com.edfx.model.Customer;
public class RunMeJob extends QuartzJobBean{

 private RunMeTask runMeTask;

@Override
protected void executeInternal(JobExecutionContext context)
        throws JobExecutionException {

    ApplicationContext applicationContext = null;
    try {
        applicationContext = (ApplicationContext)context.getScheduler().getContext().get("applicationContext");
    } catch (Exception e) {
        System.err.println("Synchronization Error :: "+e.getMessage());
    }

    if(applicationContext == null)
        applicationContext = ContextLoader.getCurrentWebApplicationContext();
    System.out.println("=============== SCHEDULAR SERVICE ON =================");
    System.out.println("MAP DETAILS == >"+new LDAPManager().getUserDetails());
    Map<String, String> userDetailsMap = new LDAPManager().getUserDetails();

    CustomerManagedBean customerManagedBean = new CustomerManagedBean();
    int i=0;
    for(Map.Entry<String, String> entry : userDetailsMap.entrySet()){
        i++;
        String key = entry.getKey();
        String value = entry.getValue();
        System.out.println("Key -->"+key+" :: Value-->"+value);
        customerManagedBean.setCustomer_id(i);
        customerManagedBean.setName(key);
        customerManagedBean.setAddress(value);
        customerManagedBean.setCreated_date(new Date());
    }
    customerManagedBean.addUser();
    runMeTask.printMe();
    System.out.println("=============== SCHEDULAR SERVICE OFF ================");
}

public void setRunMeTask(RunMeTask runMeTask) {
    this.runMeTask = runMeTask;
}
}

这里,方法addUser()正确调用。但是在执行getCustomerService().addUser(customer);时,它会抛出NullPointerException,因为它会在customerService类中注入CustomerManagedBean对象null。在applicationContext.xml中提到。

可能的原因是什么? 请帮助!

1 个答案:

答案 0 :(得分:2)

我发现的第一个问题是,在executeInternal方法中,您没有在Spring应用程序上下文中使用customerManagedBean。而不是那样,你创建一个像bellow:

CustomerManagedBean customerManagedBean = new CustomerManagedBean();

当你创建自己的bean时,你有责任在使用(注入或初始化)之前准备它,因为你没有在任何框架(Spring或其他东西)制作和管理的任何上下文中使用它。你可以通过简单地从Spring的ApplicationContext中获取bean来避免这些问题,将上面的行替换为下面的行:

CustomerManagedBean customerManagedBean = (CustomerManagedBean)applicationContext.getBean("beanName")

第二件事,

你在使用JSF和Spring吗?

如果您的答案是,那么您使用JSF注释(@ManagedBean@SessionScoped)的原因。因为Spring无法识别这些注释。 Spring实际上并没有在它的上下文中实例化它。虽然你推了,

<context:component-scan base-package="com.edfx.managedbean"/>

applicationContext.xml

如果你的回答是,那么你应该按照以下教程顺利完成配置: