为了账号安全,请及时绑定邮箱和手机立即绑定
慕课网数字资源数据库体验端
Spring入门篇_学习笔记_慕课网
为了账号安全,请及时绑定邮箱和手机立即绑定

Spring入门篇

moocer JAVA开发工程师
难度中级
时长 7小时 0分
  • AspectJ类型匹配的通配符: *:匹配任何数量字符; ..:匹配任何数量字符的重复,如在类型模式中匹配任何数量子包,而在方法参数模式中匹配任何数量参数. +:匹配指定类型的子类型,仅能作为后缀放在类型模式后边. java.lang.String:匹配String类型 java.*.String:匹配java包下的任何"一级子包"下的String类型,如匹配java.lang.String,但不匹配java.lang.ss.String java..*:匹配java包及任何子包下的任何类型,如匹配java.lang.String、java.lang.annotation.Annotation java.lang.*ing:匹配任何java.lang包下的以ing结尾的类型 java.lang.Number+:匹配java.lang包下的任何Number的自类型,如匹配java.lang.Integer,也匹配java.math.BigInteger
    查看全部
  • 基于Annotation的异常通知 使用@AfterThrowing注解来标注方法,指明该方法是异常通知要执行的方法. @Aspect public class ThrowAdvice{ @AfterThrowing(pointcut="execution(* com.own.IComponent.*(..))",throwing="throwable") public void afterThrowing(Throwable throwable){ System.out.println("有异常抛出"); } } @AfterThrowing注解中有两个属性,pointcut属性为定义切入点.throwing属性表示抛出的异常.被该注解标注的方法中的参数即表示该异常. 配置文件中同样只需要<aop:aspectj-autoproxy>便取代了所有的AOP配置.
    查看全部
  • @·接口 ·用于沟通的中介物的抽象化 ·实体把自己提供给外界的一种抽象化说明,用以由内部操作分离出外部沟通方法,使其能被修改内部而不影响外界其他实体与其交互方式 ·对应java接口即声明,声明了哪些方法是对外公开提供的 ·java8中,接口可以拥有方法体
    查看全部
    0 采集 收起 来源:IOC及Bean容器

    2016-03-11

  • 基于XML Schema的异常通知 不需要实现ThrowsAdvice接口,可以任意使用自定义的方法作为异常通知. public class ThrowAdvice{ public void afterThrowing(Throwable throwable){ System.out.println("有异常抛出"); } } afterThrowing方法是自定义的任意方法,异常通知只有在有异常抛出的时候才能触发. 那么要使IComponent接口声明的方法要throws Throwable抛出异常. 在Component实现类中方法里throw new Exception()手动抛出异常. 修改xml: 只需把<aop:before>标签改成<aop:after-throwing>标签,并且修改调用的方法即可. --
    查看全部
  • 专题--IOC ·接口及面向接口编程 ·什么是IOC ·spring的bean配置 ·bean的初始化 ·spring的常用注入方式
    查看全部
    0 采集 收起 来源:IOC及Bean容器

    2016-03-11

  • 基于Annotation的环绕通知 使用@Around注解来标注环绕通知. @Aspect public class AroundComponentAdvice{ @Around("execution(* com.own.IComponent.*(..))") public Object around(ProceedingJoinPoint joinPoint) throws Throwable{ System.out.println("用户验证"); Object result=joinPoint.proceed(); System.out.println("通知用户已经执行.."); return result; } } @Around注解的方法将会对目标对象实施环绕通知. @Around注解标注的方法就会自动将符合正则表达式中的方法调用环绕通知. 配置文件只需要添加<aop:aspectj-autoproxy></aop:aspectj>就完成了环绕通知的所有工作.
    查看全部
  • 基于XML Schema的环绕通知 不需要实体类实现特定的接口.可以任意定义一个类的一个方法.但是该方法必须有一个ProceedingJoinPoint作为参数. 环绕通知在执行完前置服务后需要使用ProceedingJoinPoint对象的proceed()方法来激活AOP目标对象的相关方法.然后再执行环绕通知中的后置服务. public class AroundComponentAdvice{ public Object around(ProceedingJoinPoint joinpoint) throws Throwable{ System.out.println("用户验证"); Object result=joinpoint.proceed(); System.out.println("通知用户已执行.."); return result; } } 方法要实现环绕通知,则要求必须有一个ProceedJoinPoint类型的参数. 修改xml: <beans..> <aop:config> //定义切入点 <aop:pointcut id="aroundPoint" expression="execution(* com.own.IComponent.*(..))"/> //定义切面,关联实体类. <aop:aspect id="around" ref="aroundAdvice" > //定义环绕通知,关联特定切入点,执行切面中的特定方法 <aop:around pointcut-ref="aroundPoint" method="around" /> </aop:aspect> </aop:config> //切面实体类 <bean id="aroundAdvice" class="com.own.AroundComponentAdvice" /> //连接点 <bean id="component" class="com.own.Component"/> </beans> 只是将<aop:before>标签改成<aop:around>标签,并且调用的方法修改为around()即可.
    查看全部
  • 基于Annotation的后置通知 使用@AfterRetruning注解类来标注后置通知 @Aspect public class AfterComponentAdvice{ @Pointcut("execution(* com.own.IComponent.*(..))") private void afterPointcut(){ } @AfterReturning(pointcut="afterPointcut()",returning="ret") public void after(Object ret){ System.out.println("通知用户已经执行.."); } } @Aspect注解标识该类是一个Aspect. @Pointcut注解可以定义一个pointcut,值为表达式,并在空方法上标注,该方法的名字就是Pointcut的名字,便于切入点的重用. @AfterReturning注解中的pointcut属性用来引用一个切入点(通过pointcut的名字). 这里也可以写成@AfterReturning(pointcut="execution(* com.own.IComponent.*(..))",returning="ret"),不过前面已经为设定了该表达式的切入点的名称为afterPointcut,所以只需对名称进行引用就可以了. returning属性表示返回值,这里定义了目标对象的返回值为ret,这样在该注解标注下的方法里就可以对该返回值进行一定的控制了. 在xml配置文档中只需<aop:aspectj-autoproxy></aop:aspectj>就取代了schema中的一系列配置. 基于Annotation的后置(finally)通知 使用@After注解标注方法,其他与普通after-returning后置通知没有区别 只有@AfterReturning、@AfterThrowing注解中有pointcut属性. @After、@Before、@Around注解都没有pointcut属性.直接在value属性中定义pointcut或者引入通过@Pointcut定义好的pointcut.
    查看全部
  • 基于XML Schema的后置通知 不需要实体类实现AfterReturningAdvice接口,可以使用任意类中的任意方法作为后置通知. public class AfterComponentAdvice{ public void after(){ System.out.println("通知用户已经执行"); } } after()方法也是任意命名的,该方法可以接收JoinPoint作为参数. 修改xml: <beans..> <aop:config> <aop:pointcut id="afterPointcut" expression="execution(* com.own.IComponent.*(..))"></aop:pointcut> <aop:aspect id="after" ref="afterAdvice"> <aop:after-returning pointcut-ref="afterPointcut" method="after"></aop:after> </aop:aspect> </aop:config> <bean id="afterAdvice" class="com.own.AfterComponentAdvice" ></bean> <bean id="component" class="com.own.Component" ></bean> </beans> 基于XML Schema的后置(finally)通知: public class Finally(){ public void finally(){ System.out.println("我都会执行"); } } 配置文件中:只需要把充当切面的实体类换成Finally,并且<aop:after-returning>标签换成<aop:after>标签并把调用的方法改成finally即可.
    查看全部
    0 采集 收起 来源:配置切面aspect

    2018-03-22

  • @·为什么使用框架 ·软件系统日趋复杂 ·重用度高,开发效率和质量提高 ·软件设计人员要专注与对领域的了解,使需求分析更充分 ·易于·上手、快速解决问题
    查看全部
    0 采集 收起 来源:Spring框架

    2016-03-11

  • 基于Annotation的前置通知 使用注解可以简化AOP的实现. 使用@Before注解来标注前置通知. @Aspect public class AdviceBeforeComponent{ @Before("execution(* com.own.IComponent).*(..)") public void bofore(){ System.out.println("用户验证"); } } @Aspect注解表示将该类设置为一个Aspect.可以在任意一个类中使用@Aspect注解来标示. @Before注解表示该方法是一个前置通知,它遵循的规则是execution(* com.own.IComponent.*(..)),也就是IComponent接口中的所有方法,before()方法也是用户任意定义的. 使用@Before注解标注的方法就会在表达式表示的方法执行前执行.这里的before()方法就是前置通知的方法. 接着修改XML配置文件: <beans.. xmlns:aop="http://www.spirngframework.rog/schema/aop" xmlns:schemaLocation=http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0xsd > <aop:aspectj-autoproxy ></aop:aspectj> <bean id="beforeAdvice" class="com.own.AdviceBeforeComponent" ></bean> <bean id="component" class="com.own.Component"></bean> </beans> 使用Annotation方式没有了基于XML方式使用<aop:config>等配置. 只有一个<aop:aspectj-autoproxy></aop:aspectj>标签.表示自动进行代理,并引入使用了@Aspect注解标注的实体类,为了指明该类是充当切面的. 基于Annotation的AOP配置是最简化的,但是它使用了@Aspect注解,这样给系统造成一定的耦合.
    查看全部
    0 采集 收起 来源:配置切面aspect

    2018-03-22

  • <aop:advisor id=" " pointcut-ref=" " advice-ref=" "> <aop:advisor>标签也是用来配置AOP的切面,与Pointcut相同,可以直接使用id来进行引用该切面. id:advisor标签的唯一标识 pointcut-ref:指定切入点 advice-ref:指定该切入点应用的切面 <aop:aspect id="" ref="" >实际上是定义横切逻辑,就是在连接点上做什么. 在其标签下可以定义<aop:before>、<aop:after-returning>、<aop:around>和<aop:after-throwing>、<aop:after>,分别是前置通知、后置通知、环绕通知、异常通知、finally后置通知. 传统的AOP实现中没有finally后置通知,在Spring AOP当中才有. <aop:advisor id="" pointcut-ref="" advice-ref="" ></aop:advisor>则定义了在哪些连接点应用什么<aop:aspect>. Spring这样做的好处就是可以让多个横切逻辑(即<aop:aspect>定义的)多次使用,提供可重用性. Adivisor是一种特殊的Aspect,Advisor代表spring中的Aspect 区别:advisor只持有一个Pointcut和一个advice,而aspect可以多个pointcut和多个advice.
    查看全部
    0 采集 收起 来源:配置切面aspect

    2018-03-22

  • XML配置文件: <beans..> <aop:config> <aop:pointcut id="beforePointcut" expression="execution(* com.own.IComponent.*(..))" /> <aop:aspect id="bofore" ref="beforeAdvice"> <aop:before pointcut-ref="beforePointcut" method="before"/> </aop:aspect> </aop:config> <bean id="beforeAdvice" class="com.own.AdviceBeforeComponent"/> <bean id="component" class="com.own.Component"/> </beans> 在切入表达语言execution(* com.own.IComponent.*(..))中的正则表达式*与后面的表达式需要有一个空格,因为修饰符与方法名之间有一个空格. 切入表达语言中可以使用Aspect提供的通配符与正则表达式. 在测试类中: ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml"); IComponent con=(IComponent)context.getBean("component"); con.getMoney(); 当调用IComponent接口中的方法之前就会执行前置通知. 不需要像传统实现AOP的方式那样,在xml配置文件中引入ProxyFactoryBean再获取代理对象. 直接获取接口的实现类的bean即可.
    查看全部
    0 采集 收起 来源:配置切面aspect

    2018-03-22

  • 基于XMl Schema的前置通知 Spring2.0提供了Schema来通过配置文件解决了前置通知的限定接口,即不用定义一个类实现MethodBeforeAdvice接口. 即: public class AdviceBeforeComponent{ public void before(){ System.out.println(用户验证); } } 其中before()方法也是自定义的,可以任意定义方法名,方法可以用接口JoinPoint作为参数,也可以不用任何参数,接着修改xml配置文件 首先加入schema的命名空间 <beans.. xmlns:aop="http://www.spirngframework.rog/schema/aop" xmlns:schemaLocation=http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0xsd > 接着就可以使用<aop:config>标签来配置一个AOP片段 用<aop:pointcut id="" expression="">标签来定义切入点 id属性:pointcut切入点的唯一标识 expression属性:切入点表达式(定义切入点) <aop:pointcut>相当于一个Bean的配置,只是它只在<aop:config>范围内有效 配置好的pointcut在配置文件中可以使用它的id来进行应用 用<aop:aspect id="" ref="">标签来定义切面 id属性:切面的唯一标识 ref属性:关联充当切面的实体类(如通知) 用<aop:before pointcut-ref="" method="">标签来表示一个前置通知的定义,该标签需要在<aop:aspect>切面标签下使用 pointcut-ref属性:指定切面要切入的位置(引入切入点) method属性:要调用切面中的哪个方法来切入 在<aop:aspect>标签下还可以定义后置通知,环绕通知的标签 <aop:config>风格的配置大量使用了Sprign的自动代理机制
    查看全部
    0 采集 收起 来源:配置切面aspect

    2018-03-22

  • 118
    查看全部
    0 采集 收起 来源:Advice扩展

    2016-03-11

举报

0/150
提交
取消
课程须知
Java的高级课程,适合对Java基础知识应用自如,并熟悉MVC架构的小伙伴们。如果想成为一名Java工程师,这门课程是一定要学哒。
老师告诉你能学到什么?
掌握依赖注入、IOC和AOP的概念,并能进行简单应用。
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!