@Value(“张三”)
public String name;//加群1025684353一起吹水聊天
}
基于Java类进行配置
@Configuration //代表这是一个配置类
@Import(MyConfig2.class) //导入合并其他配置类,类似于配置文件中的inculde标签
public class MyConfig {
@Bean //通过方法注册一个bean,这里的返回值就Bean的类型,方法名就是bean的id!
public Dog dog(){
return new Dog();//加群1025684353一起吹水聊天
}
}
==================================================================
AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
| 通知类型 | 连接点 | 实现接口 |
| — | — | — |
| 前置通知 | 在方法执行前执行 | org.spirngframework.aop.MethodBeforeAdvice |
| 后置通知 | 在方法执行后执行 | org.springframework.aop.AfterReturningAdvice |
| 环绕通知 | 在方法执行前后都执行 | org.aopalliance.intercept.MethodInterceptor |
| 异常抛出通知 | 在方法抛出异常后执行 | org.springframework.aop.ThrowsAdvice |
| 引介通知 | 在目标类中添加一些新的方法和属性 | org.springframework.aop.IntroductionInterceptor |
使用Spring实现Aop
导入依赖
org.aspectj
aspectjweaver
1.9.6
第一种实现方式:
1、编写接口
package com.sutdy.service;
public interface UserService {
public void add();
public void delete();
public void update();//加群1025684353一起吹水聊天
public void select();
}
2、编写接口实现类
package com.study.service;
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println(“增加了一个用户”);
}
@Override
public void delete() {
System.out.println(“删除了一个用户”);
}
@Override
public void update() {
System.out.println(“更新了一个用户”);
}//加群1025684353一起吹水聊天
@Override
public void select() {
System.out.println(“查询了一个用户”);
}
}
3、编写增强类
前置增强:
package com.study.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
//方法执行前执行该类
public class BeforeLog implements MethodBeforeAdvice {
//method:要执行的目标方法
//objects:参数
//o:目标对象
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName()+“的”+method.getName()+“被执行了”);
}//加群1025684353一起吹水聊天
}
后置增强:
package com.study.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
//方法执行后执行该类
public class AfterLog implements AfterReturningAdvice {
//returnValue 返回值
//method被调用的方法
//args 被调用的方法的对象的参数
//target 被调用的目标对象
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(“执行了”+method.getName()+“方法,返回结果为:”+returnValue);//加群1025684353一起吹水聊天
}
}
4、编写配置文件:
<?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
http://www.springframework.org/schema/aop/spring-aop.xsd">
aop:config
<aop:pointcut id=“pointcut” expression=“execution(* com.study.service.UserServiceImpl.*(…))”/>
<aop:advisor advice-ref=“beforeLog” pointcut-ref=“pointcut”/>
<aop:advisor advice-ref=“afterLog” pointcut-ref=“pointcut”/>
</aop:config>
aop:aspectj-autoproxy/
5、编写测试代码:
import com.study.service.UserService;
import com.study.service.UserServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(“applicationConText.xml”);
UserService userService = context.getBean(“userService”, UserService.class);
userService.select();//加群1025684353一起吹水聊天
}
}
执行结果:
[
]( )
第二种实现方式
自定义类来实现Aop
1、编写一个切入类
package com.study.diy;
public class AnnotationPointCut {
@Before(“execution(* com.kuang.service..(…))”)
public void before(){
System.out.println(“===方法执行前=”);
}
@After(“execution(* com.kuang.service..(…))”)
public void after(){
System.out.println(“===方法执行后=”);
}//加群1025684353一起吹水聊天
2、编写配置文件
<?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
http://www.springframework.org/schema/aop/spring-aop.xsd">
aop:config
<aop:aspect ref=“diy”>
<aop:pointcut id=“point” expression=“execution(* com.study.service.UserServiceImpl.*(…))”/>
<aop:before method=“before” pointcut-ref=“point”/>
<aop:after method=“after” pointcut-ref=“point”/>
</aop:aspect>
</aop:config>
aop:aspectj-autoproxy/
//加群1025684353一起吹水聊天
3.测试
第三种实现方式
使用注解实现
1、编写一个注解实现的增强类
package com.study.diy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
总结
互联网大厂比较喜欢的人才特点:对技术有热情,强硬的技术基础实力;主动,善于团队协作,善于总结思考。无论是哪家公司,都很重视高并发高可用技术,重视基础,所以千万别小看任何知识。面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。其实我写了这么多,只是我自己的总结,并不一定适用于所有人,相信经过一些面试,大家都会有这些感触。
**另外本人还整理收藏了2021年多家公司面试知识点以及各种技术点整理 **
下面有部分截图希望能对大家有所帮助。
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
总结
互联网大厂比较喜欢的人才特点:对技术有热情,强硬的技术基础实力;主动,善于团队协作,善于总结思考。无论是哪家公司,都很重视高并发高可用技术,重视基础,所以千万别小看任何知识。面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。其实我写了这么多,只是我自己的总结,并不一定适用于所有人,相信经过一些面试,大家都会有这些感触。
**另外本人还整理收藏了2021年多家公司面试知识点以及各种技术点整理 **
下面有部分截图希望能对大家有所帮助。
[外链图片转存中…(img-nD9GYeJc-1714643044411)]