MyEclipse2014快速配置Spring & Spring Testing, Spring AOP简单使用

来源:互联网 发布:淘宝店如何打快递单 编辑:程序博客网 时间:2024/06/10 21:16

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;@Servicepublic class UserServiceImp implements UserService {@Overridepublic void login(String username) {// TODO Auto-generated method stubSystem.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 {@Resourceprivate UserService userService;@Testpublic 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识别@Componentpublic 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,你值得拥有.

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 培训班孩子不学怎么办 小孩不想去学校怎么办 讨厌父母的性格怎么办 老师揪孩子耳朵怎么办 老师整天骂孩子怎么办 教师被学生骂怎么办 幼儿园学生骂老师怎么办 学生骂老师外号怎么办 小孩上课很多嘴怎么办 学生老玩手机怎么办 和校长有了矛盾怎么办 家长打了我怎么办 小朋友不想去幼儿园怎么办 小朋友不想上幼儿园怎么办 高中老师打学生家长怎么办 老师偏心学生该怎么办? 老师能打孩子怎么办 有的幼儿打老师怎么办? 孩子特别怕老师怎么办 孩子跟老师认生怎么办 和搭班老师不合怎么办 学生厌学了老师怎么办 孩子很怕我怎么办 斗米报名之后怎么办 孩子鼻子长疮怎么办 六年级孩子不爱学习怎么办 孩子不要爱学习怎么办? 孩子说不愿意读书怎么办 孩子三天没吃饭怎么办 孩子三天不吃饭怎么办 初中学生不爱学习怎么办 孩子写字头歪怎么办 学习习惯差该怎么办 孩子沉迷网络游戏家长怎么办 二年级贪玩厌学怎么办 孩子太注重外表怎么办 宝宝老爱摔跟头怎么办 小孩很讨厌你怎么办 初二作业没写完怎么办 初中孩子上课困怎么办 老师不搭理孩子怎么办