MyEclipse2014快速配置Spring & Spring Testing, Spring AOP简单使用
1.新建项目
2.右击项目,如图,利用myeclipse自动导入spring
3.在弹出的对话框中一直next到最后,在最后的页面中勾选Spring
Testing,完成.
4.在src下的applicationContext.xml里,点击namespaces,勾选aop和context
5.在上图的底部分别进入aop和context界面,
5.1在aop界面右击beans,添加<aop:aspectj-autoproxy>,这个的作用是启用aspectj类型的aop功能.
5.2 在context界面右击beans,添加<context:component-scan>,并在右边的elemen details识图中填写base-package和选择annotation-config
base-package的作用是,让spring自动扫描存在注解的包并注册为spring
beans.(我这里的包均以glut开头)
annotation-config的作用大概是启用注解.(doc里面大致的意思......)
6.创建UserService接口,UserServiceImp实现类还有MyTest测试类.
目录结构:
package glut.service;
public interface UserService {
void login(String username);
}
package glut.serviceImp;
import org.springframework.stereotype.Service;
import glut.service.UserService;
@Service
public class UserServiceImp implements UserService {
@Override
public void login(String username) {
// TODO Auto-generated method stub
System.out.println(username + " has login");
}
}
package glut.test;
import javax.annotation.Resource;
import glut.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.<span style="width: auto; height: auto; float: none;" id="2_nwp"><a target=_blank style="text-decoration: none;" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=211ff60146ba710b&k=spring&k0=spring&kdi0=0&luki=4&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=b71ba461f61f21&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D992207%2Ehtml&urlid=0" id="2_nwl"><span style="font-size:12px;color:#0000ff;width:auto;height:auto;float:none;">spring</span></a></span>framework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//启用Spring JUnit
@RunWith(SpringJUnit4ClassRunner.class)
//加载指定的配置文件
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class MyTest {
@Resource
private UserService userService;
@Test
public void test() {
userService.login("leo");
}
}
测试结果:
7.接下来测试AOP功能,新建一个切面类
package glut.aspect;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.<span style="width: auto; height: auto; float: none;" id="1_nwp"><a target=_blank style="text-decoration: none;" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=211ff60146ba710b&k=spring&k0=spring&kdi0=0&luki=4&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=b71ba461f61f21&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D992207%2Ehtml&urlid=0" id="1_nwl"><span style="font-size:12px;color:#0000ff;width:auto;height:auto;float:none;">spring</span></a></span>framework.stereotype.Component;
//声明为切面
@Aspect
//切面也要作为component识别
@Component
public class MyAspect {
//execution(任意类型 glut..任意包.任意类(任意参数)
@Pointcut("execution(* glut..*.*(..))")
public void myPointCut() {
};
//调用poincut指定的方法前执行beforeMethod
@Before("myPointCut()")
public void beforeMethod() {
System.out.println("This is before method");
}
//调用poincut指定的方法后执行afterMethod
@After("myPointCut()")
public void afterMethod() {
System.out.println("This is after method");
}
}
最后再看一次测试结果:
MyEclipse2014,你值得拥有.
MyEclipse2014快速配置Spring & Spring Testing, Spring AOP简单使用的更多相关文章
- Spring框架-IOC和AOP简单总结
参考博客: https://blog.csdn.net/qq_22583741/article/details/79589910 1.Spring框架是什么,为什么,怎么用 1.1 Spring框架是 ...
- Spring源码解析-AOP简单分析
AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等,不需要去修改业务相关的代码. 对于这部分内容,同样采用一个简单的例子和源码来说明. 接口 public ...
- [Spring Unit Testing] Spring Unit Testing with a Java Context
For example, we want to test against a implemataion: package com.example.in28minutes.basic; import o ...
- Spring、Spring Boot和TestNG测试指南 - 使用Spring Boot Testing工具
Github地址 前面一个部分讲解了如何使用Spring Testing工具来测试Spring项目,现在我们讲解如何使用Spring Boot Testing工具来测试Spring Boot项目. 在 ...
- Spring、SpringMVC、Spring Boot、Spring Cloud 概念、关系及区别
注:此文章转载于其他大神 一.正面解读: Spring主要是基于IOC反转Beans管理Bean类,主要依存于SSH框架(Struts+Spring+Hibernate)这个MVC框架,所以定位很明确 ...
- JAVA WEB快速入门之通过一个简单的Spring项目了解Spring的核心(AOP、IOC)
接上篇<JAVA WEB快速入门之从编写一个JSP WEB网站了解JSP WEB网站的基本结构.调试.部署>,通过一个简单的JSP WEB网站了解了JAVA WEB相关的知识,比如:Ser ...
- spring tx:advice 和 aop:config 配置事务
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- eclipse快速配置spring相关xml文件头信息
通过spring tools 插件工具来快速配置xml头信息 ctrl +n 创建-----------> 输入spring 选中spring Beann Configuration File ...
- Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务
Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务,Spring Cloud是一个基于Spring Boot实现的云应用开发工具:Spr ...
随机推荐
- Linux 进程管理 vmstat、top、pstree命令
vmstat命令:监控系统资源 vmstat 是 Linux 中的一个综合性能分析工具,可以用来监控 CPU 使用.进程状态.内存使用.虚拟内存使用.磁盘输入/输出状态等信息.vmstat 命令格式如 ...
- JavaScript消息机制入门篇
JavaScript这个语言本身就是建立在一种消息机制上的,所以它很容易处理异步回调和各种事件.这个概念与普通的编程语言基础是不同的,所以让很多刚接触JavaScript的人摸不着头脑.JavaScr ...
- Oracle 数据库比较日期大小
在今天或者今天之前作比较:select * from JN_BUS_KJLWSBJBXX where dqsj < to_date('2007-09-07 00:00:00','yyyy-mm- ...
- Android 6.0中在/dev下添加新设备驱动下Selinux相关设置【转】
本文转载自:https://blog.csdn.net/fantasy_wxe/article/details/52013922 错误1: 07-23 13:06:57.617 117 117 ...
- springboot学习(三)——使用HttpMessageConverter进行http序列化和反序列化
以下内容,如有问题,烦请指出,谢谢! 对象的序列化/反序列化大家应该都比较熟悉:序列化就是将object转化为可以传输的二进制,反序列化就是将二进制转化为程序内部的对象.序列化/反序列化主要体现在程序 ...
- python数据可视化(持续更新)
1.折线图 import numpy as np import matplotlib.pyplot as plt input_values = [1, 2, 3, 4, 5] s = [1, 4, 9 ...
- codeforces 703B
题意:有n座城市,其中k座是省会城市,每个城市有对应的点权,城市1-2-3-...-n-1有一条路相连,省会城市与其他所有的城市相连,且每两个城市间最多有一条路,每条路的边权为路连接的两座城市的点权乘 ...
- 线程的sleep()方法和yield()方法区别
1.sleep()方法给其他线程运行机会时不考虑线程的优先级,因此会给低优先级的线程以运行的机会 2.yield()方法只会给相同优先级或更高优先级的线程以运行的机会 3.线程执行sleep()方法后 ...
- MSER(Maximally Stable Extremal Regions)算法总结
ER与ER Tree形成过程 判断ER是否位MSER条件 MSER源码分析
- scala学习手记15 - 独立对象和伴生对象
上一节中的单例对象MarkerFactory 就是一个独立对象的例子.尽管它管理着Marker类,但是它并没有关联到任何类上. scala也可以创建关联到类上的对象.这样的对象同类共享同一个名字,这样 ...