我在Spring上有抽象工厂模式实现的问题。我正在使用Spring 3 MVC和Hibernate 3,如果我不使用抽象工厂方法模式,它可以正常工作。 我不知道我必须添加到Controller才能访问Factory Class(CategoryFactory)。
Controller或bean启动时是否缺少任何内容?
class SectionsController extends MultiActionController {
/* Do I have to initiate the CategoryFactory here? */
public ModelAndView secList() throws Exception {
CategoryFactory.CategoryType type = CategoryFactory.CategoryType.valueOf("view");
modelMap.addAttribute("sectionList", CategoryFactory.findCategory(type).list(id));
return new ModelAndView("Form", modelMap);
}
}
抽象工厂
public abstract class Category {
public abstract List list(int departId);
}
public class CategoryFactory {
public enum CategoryType { firstclass, secondClass, ... }
public static Category findCategory(CategoryType categoryType) {
// Create type specific Category implementation
}
}
答案 0 :(得分:0)
它应该是: class SectionsController扩展了MultiActionController {
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public ModelAndView secList()抛出异常{ CategoryFactory.CategoryType type = CategoryFactory.CategoryType.valueOf(“view”); modelMap.addAttribute( “sectionList”,CategoryFactory.findCategory(类型).LIST(HibernateTemplate的,ID); 返回新的ModelAndView(“Form”,modelMap); } }