第48月第18天 spring整合springmvc

1.

web.xml

  <!--*****************************Spring整合SpringMvc配置 start*****************************-->
  <!--配置Spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--设置spring配置文件的路径-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <!--resource中的文件会打包发布到WEB-INF/classes类目录下,所以前面要加classpath-->
    <param-value>classpath:spring.xml</param-value>
  </context-param>
  <!--*****************************Spring整合SpringMvc配置 end-*****************************-->

spring.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--*****************************spring配置 start*****************************-->
    <!--注解的扫描,希望处理service和dao,controller不需要Spring框架去处理-->
    <context:component-scan base-package="cn.itcast">
        <!--配置哪些注解不扫描-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--*****************************spring配置 end*****************************-->
</beans>
IAccountService.java

package cn.itcast.service;

import cn.itcast.domain.Account;

import java.util.List;

public interface IAccountService {
    List<Account> findAll();

    void saveAccount(Account account);

}

 

AccountServiceImpl.java
package cn.itcast.service.impl;

import cn.itcast.domain.Account;
import cn.itcast.service.IAccountService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("accountService")
public class AccountServiceImpl implements IAccountService {


    @Override
    public List<Account> findAll() {
        System.out.println("執行 findAll");
        return null;
    }

    @Override
    public void saveAccount(Account account) {
        System.out.println(("執行 saveAccount"));
    }
}

 

 

AjaxController

@RestController
public class AjaxController {
    @Autowired
    private IAccountService accountService;


    @RequestMapping("/a2")
    public ResultModel ajax2(){
        System.out.println("AccountController findAll");
        List<Account> accountList = accountService.findAll();

        return ResultModel.success("111");
//        return "111"; //由于@RestController注解,将list转成json格式返回
    }


}

 

posted @ 2020-09-18 16:31  lianhuaren  阅读(146)  评论(0编辑  收藏  举报