文章目录

  • 一,Spring AOP
    • --1,概述
    • --2,使用步骤
    • --3,创建切面
    • --4,测试
      • 创建启动类
      • 创建HelloController 类
      • 测试
  • 二,两个框架的整合
    • --1,需求
    • --2,开发步骤
      • 0,项目结构
      • 1,RunApp启动类
      • 2,application.yml改端口
      • 3,Car类,封装数据
      • 4,CarService接口,定义抽象方法
      • 5,CarServiceImpl实现类,重写抽象方法
      • 6,CarController,接受请求
    • --3,测试
    • --4,总结
  • 三,改造前端代码
    • --1,使用axios语法发起请求
    • --2,修改后端服务器的Controller,加一个特殊的注解,@CrossOrigin
  • 四,Vue路由
    • --1,测试
    • --2,总结

一,Spring AOP

–1,概述

是面向切面编程,扩展了面向对象的不足.
切面Aspect: 其实就是一个类, 要用@Aspect注解
通知Advice: 就是类里的方法, 分为:前置通知 后置通知 环绕通知 返回后通知 异常通知
切点Pointcut: 就是定义了方法的触发时间,切入点表达式

–2,使用步骤

加入jar包

<dependencies><!--添加aop依赖包--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency></dependencies>

–3,创建切面

package cn.tedu.service;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect //切面:通知 + 切点
public class Timer {//2,切点表达式,通过execution属性声明//* cn.tedu.service.*.*(..) 方法返回值 包名.类名.方法名(参数列表)@Pointcut("execution(* cn.tedu.controller.*.*(..))")public void pointcut(){}//1,通知:分类: 前置通知  后置通知  环绕通知  返回后通知  异常通知//前置通知:在调用你的目标方法前,就要执行@Before("pointcut()")public void beforeMethod(JoinPoint joinPoint){System.out.println("我是前置通知~~~~~");//常见的使用场景:权限   缓存  开启事务  日志}//后置通知:在调用你的目标方法后,就要执行@After("pointcut()")public void afterMethod(JoinPoint joinPoint){System.out.println("我是后置通知~~~~~");//常见的使用场景:结束事务  日志  释放资源}//环绕通知:在调用你的目标方法之前和之后,都要执行@Around("pointcut()")public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {long start = System.currentTimeMillis();//计时开始Object o = joinPoint.proceed();//继续执行你的目标方法long end = System.currentTimeMillis();//计时结束System.out.println("aop统计的总耗时是:"+(end-start));return o;//放回给调用者,继续执行方法}
}

–4,测试

创建启动类

package cn.tedu;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class RunApp {public static void main(String[] args) {SpringApplication.run(RunApp.class);}
}

创建HelloController 类

package cn.tedu.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController//Controller + ResponseBody
@RequestMapping("hello")
public class HelloController {@RequestMapping("hello")public String hello(){for (int i = 0; i <100 ; i++) {System.out.println("=");}return "恭喜您,访问成功~";}//统计hello()方法的性能//缺点:目前只能统计hello()方法的性能,想要统计其他方法的性能--AOP//AOP思想好处:让程序员更加关注业务本身,把通用的代码形成切面@RequestMapping("hello2")public String hello2(){long start = System.currentTimeMillis();hello();long end = System.currentTimeMillis();return "访问时间:"+(end-start) ;}}

测试

访问HelloController 里的每个方法,都会执行对应的通知

二,两个框架的整合

–1,需求

获取汽车数据

–2,开发步骤

0,项目结构


1,RunApp启动类

package cn.tedu;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class RunApp {public static void main(String[] args) {SpringApplication.run(RunApp.class);}
}

2,application.yml改端口

server:port: 8090

3,Car类,封装数据

package cn.tedu.pojo;
import org.springframework.stereotype.Component;
//模型层,用来封装数据
@Component//交给spring框架进行ioc
public class Car {private Integer id;private String name;private String color;private Double price;//get set tostring@Overridepublic String toString() {return "Car{" +"id=" + id +", name='" + name + '\'' +", color='" + color + '\'' +", price=" + price +'}';}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;}
}

4,CarService接口,定义抽象方法

package cn.tedu.service;
import cn.tedu.pojo.Car;
import java.util.List;
//接口里都是抽象方法
//jdk1.8也可以有static或者default的普通方法
public interface CarService {//简写形式,public abstractList<Car> get();//获取Car数据}

5,CarServiceImpl实现类,重写抽象方法

package cn.tedu.service;import cn.tedu.pojo.Car;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;import java.util.ArrayList;
import java.util.List;//实现类实现了接口要重写抽象方法,否则就是一个抽象类
@Service//和Component一样,都可以完成ioc,只是更好的表示了是service层的代码
public class CarServiceImpl implements CarService{//重写的要求:方法声明和父类一样,有权限public List<Car> get(){Car c1 = new Car();//空对象,属性都是默认值nullc1.setId(10);c1.setName("bmw");c1.setColor("red");c1.setPrice(9.9);Car c2 = new Car();//空对象,属性都是默认值nullc2.setId(20);c2.setName("audi");c2.setColor("black");c2.setPrice(6.6);List<Car> list = new ArrayList<>();list.add(c1);list.add(c2);return list;//给调用者返回list数据}
}

6,CarController,接受请求

package cn.tedu.controller;
import cn.tedu.pojo.Car;
import cn.tedu.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("car")
public class CarController {//1,调用CarServiceImpl里准备好的汽车数据--DI@Autowiredprivate CarService carService;//2,提供方法,给浏览器返回汽车数据@RequestMapping("get")public List<Car> get(){//调用service的功能,service会把结果返回来return carService.get() ;}
}

–3,测试

–4,总结


三,改造前端代码

–1,使用axios语法发起请求

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>前后端整合起来</title><!-- 1.导入js文件 --><script src="js/vue.js"></script><script src="js/axios.min.js"></script></head><body><!-- 2.准备数据渲染区 --><div id="app"><!-- 按钮的点击事件 --><button @click="get()">点我获取汽车数据</button></div><!-- 3.创建Vue对象 --><script>new Vue({el : "#app" , //挂载点methods : { //定义函数get(){//使用axios技术,访问后端服务器axios.get("http://localhost:8090/car/get").then(//a是服务器的返回值交给a变量保存 箭头函数//data属性用来获取数据, a.data就是获取a的数据a => { console.log(a.data)  })}}})</script></body>
</html>

–2,修改后端服务器的Controller,加一个特殊的注解,@CrossOrigin

package cn.tedu.controller;
import cn.tedu.pojo.Car;
import cn.tedu.service.CarService;
import cn.tedu.service.CarServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("car")
@CrossOrigin//放行js的访问请求
public class CarController {@Autowiredprivate CarService carService;@RequestMapping("get")public List<Car> get(){return carService.get() ;}
}

四,Vue路由

–1,测试

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>测试 vue路由</title><!-- 1.导入js文件,要注意顺序 --><script src="js/vue.js"></script><script src="js/vue-router.js"></script></head><body><!-- 2.准备数据渲染区 --><div id="app"><!-- 4.使用路由 router-link被HTML翻译成a标签,to属性被翻译成href属性--><router-link to="/test1">点我显示主页</router-link><router-link to="/test2">点我显示帮助页</router-link><!-- 5.展示路由匹配到的组件效果 --><router-view></router-view></div><!-- 3.创建Vue对象 --><script>//3.3.定义组件var home = { template : "<h1>我是主页</h1>" }var help = { template : "<h1>我是帮助页</h1>" }//3.2.创建路由实例var luyou = new VueRouter({routes : [ //属性用来定义路由规则//规定哪个路径匹配哪个名字的组件{ path:"/test1" , component:home },{ path:"/test2" , component:help }]}) new Vue({el : "#app", //挂载点router : luyou //3.1.通过router属性配置路由})</script></body>
</html>

–2,总结

cgb2107-day18相关推荐

  1. Python学习路程day18

    Python之路,Day18 - Django适当进阶篇 本节内容 学员管理系统练习 Django ORM操作进阶 用户认证 Django练习小项目:学员管理系统设计开发 带着项目需求学习是最有趣和效 ...

  2. day18 17.c3p0连接池使用

    连接池时间长不用空闲着,dbcp是不回收的,性能可能有些问题.c3p0是可以自动回收.实际开发中c3p的生产力比dbcp强,性能上更强. package cn.itcast.datasource;im ...

  3. Day18 (二)反射

    反射机制是什么 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为java ...

  4. python合法关键字是_python练习题-day18

    1.匹配一行文字中的所有开头的字母内容 import re s="i love you not because of who you are, but because of who i am ...

  5. 初学python之路-day18

    time时间模块 时间戳(timestamp):time.time() 延迟线程的运行:time.sleep(secs) (指定时间戳下的)当前时区时间:time.localtime([secs]) ...

  6. Java基础day18

    Java基础day18 Java基础day18-字节缓冲流&字符流 1.字节缓冲流 1.1字节缓冲流构造方法 1.2字节流复制视频 2.字符流 2.1为什么会出现字符流 2.2编码表 2.3字 ...

  7. LeetCode算法入门- Multiply Strings -day18

    LeetCode算法入门- Multiply Strings -day18 题目介绍 Given two non-negative integers num1 and num2 represented ...

  8. QT每日一练day18:文件IO

    一.读文件 day18.pro SOURCES += \main.cpp main.cpp #include<QFile> #include<QDebug> int main( ...

  9. 句句真研—每日长难句打卡Day18

    句句真研-每日长难句打卡Day18 参考译文:但研究者认为,如果外部董事在坏消息爆发前就已离开公司,那么他们会更容易避免声誉受损,即便历史记录显示'"错误行为形成时,董事们尚在其职" ...

  10. Python菜鸟入门:day18编程学习

    写在前面: 此博客仅用于记录个人学习进度,学识浅薄,若有错误观点欢迎评论区指出.欢迎各位前来交流.(部分材料来源网络,若有侵权,立即删除) 传送门: day01基础知识 day02知识分类 day03 ...

最新文章

  1. Kanzi常用操作3
  2. 【helloworld】-微信小程序开发教程-入门篇【1】
  3. SQLHELPER C#
  4. 从未知到2019年的文章汇总
  5. OpenCV 编译 - Unable to locate package libjasper-dev
  6. Flex组件的行为和动画效果(实例)
  7. 【英语学习】【English L06】U07 Jobs L4 What do you think of our service?
  8. mysql慢查询分析工具和分析方法
  9. CMOS模拟电路设计经典书籍介绍 ---看完这些模电书,那离大佬就不远了
  10. 企业版IDP的申请及“In House”发布
  11. 树莓派hwclock命令参数及用法详解--linux显示/设置硬件时钟命令
  12. 上有尧舜之君,下有尧舜之民
  13. 文悦古体仿宋字体官方版
  14. JAVA 图片地址路径转换 Base64 工具类
  15. Intellij IDEA——启动Tomcat控制台输出繁体乱码
  16. 学校信息管理系统数据库模型设计
  17. asp.net笔试题
  18. html判断app是否安装类似淘宝,H5浏览器如何检查手机中是否安装某个APP并打开
  19. 区块链赋能的6G零信任车联网可信接入方案
  20. 趣味编程Python之折纸去月球!!!

热门文章

  1. form的submit()方法不能触发onsubmit事件的解决方法,兼容各版本浏览器。
  2. FPGA的学习:5分频的实现
  3. 【CTR】《Towards Universal Sequence Representation Learning for Recommender Systems》 (KDD‘22)
  4. 有声读物 - 适合孩子
  5. 你知道是谁发明了棒球?
  6. MATLAB R2023 for Mac v9.14.0 安装教程 数学分析软件
  7. DEMUX(解扰解复用)
  8. Directx3D编程框架
  9. 使用tushare下载指定股票日线数据并存为excel文件
  10. is not translated in zh (Chinese) / is not translated in en (English) [MissingTranslat