@Autowired自動注入失敗

新手注意的問題

package cn.ryq.web.controller;

import cn.ryq.domain.company.Company;
import cn.ryq.service.company.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/system/company")
public class CompanyController {
@Autowired
public CompanyService companyService;

@RequestMapping("/list.do")
public ModelAndView list(){
List<Company> list = companyService.findAll();
ModelAndView mv= new ModelAndView();
mv.addObject("list",list );
mv.setViewName("company/company-list");
return mv;
}
}
在確保ApplicationContext-service.xml,有掃包(能夠經過右邊的小葉子圖標來查看是否有掃到包)CompanyController控制器中有@Controller,再檢查web.xml的配置,
通常狀況下咱們都是用多個模塊去完成一個項目,那麼web.xml的context-param就以下配置,
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/applicationContext-*.xml</param-value>
</context-param>
還有注意監聽器的配置,不少新手對這個配置不熟練,會忘記配置~
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
加載配置文件的路徑加*,讓web能夠掃到整個項目的配置文件!補充點:發現有些同窗的pojo類中沒有get和set方法,這會致使數據庫沒法獲取到數據,更加沒法自動注入了若是還有更多問題歡迎來訪,相互學習成長,謝謝!
相關文章
相關標籤/搜索