代码例子
Controller
package com.springboot.chapter4.aspect.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.springboot.chapter4.aspect.service.UserService;
import com.springboot.chapter4.pojo.User;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
@Qualifier("userServiceimpl")//避免歧义
private UserService userService = null;
@RequestMapping("/print")
@ResponseBody
public User printUser(Integer id) {
System.out.println("User");
User user = new User();
user.setId(id);
user.setUsername("wang.li");
user.setNote("AAA");
userService.printUser(user);
return user;
}
}
UserService
package com.springboot.chapter4.aspect.service;
import com.springboot.chapter4.pojo.User;
public interface UserService {
public void printUser(User user);
}
UserServiceimpl
package com.springboot.chapter4.aspect.service.impl;
import org.springframework.stereotype.Service;
import com.springboot.chapter4.aspect.service.UserService;
import com.springboot.chapter4.pojo.User;
/**
* @Service 是标记实现类上的。
* 因为@Service是把spring容器中的bean进行实例化,也就是等同于new操作,
* 只有实现类是可以进行new实例化的,而接口则不能,所以是加在实现类上的。不信你标注到接口上,会报BeanCreationException的。
* @author XXX
*
*/
@Service
public class UserServiceimpl implements UserService{
@Override
public void printUser(User user) {
// TODO Auto-generated method stub
if(user == null) {
throw new RuntimeException("检查用户参数是否为空. .. ...");
}
System.out.println("id = "+user.getId());
System.out.println("username = "+user.getUsername());
System.out.println("note = "+user.getNote());
}
}
AnimalServiceimpl
package com.springboot.chapter4.aspect.service.impl;
import org.springframework.stereotype.Service;
import com.springboot.chapter4.aspect.service.UserService;
import com.springboot.chapter4.pojo.User;
@Service
public class AnimalServiceimpl implements UserService{
@Override
public void printUser(User user) {
// TODO Auto-generated method stub
System.out.println("Animal");
}
}
User
package com.springboot.chapter4.pojo;
public class User {
private Integer id;
private String username;
private String note;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
aspect
package com.springboot.chapter4.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import com.springboot.chapter4.pojo.User;
/**
* 使用@Aspect注解将一个java类定义为切面类
*/
@Aspect
public class MyAspect {
/**
* execution:匹配连接点
* *: 任意返回类型
* com.springboot.chapter4.aspect.service.impl.UserServiceimpl.printUser:目标对象权限定名
* printUser:目标对象方法 (..):任意参数进行匹配
*
*/
@Pointcut("execution(* com.springboot.chapter4.aspect.service.impl.UserServiceimpl.printUser(..))")
public void pointCut() {
}
@Before("pointCut()")//前置通知
public void befor() {
System.out.println("befor--------------------");
}
@After("pointCut()")//后置通知
public void After() {
System.out.println("after--------------------");
}
@AfterReturning("pointCut()")//后置返回通知
public void AfterReturning(JoinPoint point) { //JointPoint.getArgs()可以获取执行目标方法时传入的参数
Object[] args = point.getArgs();
User d = (User) args[0];
System.out.println("AfterReturning--------------------");
System.out.println(d.getId());
}
/*
@Around("pointCut()")//环绕通知
public void Around(ProceedingJoinPoint JoinPoint) throws Throwable {
System.out.println("Start-Around--------------------");
JoinPoint.proceed();
System.out.println("End-Around--------------------");
}
*/
}
Application
package com.springboot.chapter4;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import com.springboot.chapter4.aspect.MyAspect;
/**
* http://localhost:8080/user/print?id=1
* @author XXX
*
*/
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class Application {
@Bean(name="myaspect")
public MyAspect initMyaspect() {
return new MyAspect();
}
public static void main(String args[]) {
SpringApplication.run(Application.class, args);
}
}