网盘下载地址:

链接:https://pan.baidu.com/s/1Z-em-1ouWyXeMP3JW0IbCg
   提取码:0o4o

1、目录结构:

2、配置文件  applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!--告訴Spring使用注解的形式-->
<!-- <context:annotation-config/>
控制反转目标类
<bean name="c" class="com.how2java.pojo.Category">
<property name="name" value="category 1" />
</bean>
<bean name="product" class="com.how2java.pojo.Product">
<property name="name" value="牙刷"></property>
&lt;!&ndash; <property name="category" ref="c"></property>&ndash;&gt;
</bean>-->
<!--<context:component-scan>: 表示要扫描那个包下面的文件,然后通过注解的形式注入进来-->
<context:component-scan base-package="com.how2java.pojo,com.how2java.service,com.how2java.controller"></context:component-scan> <!--面向切面编程 SpringAOP 开始--> <!--控制反转切面类-->
<bean name="loggerAspect" class="com.how2java.aspect.LoggerAspect"></bean> <!--控制反转切面类-->
<bean name="controllerAspect" class="com.how2java.aspect.controllerAspect"></bean> <aop:config>
<!--<aop:pointcut>:作用: 设置切入点的作用对象和id-->
<aop:pointcut id="loggerCutpoint" expression="execution(* com.how2java.service.ProductService.*(..))"></aop:pointcut>
<aop:pointcut id="curePoint" expression="execution(* com.how2java.controller.TestController.*(..))"></aop:pointcut> <!--<aop:aspect>:作用:设置切面的id-->
<aop:aspect ref="loggerAspect" id="logAspect">
<aop:before method="before" pointcut-ref="loggerCutpoint"></aop:before>
<aop:after method="after" pointcut-ref="loggerCutpoint"></aop:after>
<aop:around method="log" pointcut-ref="loggerCutpoint"></aop:around> </aop:aspect> <aop:aspect ref="controllerAspect" id="conAspect">
<aop:before method="testController" pointcut-ref="curePoint"></aop:before>
</aop:aspect>
</aop:config> <!--面向切面编程 SpringAOP 结束--> </beans>

3、切面类  controllerAspect.java   、LoggerAspect.java

package com.how2java.aspect;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller; /**
* describe:
*
* @author 王立朝
* @date 2019/09/18
*/
public class controllerAspect { public void testController(){
System.out.println("执行controller之前先执行这个方法再说==");
}
}
package com.how2java.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
import sun.misc.Contended; /**
* describe:日志,切面
*
* @author 王立朝
* @date 2019/09/18
*/
public class LoggerAspect { public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("日志开始-" + joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("日志结束-" + joinPoint.getSignature().getName());
return object;
} public void before(){
System.out.println("最先开始---" ); } public void after(){
System.out.println("最后结束的---" );
}
}

4、目标方法类  TestController.java 、 ProductService.java

package com.how2java.controller;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller; /**
* describe:
*
* @author 王立朝
* @date 2019/09/18
*/
@Component("testController")
public class TestController { public void testController(){
System.out.println("控制器");
}
}
package com.how2java.service;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; /**
* describe:业务类
*
* @author 王立朝
* @date 2019/09/18
*/
/*@Component("productService")*/
@Service
public class ProductService { public void doSomeService(){
System.out.println("doSomeService");
} }

5、测试类

package com.how2java.test;

import com.how2java.controller.TestController;
import com.how2java.service.ProductService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* describe:
*
* @author 王立朝
* @date 2019/09/18
*/
public class TestSpringAop { public static void main(String[] args){
// productService ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ProductService productService = (ProductService) context.getBean("productService");
productService.doSomeService();
/* TestController testController = (TestController) context.getBean("testController");
testController.testController();*/
}
}

6、运行结果

注意,在Spring中获取bean 的方式

当只有一个配置文件的时候,可以用下面的方式来获取上下文

ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);

如果有多个配置文件,可以用下面的方式来获取上下文

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml","helloContext.xml"});

最简单的SpringAop 小案例的更多相关文章

  1. 一个简单的Maven小案例

    Maven是一个很好的软件项目管理工具,有了Maven我们不用再费劲的去官网上下载Jar包. Maven的官网地址:http://maven.apache.org/download.cgi 要建立一个 ...

  2. Session小案例-----简单购物车的使用

    Session小案例-----简单购物车的使用 同上篇一样,这里的处理请求和页面显示相同用的都是servlet. 功能实现例如以下: 1,显示站点的全部商品 2.用户点击购买后,可以记住用户选择的商品 ...

  3. SqlDependency缓存数据库表小案例

    SqlDependency的简介: SqlDependency是outputcache网页缓存的一个参数,它的作用是指定缓存失效的数据库依赖项,可以具体到数据库和表. SqlDependency能解决 ...

  4. ch1-vuejs基础入门(hw v-bind v-if v-for v-on v-model 应用组件简介 小案例)

    1 hello world 引入vue.min.js 代码: ----2.0+版本 <div id="test"> {{str}} </div> <s ...

  5. MVC 小案例 -- 信息管理

    前几次更新博客都是每次周日晚上到周一,这次是周一晚上开始写,肯定也是有原因的!那就是我的 Tomact 忽然报错,无法启动,错误信息如下!同时我的 win10 也崩了,重启之后连 WIFI 的标志也不 ...

  6. JSP +MySQL实现网站的登录与注册小案例

    为了练手,我就自己试着做了一个网站的登录与注册的小案例.由于没有做美化处理,所以界面并不是很好看. 网站实现的功能如下: 用户首次注册功能 用户登录功能 项目目录展示: 下面我将会分模块展示 注册模块 ...

  7. Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例

    Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例 继上篇json解析,我用了原生的json解析,但是在有些情况下我们不得不承认,一些优秀的json解析框架确实十分的 ...

  8. 8天入门docker系列 —— 第五天 使用aspnetcore小案例熟悉容器互联和docker-compose一键部署

    这一篇继续完善webnotebook,如果你读过上一篇的内容,你应该知道怎么去挂载webnotebook日志和容器的远程访问,但是这些还远不够,webnotebook 总要和一些数据库打交道吧,比如说 ...

  9. (24/24) webpack小案例--自己动手用webpack构建一个React的开发环境

    通过前面的学习,对webpack有了更深的认识,故此节我们就利用前面相关知识自己动手用webpack构建一个React的开发环境,就算是一个小案例吧. 注:此处使用的开发工具是Webstorm. 1. ...

随机推荐

  1. 如何去把内容分享到whatsapp上?

    使用场景,公司利用whatsapp来推广商品,需要把商品和一些基本信息分享到WhatsApp上; 一:在html的head标签里面通过meta标签加上一些分享的基本网站信息,具体代码如下 <me ...

  2. elementui限制开始日期和结束日期

    项目需求:开始日期和结束日期 禁用当前日期之前的日期.同时结束日期 禁用开始日期之前的日期 <div class='startTime'> 开始时间:<el-date-picker ...

  3. Vue路由规则中定义参数

    Vue使用routerLinke定义参数的时候  路由规则中不需要更改任何属性. 路由其实就是我们在html中定义的锚点,点击这个连接跳转一个锚点.vue中的路由也是这个原理, 前提是路由必须创建在实 ...

  4. <转> Android LayoutInflater详解

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  5. VISION控制器标定及网络分析工具

    VISION 标定和数据采集软件是一个强大的集成工具包,各个工具包可以无缝组合在一起,提供集成的可定制的应用程序,从而能够实现完整的标定和数据分析功能,包括从电子控制单元及外部源收集数据,测量输入和输 ...

  6. golang Methods on structs

    原文:http://golangtutorials.blogspot.com/2011/06/methods-on-structs.html snmp 下载,有空学习一下! https://sourc ...

  7. html学习 菜鸟

    1HTML 不是一种编程语言,而是一种标记语言 2只有 <body>区域才会在浏览器中显示. 3<!DOCTYPE>声明有助于浏览器中正确显示网页.doctype 声明是不区分 ...

  8. Django --- 与数据库进行交互

    目录 1.静态文件配置 1.什么是静态文件 2.为什么用户在浏览器中输入的网址能够访问到响应的资源?有时候不能访问? 3.如果想要访问静态资源怎么做? 4.手动开设静态文件访问资源 5.关于两个sta ...

  9. UWB DWM1000 开源项目框架

    UWB 目前比较火热,不论国内还是国外目前都掀起一股热潮. 但是实际工程代码很少,开源代码更少. 目前代码主要有 1 DecaWave Release的定位源码,代码基于TWR,一个非常大的状态机. ...

  10. easyUI--入门实例

    ui框架 1.需要导入的所有jar包,以及外部的类或文件 1.1导入jar包 1.2导入WebContent外部资源 1.3导入所有需要的辅助类--Util包 2.实例代码 2.1创建TreeNode ...