在AbstractJExcelView中无法使用自动服务

时间:2014-06-15 11:54:30

标签: java spring-mvc

我尝试在类中扩展AbstractJExcelView但始终为null的Autowired My Service。 我想我可以解决这个问题,将Anpendency Injection with Annotation更改为xml配置并注入手动组件。

控制器

@Controller
@SessionAttributes("user")
public class UserController {
 @RequestMapping(value="/exportExel", method = RequestMethod.GET)
            public ModelAndView getExelView(@ModelAttribute User user){

    return new ModelAndView("ExelUserView","UserList",
            user);
   }
 }

服务

@Service
public class UserServiceImp implements UserService {

@Override
public String getAllFood(User user) {     //I Want Get All Element from model User 
                                         who contains Arrays String
    String backValue = "";              
    for(String s : user.getFavFood()){
        backValue +=s;
        backValue +=",";
    }

    return backValue;
}

}

和ExelView

public class ExelView extends AbstractJExcelView {

private UserServiceImp userService = new UserServiceImp();    // I solve my problem that 
                                                              //but in my controller i use Autowired Interface Service
                                                              // so i dont think its good solution

@Override
protected void buildExcelDocument(Map<String, Object> model,
        WritableWorkbook workbook, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    WritableSheet sheet = workbook.createSheet("User Response", 0);
    setExelHead(sheet);

    User listUser = (User)model.get("UserList");
    setExelRows(sheet, listUser);

}



public void setExelRows(WritableSheet sheet,User listUser) throws RowsExceededException, WriteException{

    sheet.addCell(new Label(4, 1, userService.getAllFood(listUser))); 
}
}

和用户模型

public class User implements Serializable{

private static final long serialVersionUID = 1L;

private String[] favFood;

public String[] getFavFood() {  //I want View Arrays in Exel in one Cell
    return favFood;
}
public void setFavFood(String[] favFood) {
    this.favFood = favFood;
}
}   

在xml中我使用

<context:component-scan base-package="com.dinor913.example" />  // I guess delete auto    scan component and inject Manual all Controller and Service and this should work then

<bean class="org.springframework.web.servlet.view.XmlViewResolver">
    <property name="location">
         <value>
             /WEB-INF/xml-views/document-views.xml
         </value>    
    </property>
    <property name="order" value="0"/>
</bean>

和document-views.xml

<bean name="ExelUserView"
  class="com.dinor913.example.businnes.ExelView" >
</bean>

更新解决方案!

我添加到document-views.xml

<context:annotation-config base-package="com.dinor913.example" />

所以我理解当我创建XmlViewResolver时,我还为XmlViewResolver和context创建了新的ApplicationContext:annotation-config将@Autowired UserService添加到DispatcherServlet。

Thx求助。

我几天前开始学习春天

对不起我可怕的英语我希望你明白我的意思.....

1 个答案:

答案 0 :(得分:0)

private UserServiceImp userService = new UserServiceImp();更改为:

@Autowired
private UserServiceImp userService

这应该有效。如果没有发布errormessage和stacktrace。