Dwr + struts2.0 + spring + jpa

1. 重要一点是如果用了Dwr,就不能用Spring的注入了。这点要注意:

public class RatingAction extends AbstractAction{
	
	private double avg_rating ;
	
	private static IRatingService getRatingService() {
		ControllerFactory cf = ControllerFactory.getInstance();
		return (IRatingService) cf.getBean("ratingService");
	}
		
	public void addRatingActor(long userId,double rating,long actorId){
		Related related = new Related();
		related.setType(Related.TYPE_RATING_ACTOR);
		related.setObj1(userId);
		related.setObj2(actorId);
		related.setDegree(rating);
		related.setCreated(new Date());
		related.setModified(new Date());
		getRatingService().addRating(related);
	}

 2.ControllerFactory.java

import cn.ctit.titanmen.util.SpringContextPublisher;

public class ControllerFactory extends SpringContextPublisher{
	
	private static ControllerFactory controllerFactory;
	
	public static ControllerFactory getInstance(){
		if(controllerFactory==null){
			controllerFactory=new ControllerFactory();
		}
		return controllerFactory;
	}
}

 3. SpringContextPublisher.java

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringContextPublisher implements ApplicationContextAware {

	private static ApplicationContext applicationContext;

	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}

	/**
	  * get object
	  * @param name
	  * @return Object : the instance which named name
	  * @throws BeansException
	  */
	  public Object getBean(String name) throws BeansException {
	    return applicationContext.getBean(name);
	  }
}

 

你可能感兴趣的:(spring,DWR,jpa)