• spring基本使用


    1. ioc(Inversion of Control) | DI(Dependency Injection)

    (1) maven坐标导包

      <dependencies>
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-contextartifactId>
          <version>5.3.22version>
        dependency>
        <dependency>
          <groupId>junitgroupId>
          <artifactId>junitartifactId>
          <version>4.13.2version>
          <scope>testscope>
        dependency>
      dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    (2) 编写配置文件bean.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
           
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    (3) 配置bean

    在bean.xml配置文件的中配置bean

    <bean id="stu" class="com.xjy.pojo.student">
    bean>
    
    • 1
    • 2

    获取注入的对象:

        @Test
        public void test1(){
            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
            student stu = context.getBean(student.class);
            stu.setName("小明");
            stu.setAge(18);
            stu.setGender('男');
            System.out.println(stu);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    (4) 配置文件注入属性

     <bean id="stu" class="com.xjy.pojo.student">
            <property name="name" value="小慧慧">property>
            <property name="age" value="18">property>
            <property name="gender" value="">property>
        bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    获取对象:

        @Test
        public void test1(){
            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
            student stu = context.getBean(student.class);
            System.out.println(stu);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2. DI(dependency injection) 依赖注入(setter)其他属性

    (1) 对象属性注入

        <bean id="address" class="com.xjy.pojo.Address">
            <property name="province" value="云南">property>
            <property name="city" value="昆明">property>
            <property name="specificPosition" value="云南大学">property>
        bean>
        
        <bean id="stu" class="com.xjy.pojo.student">
            
            <property name="address" ref="address">bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    (2) 数组属性输入

        <bean id="stu" class="com.xjy.pojo.student">
            
            <property name="address" ref="address">property>
            
            <property name="grades">
                <array>
                    <value>90value>
                    <value>70value>
                    <value>60value>
                array>
            property>
        bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    查看结果:
    在这里插入图片描述

    (3) 集合属性注入

            
            <property name="course">
                <list>
                    <value>语文value>
                    <value>数学value>
                    <value>英语value>
                list>
            property>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    (4) map集合注入

            
            <property name="girlfriend">
                <map>
                    <entry key="小诗诗" value="温柔可爱灵力大方">entry>
                map>
            property>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (5) 构造器注入

    声明构造方法:

        public student(String name, int age, char gender) {
            this.name = name;
            this.age = age;
            this.gender = gender;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    声明bean(构造方法的参数需要和注入参数一一对应)

        <bean id="stu" class="com.xjy.pojo.student">
            <constructor-arg name="name" value="小诗诗">constructor-arg>
            <constructor-arg name="age" value="18">constructor-arg>
            <constructor-arg name="gender" value="">constructor-arg>
        bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    (6) 自动装配

        <bean id="address" class="com.xjy.pojo.Address">
            <property name="province" value="云南">property>
            <property name="city" value="昆明">property>
            <property name="specificPosition" value="安宁">property>
         bean>
         
        <bean id="stu" class="com.xjy.pojo.student" autowire="byType">
            
    
      bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3. 注解定义bean和依赖注入

    (1) 开启注解扫描功能(配置文件)

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           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.xsd">
        
        <context:component-scan base-package="com.xjy.pojo">context:component-scan>
        
     <context:property-placeholder location="jdbc.properties"/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    (2) 声明bean和注入属性

    @Component("address")   // 声明bean
    @PropertySource("jdbc.properties") // 配置文件读取
    public class Address {
        @Value("云南")   // 注入属性(可以使用${}引用配置文件属性)
        private String province;
        @Value("昆明")
        private String city;
        @Value("安宁")
        private String specificPosition;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    (3) 获取bean

        @Test
        public void testAddressBean(){
            ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
            Address address = (Address) ctx.getBean("address");
            System.out.println(address);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (4) 注解注入

    	// 可以配合@Qualifier("名称")进行指定注入
        @Autowired
        private Address address;   
    
    • 1
    • 2
    • 3

    查看注入是否成功:

        @Test
        public void testStudent(){
            ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
            student bean = context.getBean(student.class);
            System.out.println(bean);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (5) 配置类代替配置文件

    编写配置类:

    @Configuration		// 相当于配置文件
    @ComponentScan("com.xjy.pojo")   //相当于包扫描
    public class beanConfig {
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    通过配置文件获取:

        @Test
        public void test1(){
            ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(beanConfig.class);
            Address add = context.getBean(Address.class);
            System.out.println(add);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (6) 第三方bean配置

    在配置文件中配置bean:

        @Bean
        public ArrayList<String> stu(){
            return new ArrayList<>();
        }
    
    • 1
    • 2
    • 3
    • 4

    4. AOP(Aspect Oriented Programming)面向切面编程

    (1) 依赖导入

        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>5.3.22</version>
        </dependency>
            <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>1.9.19</version>
        </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    (2) 编写切面类

    @Component      // 将该类声明为bean
    @Aspect         // 声明该类为切面类
    public class stuAspect {
    
        @After("execution(* com.xjy.pojo.student.*(..))")  // 后置通知,->切入点表达式表示插入方法
        public void getCurrent(JoinPoint joinPoint){
            Class<? extends JoinPoint> aClass = joinPoint.getClass();
            System.out.println(aClass+"执行结束");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    (3) 开启对aop的支持和注解扫描

    @Configuration
    @EnableAspectJAutoProxy     // 开启aop的支持
    @ComponentScan({"com.xjy.pojo","com.xjy.aspect"})// 扫描注解包
    public class beanConfig {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    (4) 编写测试类测试

        @Test
        public void stuTest() throws InterruptedException {
            ApplicationContext context = new AnnotationConfigApplicationContext(beanConfig.class);
            student stu = context.getBean(student.class);
            stu.getSum();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (5) 插入点表达式

    execution(修饰符 返回值 方法全限定名(方法参数) 异常类型)
    
    例如: (所有修饰符,所有返回值,com.examle.service中的所有类所有方法,所有参数)
    execution(* com.example.service.*.*(..))
    
    • 1
    • 2
    • 3
    • 4

    (6) 通知方法

    前置通知Before

    在目标方法执行前执行的通知。可以通过定义@Before注解的方法实现

    后置通知(AfterReturning)

    在目标方法成功执行后执行的通知。可以通过定义@AfterReturning注解的方法实现。

    环绕通知(Around)

    在目标方法执行前后都可以执行的通知,而且可以控制目标方法的执行。可以通过定义@Around注解的方法实现

    @Component      // 将该类声明为bean
    @Aspect         // 声明该类为切面类
    public class stuAspect {
        @Pointcut("execution(* com.xjy.pojo.student.*(..))")
        public void cut(){}
        @Around("cut()")  // 前置通知,->切入点表达式表示插入方法
        public int getCurrent(ProceedingJoinPoint joinPoint) throws Throwable {
            long pre = System.currentTimeMillis();
            int result = (int) joinPoint.proceed();
            long end = System.currentTimeMillis();
            System.out.println(end-pre+"执行完毕");
            return result;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    测试运行:

        @Test
        public void stuTest() throws InterruptedException {
            ApplicationContext context = new AnnotationConfigApplicationContext(beanConfig.class);
            student stu = context.getBean(student.class);
            int sum = stu.getSum();
            System.out.println(sum);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    异常后通知(AfterThrowing)

    在目标方法抛出异常时执行的通知。可以通过定义@AfterThrowing注解的方法实现

    最终通知(AfterAdvice)

    无论目标方法是否正常执行完成(包括正常返回或抛出异常),都会执行的通知。可以通过定义@After注解的方法实现。

  • 相关阅读:
    pyinstaller瘦身指南
    形态学操作—开运算
    Python <算法思想集结>之抽丝剥茧聊动态规划
    2023-5-22-C++异常处理机制学习
    当资本对于区块链的关注开始减退,一场降温开始在区块链行业上演
    Windows卸载Nessus与安装Nessus
    如何将python脚本变成linux命令?
    实测 ubuntu20.04 camera_calibration 相机内参标定
    【猫meme】轻松C语言-2.C语⾔数据类型和变量
    基于FPGA的图像坏点像素修复算法实现,包括tb测试文件和MATLAB辅助验证
  • 原文地址:https://blog.csdn.net/m0_65385133/article/details/138144849