springmvc service自動注入 dao自動注入 null


前提新建一個項目,其結構dao、service、controller,controller自動注入service,service自動注入dao,但是dao我為了測試,沒有使用mybatis,當時的想法將service和dao都交給spring管理

代碼如下:

一、Dao

  ①、applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

     <bean id="itemsMapperCustomDao" class="com.xxx.springmvc.dao.impl.ItemsMapperCustomDaoImpl" />
       
</beans>

  ②、Java代碼

public class ItemsMapperCustomDaoImpl implements ItemsMapperCustomDao {

    @Override
    public List<ItemsCustom> findItemsList() throws Exception {
        List<ItemsCustom> itemsList=new ArrayList<ItemsCustom>();
        
        ItemsCustom items_1 = new ItemsCustom();
        items_1.setName("筆記本");
        items_1.setPrice(6000f);
        items_1.setDetail("筆記本電腦!");
        
        ItemsCustom items_2 = new ItemsCustom();
        items_2.setName("手機");
        items_2.setPrice(5000f);
        items_2.setDetail("手機!");
        
        itemsList.add(items_1);
        itemsList.add(items_2);

        return itemsList;
    }
}

二、Service

  ①、applicationContext-service.xml

<bean id="itemsMapperCustomService" class="com.xxx.springmvc.service.impl.ItemsMapperCustomServiceImpl">
</bean>

  ②、Java代碼(通過自動注入[@Autowired]獲取Dao對象,調用其對應方法)

public class ItemsMapperCustomServiceImpl implements ItemsMapperCustomService {

    @Autowired
    private ItemsMapperCustomDao itemsMapperCustomDao;

    // 商品查詢列表
    public List<ItemsCustom> findItemsList() throws Exception {
        return itemsMapperCustomDao.findItemsList();
    }

}
View Code

三、Controller

  ①、springmvc.xml

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

    <!-- 可以掃描controller、service、...
    這里讓掃描controller,指定controller的包
     -->
    <context:component-scan base-package="com.xxx.springmvc.controller"/>
    <!-- 使用 mvc:annotation-driven代替注解映射器和注解適配器配置
    mvc:annotation-driven默認加載很多的參數綁定方法,
    比如json轉換解析器就默認加載了,
     -->
    <mvc:annotation-driven/>

    <!-- 視圖解析器 
    解析jsp解析,默認使用jstl標簽,classpath下的得有jstl的包
    -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>
View Code

  ②、Java代碼(通過自動注入[@Autowired]獲取Service對象,調用其對應方法)

@Controller
public class ItemsController {

    @Autowired
    private ItemsMapperCustomService itemsMapperCustomService;

    // 商品查詢
    @RequestMapping("/queryItems")
    public ModelAndView queryItems(HttpServletRequest request) throws Exception {

        // 調用service查找 數據庫,查詢商品列表
        List<ItemsCustom> itemsList = itemsMapperCustomService.findItemsList();

        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        // 相當 於request的setAttribut,在jsp頁面中通過itemsList取數據
        modelAndView.addObject("itemsList", itemsList);

        // 指定視圖
        // 下邊的路徑,如果在視圖解析器中配置jsp路徑的前綴和jsp路徑的后綴,修改為
        // modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
        // 上邊的路徑配置可以不在程序中指定jsp路徑的前綴和jsp路徑的后綴
        modelAndView.setViewName("items/itemsList");

        return modelAndView;

    }
}
View Code

四、開啟spring(配置監聽器)和springmvc(配置前端控制器),配置web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    version="4.0">
    <display-name>03springmvc</display-name>
    <!-- 1、加載spring容器 
        1.1、整合applicationContext
        1.2、配置監聽器
    -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/classes/applicationContext-*.xml        
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 2、springmvc前端控制器,rest配置 
        2.1、配置DispatcherServlet前端控制器
        2.1、配置servlet映射(訪問方式)
    -->
    <servlet>
        <servlet-name>springmvc_rest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc_rest</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- post亂碼過慮器 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>
View Code

五、問題來了

  debug發現service層在自動注入dao層的對象為null,什么原因不知道

解決方法:

1、修改applicationContext-dao.xml

<context:component-scan base-package="com.xxx.springmvc.dao.impl"/>

2、在dao層類上添加一個@Repository注解

@Repository
public class ItemsMapperCustomDaoImpl implements ItemsMapperCustomDao {

@Controller

用來表示一個web控制層bean,如SpringMvc中的控制器。

@Service

用來表示一個業務層bean。

@Repository

用來表示一個持久層bean,即數據訪問層DAO組件。

@Component

用來表示一個平常的普通組件,當一個類不合適用以上的注解定義時用這個組件修飾。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM