1. 概念解讀
控制反轉(zhuǎn):原來通過new構(gòu)造方法創(chuàng)建對象變成交由spring創(chuàng)建對象。
依賴注入:對象的屬性已經(jīng)被spring注入好值,可直接使用。
面向切面編程:其思想是將功能分為 核心業(yè)務(wù)功能和周邊功能 周邊功能又被定義為切面。在開發(fā)過程中,核心業(yè)務(wù)功能和切面功能獨立開發(fā),再‘編織’再一起,這就叫AOP。
2. 聲明bean的方法
xml配置文件中聲明
<context:annotation-config/>//開啟注解方式
<context:component-scan base-package="com.how2java.pojo"/>//掃描注解方式聲明的bean
<bean name="c" class="com.how2java.pojo.Category">
<property name="name" value="category 1" />
</bean>
<bean name="p" class="com.how2java.pojo.Product">
<property name="name" value="product1" />
<!-- <property name="category" ref="c" /> -->
</bean>
注釋方式聲明一個bean
@Component("p")
public class Product {
}
//p就是此bean的名稱 Product就是 class
3. 屬性注入的方式
//這種方式會根據(jù)類型來注入Category
@Autowired
private Category category;
//會根據(jù)名稱來注入 category
@Resource
private Category category;
4. AOP示例
spring boot中添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
核心業(yè)務(wù)代碼
package com.how2java.service;
@Component("s")
public class ProductService {
public void doSomeService(){
System.out.println("doSomeService");
}
}
切面
@Aspect
@Component
public class LoggerAspect {
// * 返回任意類型 (..) 參數(shù)是任意數(shù)量和類型
@Around(value = "execution(* com.how2java.service.ProductService.*(..))")
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("start log:" + joinPoint.getSignature().getName());
//執(zhí)行核心業(yè)務(wù)代碼
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
return object;
}
}
application.xml中添加
<context:component-scan base-package="com.how2java.aspect"/>
<context:component-scan base-package="com.how2java.service"/>
<aop:aspectj-autoproxy/>
詳細點的使用,先定義pointcut
摘自 https://www.cnblogs.com/bigben0123/p/7779357.html
@Aspect
@Component
public class LogAspect {
@Pointcut("execution(public * com.example.controller.*.*(..))")
public void webLog(){}
@Before("webLog()")
public void deBefore(JoinPoint joinPoint) throws Throwable {
// 接收到請求,記錄請求內(nèi)容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 記錄下請求內(nèi)容
System.out.println("URL : " + request.getRequestURL().toString());
System.out.println("HTTP_METHOD : " + request.getMethod());
System.out.println("IP : " + request.getRemoteAddr());
System.out.println("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
System.out.println("ARGS : " + Arrays.toString(joinPoint.getArgs()));
}
@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
// 處理完請求,返回內(nèi)容
System.out.println("方法的返回值 : " + ret);
}
//后置異常通知
@AfterThrowing("webLog()")
public void throwss(JoinPoint jp){
System.out.println("方法異常時執(zhí)行.....");
}
//后置最終通知,final增強,不管是拋出異常或者正常退出都會執(zhí)行
@After("webLog()")
public void after(JoinPoint jp){
System.out.println("方法最后執(zhí)行.....");
}
//環(huán)繞通知,環(huán)繞增強,相當于MethodInterceptor
@Around("webLog()")
public Object arround(ProceedingJoinPoint pjp) {
System.out.println("方法環(huán)繞start.....");
try {
Object o = pjp.proceed();
System.out.println("方法環(huán)繞proceed,結(jié)果是 :" + o);
return o;
} catch (Throwable e) {
e.printStackTrace();
return null;
}
}
}