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

已有网站备案iis网站找不到网页

已有网站备案,iis网站找不到网页,石家庄网站建设哪家便宜,三端互通的传奇手游打金Spring使用注解对Bean进行管理 1 使用注解需配置aop相关xsd文件的约束和命名空间 xsd文件名为#xff1a;spring-aop-4.2.xsd 2 注解组件扫描配置 示例如下#xff1a;base-package属性 设置扫描指定包下的所有子孙包 context:component-scan base-packagecn.itma.…Spring使用注解对Bean进行管理 1 使用注解需配置aop相关xsd文件的约束和命名空间 xsd文件名为spring-aop-4.2.xsd 2 注解组件扫描配置 示例如下base-package属性 设置扫描指定包下的所有子孙包 context:component-scan base-packagecn.itma.bean/context:component-scan 3 spring管理bean的常用注解 1作用在类上的 用于创建对象的 Component组件注解 的三个衍生注解(功能一致) * Controller :WEB层 * Service业务层 * Repository持久层 示例如下注解书写package xxx.yyy.test;Component(user)Scope(scopeNameprototype)public class User{}对应的xml配置书写bean nameuser classxxx.yyy.test.User scopeprototype/ 2 用于属性注入的注解 (使用注解注入的方式可以不用提供set方法) * Value用于注入普通类型 * Autowired自动装配(默认按类型进行装配)存在的问题:如果匹配多个类型一致的对象,将无法选择具体注入哪一个对象,需要加一个注解辅助,声明它要注入哪一个Car对象 * Qualifier强制按照名称进行注入。存在的问题:如果存在多个名称一致的类型不一致的对象将无法准确匹配到。 * Resource相当于Autowired和Qualifier一起使用 示例如下注解书写public class User{Value(flower)private String name;public String getName() {return name;}public void setName(String name) {this.name name;}Resource(namecar)private Car c; } 对应的xml配置书写 bean nameuser classxxx.yyy.Userproperty namename valueflower/property namecar refcar/property /bean bean namecar classxx.bean.Carproperty namename valuedd/propertyproperty namecolor valueblack/property /bean 3Spring整合junit测试的 注解 * 导入的包为spring-test-4.2.4.RELEASE.jar * RunWith(SpringJUnit4Cla***unner.class) --帮我们创建容器 * ContextConfiguration(classpath:applicationContext.xml) --指定创建容器使用哪个配置文件 4设置Bean作用范围的注解 Scope* singleton单例* prototype: 多例 4设置Bean生命周期的注解 PostConstruct相当于init-method PreDestroy相当于destroy-method 示例如下注解书写 public class User{PostConstructpublic void init(){syso(这里是初始化方法)}PreDestroypublic void destroy(){syso(这里是销毁方法)}}对应的xml配置书写bean nameuser classxxx.yyy.User init-methodinit destroy-methoddestroy /bean 4 spring管理bean的xml方式与注解方式对比 xml配置 注解配置(1)bean的创建和定义 bean id class/ Component(三个衍生Controller,Service,Repository)(2)bean名称的指定 通过id或name指定 Component(person)(3)bean中参数的注入 通过property或p命名空间 Autowired(按类型注入)或QUalifier(按名称注入)或Resource(两者相加)(4)bean生命周期和 通过设置Scope属性,包括: singleton和prototype Scope(scopeNamesingleton作用范围的设置 总结 xml结构清晰注解开发方便所以实际开发中有一种xml和注解开发(Bean有XML配置但使用的属性用注解注入)。 5 Spring和AOP面向切面编程 1 AOP概述AOP(aspect oriented Programming面向切面编程)AOP是OOP(面向对象编程)的延续也是Spring框架的一个重要内容spring利用AOP可以对业务逻辑各个部分进行隔离降低业务逻辑之间的耦合性提供程序的重用性和开发效率。 2 Spring中AOP的主要功能在不修改源码的基础上对程序进行增强可进行权限校验日志记录性能监控事务控制。3 Spring的AOP底层实现两种代理机制* JDK的动态代理针对实现了接口的类产生代理示例//手动实现动态代理--演示(针对的是代理对象和被代理对象的实现同一接口)public class UserServiceProxyFactory implements InvocationHandler {private UserService us;public UserServiceProxyFactory() {super();}public UserServiceProxyFactory(UserService us) {super();this.us us;}public UserService getUserServiceProxy() {//生成 UserService动态代理对象UserService usProxy(UserService) Proxy.newProxyInstance(UserServiceProxyFactory.class.getClassLoader(),UserServiceImp.class.getInterfaces(),this);//返回return usProxy;}//下面为动态代理对象 对原对象的方法加强Overridepublic Object invoke(Object arg0, Method method, Object[] arg2) throws Throwable {System.out.println(打开事务);Object invoke method.invoke(us,arg2);System.out.println(提交事务);return invoke;}}* Cglib的动态代理针对没有实现的接口的类产生代理应用的是底层字节码增强技术生成当前类的子类对象。示例 //通过第三方实现 代理技术--cglib技术(针对的是代理对象和被代理对象的继承关系)public class UserServiceProxyFactory2 implements MethodInterceptor{private UserService us;public UserServiceProxyFactory2(UserService us) {super();this.us us;}public UserServiceProxyFactory2() {super();}public UserService getUserServiceProxy() {Enhancer ennew Enhancer(); //帮我们生成代理的对象en.setSuperclass(UserServiceImp.class); //设置对谁进行代理en.setCallback(this); //指定代理对象 要增强什么功能(代理要做什么)UserService us (UserService) en.create(); //创建代理对象return us;}Overridepublic Object intercept(Object proxyobj, Method method, Object[] arg, MethodProxy methodProxy) throws Throwable {//打开事务System.out.println(打开事务);//调用原有方法Object returnValue methodProxy.invokeSuper(proxyobj,arg);//提交事务System.out.println(提交事务);return returnValue;}} 4 Spring基于AspectJ的AOP开发(1)Spring的AOP开放相关术语1Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点.2Pointcut(切入点):所谓切入点是指我们要对哪些Joinpoint进行拦截的定义 execution()切入点表达式的基本语法* 基本格式execution( [方法访问修饰符] 方法返回值 全类名.方法名(方法参数))* 配置方法名的不同形式:public void cn.itheima.service.UserServiceImp.save() 简单形式void cn.itheima.service.UserServiceImp.save() public可省略* cn.itheima.service.UserServiceImp.save() *代表任意的返回值类型* cn.itheima.service.UserServiceImp.*() *()代表该目标对象下的所有方法* cn.itheima.service.UserServiceImp.*(..) *(..)对方法参数不作任何要求(空参和实参都行)//下面为开发常用形式* cn.itheima.service.*ServiceImp.*(..) *ServiceImp代表名称包含 ServiceImp的目标对象(是开发的标准形式)* cn.itheima.service..*ServiceImp.*(..) ..*ServiceImp 代表 在包括service下的子孙包中的 名称包含 ServiceImp的目标对象 配置AOP切入点示例:aop:configid属性:为切入点命名aop:pointcut expressionexecution(* cn.ith.service.*ServiceImp.*(..)) idpc//aop:config3Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)有一种特殊的通知Introduction(引介)不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field.通知类型详解前置通知(before)在目标方法执行 之前. 后置通知(afterReturning) 在目标方法执行 之后,如果出现异常则不会调用环绕通知(around) 在目标方法执行 前和后异常抛出通知(afterException)在目标方法执行 出现异常的时候 执行 最终通知(after) 无论目标方法是否出现异常 最终通知都会 执行 .4Aspect(切面): 是切入点和通知引介的结合配置切面!-- 属性ref:要关联的 通知 名称 --!--2.配置通知对象(目标对象的增强版)--bean namemyAdvice classcn.itheima.d_aspect.MyAdvice/beanaop:aspect refmyAdvice!-- method属性:设置方法名 pointcut-ref:设置要关联的 切点名称--!--指定名为before为前置通知--aop:before methodbefore pointcut-refpc/!--指定名为afterReturning为后置通知(出现异常不会调用) --aop:after-returning methodafterReturning pointcut-refpc/ !--指定名为around为环绕通知 --aop:around methodaround pointcut-refpc/ !--指定名为afterException为异常拦截通知 --aop:after-throwing methodafterException pointcut-refpc/ !--指定名为after为后置通知(是否出现异常都会调用) --aop:after methodafter pointcut-refpc/ /aop:aspect5Target(目标对象):代理的目标对象6Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程。spring采用动态代理织入而AspectJ采用编译期织入和类装在期织入.AspectJ是一个面向切面的框架它定义了AOP语法扩展了Java语言。7Proxy代理:一个类被AOP织入增强后就产生一个结果代理类 (2)Spring使用AspectJ进行AOP开发1要导入相关的jar包如下* spring传统aop开发包spring-aop-4.2.4.RELEASE.jarcom.springsource.org.aopalliance-1.0.0.jar* aspectJ开发包com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jarspring-aspects-4.2.4.RELEASE.jar2导入aop约束beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/context xmlns:aophttp://www.springframework.org/schema/aop xsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 3配置xml方式实现AOP的代码示例示例需求对一个UserServiceImpl类 进行AOP增强 其save()方法* UserService类(目标对象)public interface UserService {void save();}public class UserServiceImp implements UserService {Overridepublic void save() {System.out.println(保存);}}* MyAdvice(通知对象)public class MyAdvice {public void before() {System.out.println(前置通知!);}public void afterReturning() {System.out.println(后置通知!(如果出现异常则不会调用));}public Object around(ProceedingJoinPoint pjp) throws Throwable {System.out.println(这是环绕通知之前的部分!);Object proceed pjp.proceed(); //调用目标方法System.out.println(这是环绕通知之后的部分!);return proceed;}public void afterException() {System.out.println(出现异常了!);}public void after() {System.out.println(另一个后置通知(目标方法运行之后调用,无论是否出现异常都会调用));}}* AOP相关配置!--1.配置目标对象 --bean nameuserService classcn.ith.service.UserServiceImp/bean!--2.配置通知对象(目标对象的增强版)--bean namemyAdvice classcn.it_aspect.MyAdvice/bean!--3.配置将通知织入目标对象 --aop:config配置切入点aop:pointcut expressionexecution(* cn.itheima.service.*ServiceImp.*(..)) idpc/配置切面(切点通知)aop:aspect refmyAdviceaop:before methodbefore pointcut-refpc/aop:after-returning methodafterReturning pointcut-refpc/ aop:around methodaround pointcut-refpc/ aop:after-throwing methodafterException pointcut-refpc/ aop:after methodafter pointcut-refpc/ /aop:aspect/aop:config* 测试类代码RunWith(SpringJUnit4Cla***unner.class) ContextConfiguration(classpath:cn/itheima/d_aspect/applicationContext.xml)public class Demo {//将名为 userService的目标对象注入到 us成员变量中Resource(nameuserService)private UserService us;Testpublic void fun1() {us.save();}}* 结果对比未使用AOP时调用UserService的save方法结果为 输出 保存字符串使用了AOP后字符串输出结果为前置通知!这是环绕通知之前的部分!保存另一个后置通知(目标方法运行之后调用,无论是否出现异常都会调用)这是环绕通知之后的部分!后置通知!(如果出现异常则不会调用)4注解方式 实现AOP的代码示例示例需求对一个UserServiceImpl类 进行AOP增强 其save()方法* UserService类(目标对象)public interface UserService {void save();}public class UserServiceImp implements UserService {Overridepublic void save() {System.out.println(保存);}}* MyAdvice(通知对象)//注入切面Aspect public class MyAdvice {//注入切点Pointcut(execution(* cn.itheima.service.*ServiceImp.*(..)))//空参方法名pc作为 该切点的idpublic void pc(){} //注入前置通知,并指定切入点//Before(execution(* cn.itheima.service.*ServiceImp.*(..)))Before(MyAdvice.pc()) //简写public void before() {System.out.println(前置通知!);}//注入后置通知,并指定切入点AfterReturning(execution(* cn.itheima.service.*ServiceImp.*(..)))public void afterReturning() {System.out.println(后置通知!(如果出现异常则不会调用));}//注入环绕通知,并指定切入点Around(execution(* cn.itheima.service.*ServiceImp.*(..)))public Object around(ProceedingJoinPoint pjp) throws Throwable {System.out.println(这是环绕通知之前的部分!);Object proceed pjp.proceed(); //调用目标方法System.out.println(这是环绕通知之后的部分!);return proceed;}//注入异常拦截通知,并指定切入点AfterThrowing(execution(* cn.itheima.service.*ServiceImp.*(..)))public void afterException() {System.out.println(出现异常了!);}//注入后置通知,并指定切入点After(execution(* cn.itheima.service.*ServiceImp.*(..)))public void after() {System.out.println(另一个后置通知(目标方法运行之后调用,无论是否出现异常都会调用));}}* AOP内的配置内容!--1.配置目标对象 --bean nameuserService classcn.itma.service.UserServiceImp/bean!--2.配置通知对象 --bean namemyAdvice classcn.ithnotationAop.MyAdvice/bean!-- 开启使用注解完成织入 --aop:aspectj-autoproxy/aop:aspectj-autoproxy转载于:https://blog.51cto.com/14008076/2314450
http://www.huolong8.cn/news/135233/

相关文章:

  • 网上报建贵州建设局网站网络直播平台
  • 做调研的网站一般有哪些如何做垂直门户网站
  • 网站代理被抓合肥高端网站建设公司哪家好
  • 无锡品牌网站建设中小企业建站可以怎么做
  • 心悦每周免做卡网站深圳企业招聘
  • 南浔建设网站全国企业信息公开系统
  • 蒙阴网站优化企业网站推广案例
  • 网络公司如何开网站怎么制作图片和文字一起
  • 网站开发合同付款方式宜良网站建设
  • 苏州seo网站优化软件网站建设书案例
  • 建设网站的协议虚拟主机服务
  • 网站建设新手教程微信开发商
  • 网站分析实例河北建设厅网站三类人
  • 厂房验收 技术支持 东莞网站建设用什么软件做网站最简单
  • 做公司企业网站标准尺寸超级外链推广
  • 做网站推广如何哪个协会要做网站建设啊
  • 可以做企业网站企业邮箱在哪里注册
  • 如何用网站设计制作东莞专业微网站建设
  • 野外美食网站设计欣赏sun0769东莞阳光网
  • 网站加盟城市分站租网站服务器一个月多少钱
  • 东莞免费网站制作常见的网站推广方式有哪些
  • 网站模块化兰州专业网站建设公司哪家好
  • 网站建设设计 网络服务重庆网站建站推广
  • 免费动态素材网站服务器租用大概多少钱
  • 保定企业自助建站知言 wordpress
  • 网站优化 代码做网站需要做什么页面
  • 钢格板保定网站建设如何免费建立官方网站
  • php开源多用户商城系统郑州seo管理
  • 网站建设div asswordpress 分类合并
  • 新网站的宣传推广中国企业500强门槛