栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

浅入Spring

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

浅入Spring

Spring 特征:
IOC:控制反转

不需要我们创建对象,对象的创建交给Spring管理
AOP:面向切面

IOC

Spring实例:
单例:Spring构建的时候创建
多例:用到的时候创建

FactoryBean:
包含三个方法:

  1. 创建实例
  2. 获取实例类型
  3. 是否是单例(无论是否是单例,对象都创建都是使用的时候才会用到)

possprocessor bean增强

Spring 扫描配置

排查
< context:exclude-filter type=“” expression=“路径”>

包含
< context:include-filter type=“” expression=“路径”>

type:

  • assignable:可以指定类的名称,但必须是全路径
  • annotation:可以指定注解,但必须是全路径
  • regex:正则表达式
  • aspect:使用切面的方式
  • custom:使用自定义的方式

@Autowired 注入规则:

  • 默认根据type装配,找到直接装配,找不到报错
  • 如果有多个(子类继承父类)的情况,默认根据类型名称小写查找,有则装配,无则报错
  • 如果想根据名字指定装配,使用注解:@qualifier(“名称”)

@Resource 和 @Autowired 的区别:

  • resource是JDK提供的,Autowired是Spring提供的
  • resource 是根据名称装配的,Autowired 是根据类型装配的
  • resource 先根据名称装配,再根据类型装配
  • Autowired 先根据类型,在根据名称装配
AOP 动态代理:
public interface Calculator {



    Integer add(Integer i,Integer j);

    Integer sub(Integer i,Integer j);
}


public class MyCalculator implements Calculator{


    @Override
    public Integer add(Integer i, Integer j) {
        return i+j;
    }

    @Override
    public Integer sub(Integer i, Integer j) {
        return i-j;
    }
}


@Service
public class MyCalculatorProxy {


    public Calculator proxyMethod(Calculator calculator) {

        //类加载器
        ClassLoader loader = calculator.getClass().getClassLoader();
        //类接口集合
        Class[] interfaces = calculator.getClass().getInterfaces();
        //类处理器
        InvocationHandler invocationHandler = (proxy, method, args) -> {

            System.out.println("开始了");

            Object invoke = method.invoke(calculator, args);

            System.out.println("结束了");

            return invoke;
        };

        Object o = Proxy.newProxyInstance(loader, interfaces, invocationHandler);


        return (Calculator) o;

    }
}




@Test
public void proxy(){


		Calculator calculator = myCalculatorProxy.proxyMethod(new MyCalculator());

		Integer add = calculator.add(1, 1);

		System.out.println(add);
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/915392.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号