一、javaConfig方式的配置
1、舍弃了原来的spring.xml配置文件,用纯注解的方式实现。
2、简单步骤:
//加这个注解,就是让这个类加载到容器中
@Component
public class User {
private String name;
public String getName() {
return name;
}
@Value("张三")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
//@Configuration: 代表这个类也会被注册到spring容器中
// 相当于原来的spring.xml,代表这是一个配置类
@Configuration
@ComponentScan("com.hr.pojo")
public class Config {
//通过方法注册一个bean,这里的返回值就Bean标签中的class属性,方法名就是bean的id!
@Bean
public User getUser(){
return new User();
}
}
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
User getUser = (User) context.getBean("getUser");
System.out.println(getUser.getName());
}
二、AOP(我们在不改变原来的代码的情况下,实现了对原有功能的增强,这是AOP中最核心的思想)
1、动态代理:AOP的底层机制就是动态代理!
2、动态代理的代理类是动态生成的 ,静态代理的代理类是我们提前写好的。
3、动态代理分为两大类:基于接口的动态代理(JDK动态代理)、基于类的动态代理(cglib);
三、Aop在Spring中的的实现(一:原生的aop接口)
(1)应用:如何在上spring中实现aop
- 横切关注点:比如现在有一个需求,不改变原有代码,让你在所有调用到方法的地方增加一个前置日志、后置日志。(还有缓存、事务、安全)
- 切面:aspect。就是一个类。比如Log;
- 通知:advice。切面要完成的工作。就是类中的一个方法。log.方法
- 目标:target。被通知对象。
- 代理:proxy。向目标对象应用通知后创建的对象。
- 切入点:ponitcut。
- 连接点:jointpoint
(2)案例:
public class AfterAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName() + "返回值" + returnValue);
}
}
public class BeforeLog implements MethodBeforeAdvice {
// 前置通知
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName() + "的" + method.getName() + "33");
}
}
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.yhl.spring.service.UserServiceImpl"/>
<bean id="afterAdvice" class="com.yhl.spring.log.AfterAdvice"/>
<bean id="beforeAdvice" class="com.yhl.spring.log.BeforeLog"/>
<!--配置aop-->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.yhl.spring.service.UserServiceImpl.*(..))"/>
<!--把前置日志和后置日志切入UserServiceImpl类的任一方法-->
<aop:advisor advice-ref="afterAdvice" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="beforeAdvice" pointcut-ref="pointcut"/>
</aop:config>
</beans>
(3)测试:
@Test
void contextLoads() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
// 输出
com.yhl.spring.service.UserServiceImpl : 添加了一个用户
com.yhl.spring.service.UserServiceImpl返回值null
四、Aop实现方式二(自定义类,切面定义)
<!--配置aop2:自定义类实现-->
<bean id="diy" class="com.yhl.spring.log.dIyAspectLog"/>
<aop:config>
<aop:aspect ref="diy">
<aop:pointcut id="point" expression="execution(* com.yhl.spring.service.UserServiceImpl.*(..)))"/>
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
public class dIyAspectLog {
public void before(){
System.out.println("方法执行之前======");
}
public void after(){
System.out.println("方法执行之后======");
}
}
输出
方法执行之前======
2022-02-27 19:07:41.267 INFO 74704 — [ main] com.yhl.spring.service.UserServiceImpl : 添加了一个用户
方法执行之后======
五、Aop实现方式三(使用注解方式实现aop)
<!--配置aop3:使用注解-->
<bean id="annotation" class="com.yhl.spring.log.AnnotationAsprct"/>
<!--开启aop注解: 有jdk的动态代理,有cglib的动态代理,默认为jdk,当参数proxy-target-class="true"用的是cglib-->
<aop:aspectj-autoproxy proxy-target-class="false"/>
@Aspect
public class AnnotationAsprct {
@Before("execution(* com.yhl.spring.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("方法执行之前======");
}
@After("execution(* com.yhl.spring.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("方法执行之后======");
}
// 给定一个参数,代表我们执行要处理切入的点
@Around("execution(* com.yhl.spring.service.UserServiceImpl.*(..))")
public void afterRunning(ProceedingJoinPoint joinPoint){
System.out.println("环绕前======");
Signature signature = joinPoint.getSignature();
System.out.println(signature + "====");
System.out.println("环绕后======");
}
}