当前位置: 首页 > wzjs >正文

公司网站开发百度助手下载

公司网站开发,百度助手下载,网络平台运营计划方案,游戏ui素材网站1.实际开发的使用 实际开发中我们会将程序分为三层: ControllerServiceRepository(DAO) 关系 Controller --》Service --〉Repository Component 注解是将标注的类加载到 IoC 容器中,实际开发中可以根据业务需求分别使⽤ Cont…

1.实际开发的使用

实际开发中我们会将程序分为三层:

  • Controller
  • Service
  • Repository(DAO)

关系 Controller --》Service --〉Repository

@Component 注解是将标注的类加载到 IoC 容器中,实际开发中可以根据业务需求分别使⽤

@Controller、@Service、@Repository 注解来标注控制层类、业务层类、持久层类。

Controller
package com.southwind.controller;
import com.southwind.service.MyService;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Setter
@Controller
public class MyController {@Autowiredprivate MyService myService;/*** 模拟客户端请求*/public String service(Double score){return myService.doService(score);}
}
Service
package com.southwind.service;public interface MyService {public String doService(Double score);
}package com.southwind.service.impl;
import com.southwind.repository.MyRepository;
import com.southwind.service.MyService;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Setter
@Service
public class MyServiceImpl implements MyService {@Autowiredprivate MyRepository myRepository;@Overridepublic String doService(Double score) {return myRepository.doRepository(score);}
}
Repository
package com.southwind.repository;
public interface MyRepository {public String doRepository(Double score);
}package com.southwind.repository.impl;
import com.southwind.repository.MyRepository;
import org.springframework.stereotype.Repository;@Repository
public class MyRepositoryImpl implements MyRepository {@Overridepublic String doRepository(Double score) {String result = "";if(score < 60){result = "不及格";}if(score >= 60 && score < 80){result = "合格";}if(score >= 80){result = "优秀";}return result;}
}

spring.xml

<context:component-scan base-package="com.southwind"></context:component-scan>

Spring AOP

  1. AOP(Aspect Oriented Programming)⾯向切⾯编程。
  2. OOP(Object Oriented Programming)⾯向对象编程,⽤对象化的思想来完成程序。
  3. AOP 是对 OOP 的⼀个补充,是在另外⼀个维度上抽象出对象

具体是指程序运⾏时动态地将⾮业务代码切⼊到业务代码中,从⽽实现程序的解耦合,将⾮业务代码抽

象成⼀个对象,对对象编程就是⾯向切⾯编程。

上述形式的代码维护性差,没有代码复⽤性,使⽤ AOP 进⾏优化,如下图所示

1.AOP 的优点:
  • 可以降低模块之间的耦合性
  • 提⾼代码的复⽤性
  • 提⾼代码的维护性
  • 集中管理⾮业务代码,便于维护
  • 业务代码不受⾮业务代码的影响,逻辑更加清晰

1、创建⼀个计算器接⼝ Cal

package com.southwind.aop;
public interface Cal {public int add(int num1,int num2);public int sub(int num1,int num2);public int mul(int num1,int num2);public int div(int num1,int num2);
}

2、创建接⼝的实现类 CalImpl

package com.southwind.aop.impl;
import com.southwind.aop.Cal;
public class CalImpl implements Cal {@Overridepublic int add(int num1, int num2) {int result = num1 + num2;return result;}@Overridepublic int sub(int num1, int num2) {int result = num1 - num2;return result;}@Overridepublic int mul(int num1, int num2) {int result = num1 * num2;return result;}@Overridepublic int div(int num1, int num2) {int result = num1 / num2;return result;}
}

⽇志打印

  • 在每个⽅法开始位置输出参数信息。
  • 在每个⽅法结束位置输出结果信息。

对于计算器来讲,加减乘除就是业务代码,⽇志打印就是⾮业务代码。

AOP 如何实现?使⽤动态代理的⽅式来实现。

代理⾸先应该具备 CalImpl 的所有功能,并在此基础上,扩展出打印⽇志的功能。

1、删除 CalImpl ⽅法中所有打印⽇志的代码,只保留业务代码。

2、创建 MyInvocationHandler 类,实现 InvocationHandler 接⼝,⽣成动态代理类。

动态代理类,需要动态⽣成,需要获取到委托类的接⼝信息,根据这些接⼝信息动态⽣成⼀个代理类,

然后再由 ClassLoader 将动态⽣成的代理类加载到 JVM。

package com.southwind.aop;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;public class MyInvocationHandler implements InvocationHandler {//委托对象private Object object = null;//返回代理对象public Object bind(Object object){this.object = object;return Proxy.newProxyInstance(object.getClass().getClassLoader(),object.getClass().getInterfaces(),this);}@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {//实现业务代码和⾮业务代码的解耦合System.out.println(method.getName()+"⽅法的参数是"+ Arrays.toString(args));Object result = method.invoke(this.object,args);System.out.println(method.getName()+"⽅法的结果是"+ result);return result;
}

test.java

package com.southwind.aop;
import com.southwind.aop.impl.CalImpl;
public class Test {public static void main(String[] args) {//实例化委托对象Cal cal = new CalImpl();//获取代理对象MyInvocationHandler myInvocationHandler = new MyInvocationHandler();Cal proxy = (Cal) myInvocationHandler.bind(cal);proxy.add(10,3);proxy.sub(10,3);proxy.mul(10,3);proxy.div(10,3);}
}

上述代码通过动态代理机制实现了业务代码和⾮业务代码的解耦合,这是 Spring AOP 的底层实现机

制,真正在使⽤ Spring AOP 进⾏开发时,不需要这么复杂,可以⽤更好理解的⽅式来完成开发。

2.Spring AOP 的开发步骤

1、创建切⾯类。
package com.southwind.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
@Aspect
public class LoggerAspect {@Before("execution(public int com.southwind.aop.impl.CalImpl.*(..))")public void before(JoinPoint joinPoint){String name = joinPoint.getSignature().getName();String args = Arrays.toString(joinPoint.getArgs());System.out.println(name+"⽅法的参数是"+args);}@After("execution(public int com.southwind.aop.impl.CalImpl.*(..))")public void after(JoinPoint joinPoint){String name = joinPoint.getSignature().getName();System.out.println(name+"⽅法执⾏完毕");}@AfterReturning(value = "execution(public int com.southwind.aop.impl.CalImpl.*(..))",returning = "result")public void afterReturn(JoinPoint joinPoint,Object result){String name = joinPoint.getSignature().getName();System.out.println(name+"⽅法的结果是"+result);}@AfterThrowing(value = "execution(public int com.southwind.aop.impl.CalImpl.*(..))",throwing = "ex")public void afterThrowing(JoinPoint joinPoint,Exception ex){String name = joinPoint.getSignature().getName();System.out.println(name+"⽅法抛出异常"+ex);}
}
  • @Component,将切⾯类加载到 IoC 容器中。
  • @Aspect,表示该类是⼀个切⾯类。
  • @Before,表示⽅法的执⾏时机是在业务⽅法之前,execution 表达式表示切⼊点是 CalImpl 类中的 add ⽅法。
  • @After,表示⽅法的执⾏时机是在业务⽅法结束之后,execution 表达式表示切⼊点是 CalImpl 类中的 add ⽅法。
  • @AfterReturning,表示⽅法的执⾏时机是在业务⽅法返回结果之后,execution 表达式表示切⼊点是 CalImpl 类中的 add ⽅法,returning 是将业务⽅法的返回值与切⾯类⽅法的形参进⾏绑定。
  • @AfterThrowing,表示⽅法的执⾏时机是在业务⽅法抛出异常之后,execution 表达式表示切⼊点是 CalImpl 类中的 add ⽅法,throwing 是将业务⽅法的异常与切⾯类⽅法的形参进⾏绑定。
2、委托类也需要添加 @Component
package com.southwind.aop.impl;
import com.southwind.aop.Cal;
import org.springframework.stereotype.Component;
@Component
public class CalImpl implements Cal {@Overridepublic int add(int num1, int num2) {int result = num1 + num2;return result;}@Overridepublic int sub(int num1, int num2) {int result = num1 - num2;return result;}@Overridepublic int mul(int num1, int num2) {int result = num1 * num2;return result;}@Overridepublic int div(int num1, int num2) {int result = num1 / num2;return result;}
}
3、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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
"><context:component-scan base-package="com.southwind.aop"></context:component-scan><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>
  • aop:aspectj-autoproxy,Spring IoC 容器会结合切⾯对象和委托对象⾃动⽣成动态代理对象,

AOP 底层就是通过动态代理机制来实现的。

4.AOP 的概念:
  • 切⾯对象:根据切⾯抽象出来的对象,CalImpl 所有⽅法中需要加⼊⽇志的部分,抽象成⼀个切⾯类 LoggerAspect。
  • 通知:切⾯对象具体执⾏的代码,即⾮业务代码,LoggerAspect 对象打印⽇志的代码。
  • ⽬标:被横切的对象,即 CalImpl,将通知加⼊其中。
  • 代理:切⾯对象、通知、⽬标混合之后的结果,即我们使⽤ JDK 动态代理机制创建的对象。
  • 连接点:需要被横切的位置,即通知要插⼊业务代码的具体位置。
http://www.dtcms.com/wzjs/171139.html

相关文章:

  • 制作公司网站多少钱上海企业网站推广
  • 泉州有什么网站是做鞋子批发的今日国际军事新闻最新消息
  • 做微信投票的网站5搜索率最高的关键词
  • 北京建筑公司排名福建seo网站
  • 江苏外协机械加工网站长工具seo综合查询权重
  • 电子商务网站的建设流程微博seo营销
  • 个人工作室网站怎么做b站推广入口2023破解版
  • 微信运营方案快排seo
  • 建网页还是网站好长尾词挖掘
  • 个人网站免费空间申请今日国际新闻头条15条
  • 手机网站建设 小程序google seo 优化招聘
  • 基础建设包括哪些板块seo关键词排名报价
  • 有域名了怎么建设网站深圳网站设计三把火
  • 网站开发的常用流程2020新闻大事件摘抄
  • 郑州做网站建设公司企业网站怎么制作
  • 天津网站建设设计费用广告推广渠道
  • 学做网站论坛 可以吗东莞今天最新消息新闻
  • 修车店怎么做网站百度搜索量排名
  • 做策划常用的网站站长之家查询
  • 用软件做的网站权限管理上海seo顾问
  • 企业网站广告图片轮播代码十大广告公司排名
  • 成品网站源码1688danji6自媒体135的网站是多少
  • 单机游戏网页版windows优化大师怎么彻底删除
  • 海外网站推广方案seo工资待遇怎么样
  • 有哪些做投行网站如何做好网络推广工作
  • 网站建站管理系统关键词排名优化如何
  • 安徽网站设计哪家效果好网站注册查询官网
  • 网站优化seo推广服务青岛seo全网营销
  • 哪些网站论坛做推广好百度统计手机app
  • 北京网站设计服务商宁波百度seo排名优化