2021-05-24

Spring5框架

AOP
什么是AOP?
AOP是一种编程思想他是OOP的一种深化,他叫做面向切面编程或者横面,他主要是可以通过不改变源代码来实现功能的加强。
AOP的优势?
AOP能通过横面达到深化代码的功能,达到解耦的效果将各个业务隔离,提高了代码的复用性。
AOP的两种实现方式:
JDK动态代理(有接口的情况)
基于CGLIB(没有接口的情况)

基于JDK的动态代理

原理:使用Proxy里面的方法创建代理对象
在这里插入图片描述

public class JDKProxy {
 public static void main(String[] args) {
 //创建接口实现类代理对象
 Class[] interfaces = {UserDao.class};
// Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, 
new InvocationHandler() {
// @Override
// public Object invoke(Object proxy, Method method, Object[] args) 
throws Throwable {
// return null;
// }
// });
 UserDaoImpl userDao = new UserDaoImpl();
 UserDao dao = 
(UserDao)Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, 
new UserDaoProxy(userDao));
 int result = dao.add(1, 2);
 System.out.println("result:"+result);
 } }
//创建代理对象代码
class UserDaoProxy implements InvocationHandler {
 //1 把创建的是谁的代理对象,把谁传递过来
 //有参数构造传递
 private Object obj;
 public UserDaoProxy(Object obj) {
 this.obj = obj;
 }
 //增强的逻辑
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws 
Throwable {
 //方法之前
 System.out.println("方法之前执行...."+method.getName()+" :传递的参
数..."+ Arrays.toString(args));
 //被增强的方法执行
 Object res = method.invoke(obj, args);
 //方法之后
 System.out.println("方法之后执行...."+obj);
 return res;
 } }

AOP的术语

  1. 连接点:类里面有哪些方法可以被增强
  2. 切入点:在实际当中被增强的方法
  3. 通知(增强):实际增强的逻辑部分称为通知(增强)
    通知有多种类型:前置通知,后置通知,环绕通知,异常通知,最终通知
    4.切面:是一个动作,把通知应用到切入点的过程

AOP操作

基于AspectJ实现AOP操作
AspectJ不是Spring当中的他是一款AOP框架

实现的方式?
基于AspectJ实现AOP

  1. 基于xml文件实现
  2. 基于注解实现
    切入点表达式:“execution*(* 方法的位置(…))”

基于注解的纯Java的实现:
被增强的方法

@Component
public class cat {
    public void eat(){
        System.out.println("猫要吃鱼了");
    }
}

增强类

@Aspect
@Component
@Order(3)
public class catProxy {
    @Pointcut("execution(* com.wei.cat.cat.eat(..))")
    public void traffic(){

    }

    @Before("traffic()")
    public void before(){
        System.out.println("小猫咪事先说好你只能吃两个小鱼干");
    }
    @After("traffic()")
    public void after(){
        System.out.println("检查一下吃了多少小鱼干");
    }
    @AfterReturning("traffic()")
    public void afterReturning(){
        System.out.println("你吃了这么多啊!");
    }
    @AfterThrowing("traffic()")
    public void throwing(){
        System.out.println("这个小鱼干的数量不对");
    }


    @Around("traffic()")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("吃鱼前的环绕");
        proceedingJoinPoint.proceed();
        System.out.println("吃鱼后的环绕");
    }
}
@Configuration//声明当前类是一个配置类,相当于一个Spring的XML配置文件,与@Bean配合使用。
@ComponentScan(basePackages = {"com.wei.cat"})//替代xml当中的扫描路径//配置xml当中的扫描包
@EnableAspectJAutoProxy(proxyTargetClass =true)//自动生成代理对象
public class aop { //
}
   @Test
    public void test5() {
        //利用反射得到该类当中的任意方法
        ApplicationContext user = new AnnotationConfigApplicationContext(AOPAnnotation.class);
        doctor bean = user.getBean("doctor", doctor.class);
        bean.doctor();
    }

基于配置文件实现

  1. 配置对应的扫描路径
  2. 配置对应的自动生成代理类对象
  3. 注册bean
<aop:config>
        <aop:pointcut id="qq" expression="execution(* com.wei.aopxml.stu.stu(..))"/>
<!--        配置切面 把通知应用到切入点 用id关联-->
        <aop:aspect ref="stuProxy">
            <aop:before method="before" pointcut-ref="qq"/>
            <aop:after method="after" pointcut-ref="qq"/>
         </aop:aspect>
    </aop:config>

IOC

什么是IOC?

  • 控制反转,将对象的创建与调用交给spring管理

IOC的作用?
解耦,创建对象不再是new,在配置文件与注解当中实现值的注入,不用修改源代码。

IOC的底层原理:
反射,控制反转,xml

在这里插入图片描述
什么是Bean管理?

  • spring值的注入
  • spring对象的创建
set方法注入
public class cat {
    private String name;
    private String address;
    private int id;

    public void setName(String name) {
        this.name = name;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "cat{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", id=" + id +
                '}';
    }

xml文件

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="cat" class="com.wei.dao.cat">
    <property name="name" value="蔡徐坤"/>
    <property name="address" value="邵阳"/>
    <property name="id" value="48"/>
</bean>
</beans>

测试类

 @Test
    public void cat(){
        ApplicationContext context = new ClassPathXmlApplicationContext("cat.xml");
        System.out.println("这是第四步,使用bean");
        cat myTest = context.getBean("cat", cat.class);
        System.out.println(myTest.toString());
    }

使用有参方法注入

  private String name;
    private int id;

    public dog(String name, int id) {
        this.name = name;
        this.id = id;
    }

    @Override
    public String toString() {
        return "dog{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }

    public dog() {//有参会覆盖无参所以需要加上对应的无参构造

    }

p标签的引入


```xml
<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="dog" class="com.wei.dao.dog" p:id="1" p:name="旺财"/>

</beans>

使用特殊字符<南京> <![CDATA[<南京>]]> <![CDATA[<>]]]>

内部Bean的注入

一个部门有多个员工

//部门类  一个部门有多个人 那么部门对应多个人 所以当相同的部门就会有多个人 所以部门从person处吸引人过来
public class dept(){
	private String name;
	public void setName(String name){
		this.name=name;
}
//员工类 一个部门对应多个员工
public class person {
    private String name;
    private String id;
    private dept dept;

    public void setName(String name) {
        this.name = name;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setDept(com.wei.dao.dept dept) {
        this.dept = dept;
    }

    @Override
    public String toString() {
        return "person{" +
                "name='" + name + '\'' +
                ", id='" + id + '\'' +
                ", dept=" + dept +
                '}';
    }
}


xml文件

<bean id="person" class="com.wei.dao.person">
    <property name="name" value="王芬"/>
    <property name="id" value="2"/>
    <property name="dept">
        <bean id="dept" class="com.wei.dao.dept">
            <property name="name" value="秘书部"/>
        </bean>
    </property>
</bean>

测试类

 @Test
    public void demo(){
        ApplicationContext context = new ClassPathXmlApplicationContext("company.xml");
        System.out.println("这是第四步,使用bean");
        person myTest = context.getBean("person", person.class);
        System.out.println(myTest.toString());
    }

scope的作用域 工具类的提取(有这个Until)
singleton 和 prototype 区别 bean当中的属性
第一 singleton 单实例,prototype 多实例
第二 设置 scope 值是 singleton 时候,加载 spring 配置文件时候就会创建单实例对象
设置 scope 值是 prototype 时候,不是在加载 spring 配置文件时候创建 对象,在调用
getBean 方法时候创建多实例对象

关于Bean的生命周期:
生命周期:从出身到消亡的过程
(1)通过构造器创建 bean 实例(无参数构造)
(2)为 bean 的属性设置值和对其他 bean 引用(调用 set 方法)
(3)调用 bean 的初始化的方法(需要进行配置初始化的方法)
(4)bean 可以使用了(对象获取到了)
(5)当容器关闭时候,调用 bean 的销毁的方法(需要进行配置销毁的方法)

当加入后置处理器时,为七步:
(1)通过构造器创建 bean 实例(无参数构造)
(2)为 bean 的属性设置值和对其他 bean 引用(调用 set 方法)
(3)把 bean 实例传递 bean 后置处理器的方法 postProcessBeforeInitialization (4)调用 bean 的初始化的方法(需要进行配置初始化的方法)
(5)把 bean 实例传递 bean 后置处理器的方法 postProcessAfterInitialization
(6)bean 可以使用了(对象获取到了)
(7)当容器关闭时候,调用 bean 的销毁的方法(需要进行配置销毁的方法)

完全基于注解:

@Repository
@Order(value = 1)
public class userDao {
    @Value(value = "abc")
    private String name;

    public void add() {
        System.out.println("userDao add......."+name);
    }
}

@Service
@Order(0)
public class userService {
@Autowired
private userDao userDao;
    @Value(value = "qqq")
    private String id;
    public void add() {
        System.out.println("service add......."+id);
        userDao.add();
    }

    public void setUserDao(com.wei.Demo.userDao userDao) {
        this.userDao = userDao;
    }

}

//约束类
@Configuration
@ComponentScan(basePackages = {"com.wei.Demo"})

public class AOPConfig {
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值