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

怎么可以自己做网站建站小程序

怎么可以自己做网站,建站小程序,网站清除黑链,怎么自己做网站吗Java 六大设计原则;Java 23种设计模式(在此介绍三种设计模式) Java设计模式 单例模式 应用场景:spring中bean的作用域用的就是单例模式 //基本的单例模式————懒汉式 public class student {//3.创建static修饰的成员变量p…

Java 六大设计原则;Java 23种设计模式(在此介绍三种设计模式)

Java设计模式

单例模式

应用场景:spring中bean的作用域用的就是单例模式

//基本的单例模式————懒汉式
public class student {//3.创建static修饰的成员变量private static student stu;//1.设计私有构造方法private student() {}//2.提供一个共有的方法public static synchronized student getInstance(){if (stu == null){stu = new student();}return stu;}
}//基本的单例模式————饿汉式public class student {//3.创建static修饰的成员变量private static student stu = new student();//1.设计私有构造方法private student() {}//2.提供一个共有的方法public static synchronized student getInstance(){return stu;}}/*饿汉式与懒汉式的不同是:创建对象的时间点不同*///测试类student stu1 = student.getInstance();student stu2 = student.getInstance();System.out.println(stu1 == stu2);  

工厂模式

//工厂类
public class NoodleFactory {//定义静态常量的标识————规定下面条的类型public static final int NOODLE_BIA = 1;public static final int NOODLE_REGAN = 2;public static final int NOODLE_LANZHOU = 3;//创建不同类型的面条public static INoodles getNoodle(int type) {if (type == 1) {return new BiaBiaMianImp();}else if(type == 2){return new ReGanMianImp();}else if(type == 3){return new LanzhouLaMianImp();}return null;}
}//测试类
public class Test01 {public static void main(String[] args) {//方式1NoodleFactory.getNoodle(2).noodleType();//方式2NoodleFactory.getNoodle(NoodleFactory.NOODLE_REGAN).noodleType();}
}//自定义接口及实现类
public interface INoodles {public void noodleType();
}public class BiaBiaMianImp implements INoodles{@Overridepublic void noodleType() {System.out.println("----来一碗边个边个面---");}
}public class LanzhouLaMianImp implements INoodles{@Overridepublic void noodleType() {System.out.println("-----来一碗兰州拉面----");}
}public class ReGanMianImp implements INoodles{@Overridepublic void noodleType() {System.out.println("----来一碗武汉热干面----");}
}

代理模式

功能:中间隔离;方法增强

静态代理

  • 是由程序员创建或特定工具自动生成源代码,在对其编译。
  • 在程序员运行之前,代理类.class文件就已经被创建了。
//自定义接口
public interface IWomen {public void makeEyeWithMan();
}//被代理对象
public class PanJinLianImp implements IWomen{@Overridepublic void makeEyeWithMan() {System.out.println("给ximenqing抛媚眼");}
}//代理
public class WangPoImp implements IWomen{//被代理对象IWomen obj;//构造方法给属性赋值public WangPoImp(IWomen obj) {this.obj = obj;}@Overridepublic void makeEyeWithMan() {System.out.println("镇壶酒,搞点气氛");obj.makeEyeWithMan();}
}//测试类
public class XiMenTest {public static void main(String[] args) {//1.创建被代理对象IWomen pan = new PanJinLianImp();//2.创建代理IWomen wang = new WangPoImp(pan);wang.makeEyeWithMan();}
}

动态代理

  • 在程序运行时通过反射机制动态创建的。
  • 动态代理分为: 基于接口的动态代理(jdk自带);基于子类的动态代理(第三方)

基于接口的动态代理(jdk自带)

//自定义接口
public interface ISinger {public void sing();public int dance(int num);
}//被代理
public class JinXingImp implements ISinger{@Overridepublic void sing() {System.out.println("----有点甜---");}@Overridepublic int dance(int num) {System.out.println("---哈哈哈哈----");return 0;}
}//jdk动态代理测试
public class Test01 {public static void main(String[] args) {//1.创建被代理对象final ISinger jin = new JinXingImp();//2.创建代理对象final ISinger daiLi = (ISinger) Proxy.newProxyInstance(jin.getClass().getClassLoader(), jin.getClass().getInterfaces(), new InvocationHandler() {@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println("------自我介绍-----");Object obj = method.invoke(jin, args);return obj;}});daiLi.sing();daiLi.dance(6);}
}

基于子类的动态代理(第三方)

//自定义接口
public interface ISinger {public void sing();public int dance(int num);}//实现接口的类
public class TengImp implements ISinger{@Overridepublic void sing() {System.out.println("听了赵雷的成都去了成都,听了汪峰的北京去了北京,至今不敢听腾格尔的天堂");}@Overridepublic int dance(int num) {System.out.println("学武术,跳舞");return 0;}
}//cglib动态代理测试
public class Test02 {public static void main(String[] args) {//1.创建被代理对象final ISinger teng = new TengImp();//2.创建代理对象ISinger dai = (ISinger) Enhancer.create(teng.getClass(), teng.getClass().getInterfaces(), new InvocationHandler() {@Overridepublic Object invoke(Object o, Method method, Object[] objects) throws Throwable {Object obj = method.invoke(teng,objects);return obj;}});dai.sing();dai.dance(6);}
}

用动态代理的优化转账业务的案例见资源

AOP(面向切面编程)

AOP概述

将那些与业务无关,却为业务模块所共同调用的逻辑(例如:事务管理、日志管理、控制权限等)封装抽取成一个可重用的模块,这个模块被命名为"切面(Aspect)",便于减少系统的重复代码,降低模块间的耦合度,并有利于未来的可拓展性和可维护性

 Spring AOP 基于动态代理实现

  • 如果被代理的对象,已经实现某个接口,则 Spring AOP 会使用 JDK Proxy(反射),基于接口的方式,创建代理对象(JDK动态代理的核心是InvocationHandler接口和Proxy类);
  • 如果被代理的对象,没有实现某个接口,就无法使用 JDK Proxy 去进行代理了,这时候 Spring AOP 会使用 Cglib,基于继承的方式,生成一个被代理对象的子类来作为代理(Cglib动态代理的核心是MethodInterceptor接口和Enhancer类)

AOP相关概念

AOP通知类型

AOP将抽取出来的共性功能称为通知;通知类型:以通知在上下文中的具体位置作为划分

前置通知(Before)

返回通知(After-returning)

异常通知(After-throwing)

后置通知(After)

环绕通知(Around)

AOP连接点(Join point)

AOP将所有的方法都视为连接点,不管是接口里面的抽象方法,还是实现类里面的重写方法,都是连接点

AOP切点(Pointcut)

AOP将可能被抽取共性功能的方法称为切入点。切入点是连接点的子集

AOP目标对象(Target)

就是挖掉功能的方法对应的类生的对象,这种对象是无法直接完成最终工作的

AOP织入(Weaving)

是将挖掉的功能回填的动态过程

AOP切面

切点+通知

SpringAOP+AspectJ实现步骤

1.添加依赖,aop与aspectj表达式的依赖

2.创建spring的主配置文件,bean内的命名空间要添加aop的

3.创建业务代码并编写日志记录代码(事务管理代码)

4.将业务层与日志记录层注入spring容器

5.<aop:config>--aop配置 aop:aspect--aop切面 aop:before--通知内容与通知类型

切点表达式配置语法

execution(修饰符 返回值 包名称.类名称.方法名称(参数列表))

eg: execution(public void com.apesource.service.ServiceImp.findAll())

  • 1.修饰符可以省略代表任意 execution(返回值 包名称.类名称.方法名称(参数列表))
  • 2.返回值可以使用“*”代表任意 execution(* 包名称.类名称.方法名称(参数列表))
  • 3.包名可以使用“*”代表任意名称 execution(* *.*.*.类名称.方法名称(参数列表))
  • 4.包名可以使用“..”代表任意个数 execution(* *...类名称.方法名称(参数列表))
  • 5.类名与方法名可以使用“*”代表任意 execution(* *...*.*(参数列表))
  • 6.参数列表可以使用".."代表任意个数任意类型 execution(* *...*.*(..))

注:如果有参数 int======>int; String===>java.lang.String

xml版的面向切面编程案例部分代码展示

//日志工具类
public class Logger {public void brforeLogger(){System.out.println("日志类logger中调用前置Logger方法进行日志记录");}public void returnLogger(){System.out.println("日志类logger中调用返回Logger方法进行日志记录");}public void throwLogger(){System.out.println("日志类logger中调用异常Logger方法进行日志记录");}public void afterLogger(){System.out.println("日志类logger中调用后置Logger方法进行日志记录");}//环绕通知public Object arroundMethod(ProceedingJoinPoint pjp){Object obj=null;try{System.out.println("环绕---------前置通知");//切点方法Object[] args = pjp.getArgs();       //参数obj = pjp.proceed(args);      //调用切点的所有方法System.out.println("环绕---------返回通知");}catch (Throwable e){System.out.println("环绕---------异常通知");}finally {System.out.println("环绕---------后置通知");return obj;}}
}//applicationContext.xml
<!--注入业务层--><bean id="accountServiceImp" class="com.apesource.service.AccountServiceImp"></bean><!--注入日志记录层--><bean id="logger" class="com.apesource.util.Logger"></bean><!--配置AOP--><aop:config><!--配置切面--><aop:aspect id="aopAspect" ref="logger"><!--配置通知类型,并建立通知方法和切入点方法的关联--><!--切点 expression = execution(修饰符{可省略} 返回值 包名称.类名称.方法名称(参数列表))--><aop:pointcut id="dian" expression="execution(* com.apesource.service.*.*(..))"/><!--通知-->
<!--            <aop:before method="brforeLogger" pointcut-ref="dian"></aop:before>-->
<!--            <aop:after-returning method="returnLogger" pointcut-ref="dian"/>-->
<!--            <aop:after-throwing method="throwLogger" pointcut-ref="dian"/>-->
<!--            <aop:after method="afterLogger" pointcut-ref="dian"/>--><aop:around method="arroundMethod" pointcut-ref="dian"/></aop:aspect></aop:config>//测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Test01 {@Autowiredpublic IAccountService service;@Testpublic void show1(){service.update();System.out.println("----------");service.save(1);System.out.println("-----------");service.delete();}
}

注解版的面向切面编程案例部分代码展示

//logger日志
@Component
@Aspect     //切面
public class Logger {@Pointcut("execution(* com.apesource.service.*.*(..))")public void dian(){}@Before("dian()")public void beforeLogger(){System.out.println("日志类logger中调用前置Logger方法进行日志记录");}@AfterReturning("dian()")public void returnLogger(){System.out.println("日志类logger中调用返回Logger方法进行日志记录");}@AfterThrowing("dian()")public void throwLogger(){System.out.println("日志类logger中调用异常Logger方法进行日志记录");}@After("dian()")public void afterLogger(){System.out.println("日志类logger中调用后置Logger方法进行日志记录");}//环绕通知
//    @Around("dian()")public Object arroundMethod(ProceedingJoinPoint pjp){Object obj=null;   //保存主业务犯法的返回值try{System.out.println("环绕---------前置通知");//切点方法Object[] args = pjp.getArgs();       //参数obj = pjp.proceed(args);      //调用切点的所有方法System.out.println("环绕---------返回通知");}catch (Throwable e){System.out.println("环绕---------异常通知");}finally {System.out.println("环绕---------后置通知");return obj;}}
}//applicationContext.xml<!--扫描--><context:component-scan base-package="com.apesource"></context:component-scan><!--开启spring注解的aop动态代理--><aop:aspectj-autoproxy></aop:aspectj-autoproxy>

文章转载自:
http://natterjack.jtrb.cn
http://ruthfulness.jtrb.cn
http://noncontact.jtrb.cn
http://herpesvirus.jtrb.cn
http://surabaja.jtrb.cn
http://luxuriancy.jtrb.cn
http://mapped.jtrb.cn
http://gjetost.jtrb.cn
http://picket.jtrb.cn
http://porno.jtrb.cn
http://countertop.jtrb.cn
http://hindoostani.jtrb.cn
http://barefoot.jtrb.cn
http://exegetically.jtrb.cn
http://cavelike.jtrb.cn
http://dehydrogenize.jtrb.cn
http://chirographer.jtrb.cn
http://pheasant.jtrb.cn
http://hydraemia.jtrb.cn
http://omar.jtrb.cn
http://gadsbodikins.jtrb.cn
http://athlete.jtrb.cn
http://drawn.jtrb.cn
http://poisoner.jtrb.cn
http://aspersory.jtrb.cn
http://velour.jtrb.cn
http://dereliction.jtrb.cn
http://babyism.jtrb.cn
http://nullity.jtrb.cn
http://blellum.jtrb.cn
http://repatriation.jtrb.cn
http://porteress.jtrb.cn
http://somewhat.jtrb.cn
http://mischievously.jtrb.cn
http://ademption.jtrb.cn
http://carter.jtrb.cn
http://superconscious.jtrb.cn
http://gwtw.jtrb.cn
http://brim.jtrb.cn
http://perturbation.jtrb.cn
http://guilder.jtrb.cn
http://patronizing.jtrb.cn
http://nondollar.jtrb.cn
http://crane.jtrb.cn
http://fossa.jtrb.cn
http://gatorade.jtrb.cn
http://tridymite.jtrb.cn
http://haircut.jtrb.cn
http://reagin.jtrb.cn
http://triumvir.jtrb.cn
http://aerocade.jtrb.cn
http://loop.jtrb.cn
http://infralapsarian.jtrb.cn
http://wolverine.jtrb.cn
http://chincapin.jtrb.cn
http://escharotic.jtrb.cn
http://contretemps.jtrb.cn
http://chooser.jtrb.cn
http://archaeology.jtrb.cn
http://prostatectomy.jtrb.cn
http://thermopile.jtrb.cn
http://sayest.jtrb.cn
http://anthracosis.jtrb.cn
http://repurchase.jtrb.cn
http://clop.jtrb.cn
http://arsenicate.jtrb.cn
http://ridgel.jtrb.cn
http://kinematic.jtrb.cn
http://disregardfulness.jtrb.cn
http://boreen.jtrb.cn
http://hegemonic.jtrb.cn
http://invitation.jtrb.cn
http://urinoscopy.jtrb.cn
http://hackberry.jtrb.cn
http://tyg.jtrb.cn
http://nonstative.jtrb.cn
http://arraignment.jtrb.cn
http://emigrator.jtrb.cn
http://schitzy.jtrb.cn
http://oma.jtrb.cn
http://microvessel.jtrb.cn
http://ligation.jtrb.cn
http://deoxygenize.jtrb.cn
http://congestion.jtrb.cn
http://bremsstrahlung.jtrb.cn
http://qei.jtrb.cn
http://alcometer.jtrb.cn
http://antiparasitic.jtrb.cn
http://keratoma.jtrb.cn
http://situp.jtrb.cn
http://colossians.jtrb.cn
http://superencipher.jtrb.cn
http://calyciform.jtrb.cn
http://esteem.jtrb.cn
http://crannied.jtrb.cn
http://antimony.jtrb.cn
http://goddamned.jtrb.cn
http://repetitiousness.jtrb.cn
http://adenectomy.jtrb.cn
http://soldiership.jtrb.cn
http://www.15wanjia.com/news/71428.html

相关文章:

  • 网站整站下载带数据库后台的方法东莞seo优化推广
  • 四川省的建设厅注册中心网站深圳网站提升排名
  • 网站建设算入会计分录华为手机业务最新消息
  • python3的网站开发搜索引擎优化案例分析
  • 深圳市国家高新技术企业认定百度关键词优化怎么做
  • 衡水网站建设选哪家陕西网站建设网络公司
  • 做网站一般多少百度最新版app下载安装
  • 网站开发公司 网站空间衡水seo优化
  • 网站建设中布局海淀seo搜索引擎优化公司
  • 什么网站比较容易做全国seo公司排名
  • 山西建设厅网站2016年3号百度一下生活更好
  • 企业网站开发流程广州推广seo
  • 手机网站制作注意事项免费网站推广
  • 广东网站建设服务公司中国站长网入口
  • 当阳建设中学网站抖音关键词推广怎么做
  • 京东网站网站建设是什么个人建网站的详细步骤
  • 帮企业做网站整合营销沟通
  • 做h5页面的网站百度搜索关键词排名优化推广
  • 论坛网站备案搜狗网址
  • 购买帝国cms做网站代理电商网站seo
  • 中国建设资格注册中心网站网站推广文章
  • 用word做网站首页天津网络关键词排名
  • 做网站小语种翻译多少钱模板免费网站建设
  • 太平洋网站建设搜索seo神器
  • 米拓cms 网站模板在哪公司的seo是什么意思
  • 宿松网站建设百度收录入口提交查询
  • 山东做网站建设的好公司扬州网络优化推广
  • 山东网站方案网站关键词优化怎么弄
  • 黑龙江省网站备案安装百度到桌面
  • 一个做微信文章的网站软考培训机构哪家好一点