• IOC和AOP


    前提IoC 容器

        Spring 容器是 Spring 框架的核心。容器将创建对象,把它们连接在一起,配置它们,并管理他们的整个生命周期从创建到销毁。Spring 容器使用依赖注入(DI)来管理组成一个应用程序的组件。这些对象被称为 Spring Beans,我们将在下一章中进行讨论。

        通过阅读配置元数据提供的指令,容器知道对哪些对象进行实例化,配置和组装。配置元数据可以通过 XML,Java 注释或 Java 代码来表示。下图是 Spring 如何工作的高级视图。 Spring IoC 容器利用 Java 的 POJO 类和配置元数据来生成完全配置和可执行的系统或应用程序。

        IOC 容器具有依赖注入功能的容器,它可以创建对象,IOC 容器负责实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。通常new一个实例,控制权由程序员控制,而"控制反转"是指new实例工作不由程序员来做而是交给Spring容器来做。在Spring中BeanFactory是IOC容器的实际代表者。

     

    一.使用多种方式实现IOC

    Spring的XML配置中,只有一种声明bean的方式:使用<bean>元素并指定class属性。Spring会从这里获取必要的信息来创建bean。

    但是,在XML中声明DI时,会有多种可选的配置方式和风格,具体到构造器方式和P命名空间方式。

    共三种:

        1.基于Setter注入值:

    <bean id="student" class="com.wdkseft.entity.Student">
        <property name="stu_id" value="1"></property>
        <property name="stu_name" value="456"></property>
    </bean>

     

        2.基于构造注入

    <bean id="student" class="com.wdkseft.entity.Student">
        <constructor-arg value="2" type="java.lang.Integer" index="0"></constructor-arg>
        <constructor-arg value="温柔的电" type="java.lang.String" index="1"></constructor-arg>
    </bean>

        3.p命名空间注入:基于setter注入    必须要有setter方法

    <bean id="student" class="com.wdkseft.entity.Student" p:stu_name=""></bean>

    二、 使用多种方式实现

        JDK动态代理

                 JDK动态代理所用到的代理类在程序调用到代理类对象时才由JVM真正创建JVM根据传进来的 业务实现类对象 以及 方法名 ,动态地创建了一个代理类的class文件并被字节码引擎执行,然后通过该代理类对象进行方法调用

     

    1、定义业务接口以及实现类

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Student {
        private Integer stu_id;
        private String stu_name;
    
        public void say(){
            System.out.println("大家好我的编号为:"+stu_id+"我的姓名是:"+stu_name);
        }
    }
    public interface IUserIntoMapper {
        //添加用户
        Integer addIUser(StudentIoc ioc);
    }
    public class IUserIntoMapperImpl implements IUserIntoMapper {
        @Override
        public Integer addIUser(StudentIoc ioc) {
            System.out.println("add scouces");
            return 0;
        }
    }

    2、调用管理接口MyBeforeAdvice创建动态代理类

    public class MyBeforeAdvice implements MethodBeforeAdvice, AfterReturningAdvice {
        @Override
        public void before(Method method, Object[] objects, Object o) throws Throwable {
            System.out.println("==========前置增强==========");
        }
    
        @Override
        public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
            System.out.println("==========后置增强==========");
        }
    }

     

        3.编写applicationContext.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: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">
    
        <!--声明DaoBean     Bean的注入都是实现类-->
        <bean id="iUserIntoMapper" class="com.wdkseft.mapper.impl.IUserIntoMapperImpl"></bean>
        <!--声明Service-->
        <bean id="iUserIntoService" class="com.wdkseft.service.impl.IUserIntoServiceImpl">
            <!--setter方法怎么注入:找到Name属性,将属性的开头改为大写,然后前缀加上setIUserIntoMapper-->
            <property name="iUserIntoMapper" ref="iUserIntoMapper"></property>
        </bean>
    
        <!--切面:增强类-->
        <bean id="myBeforeAdvice" class="com.wdkseft.advice.MyBeforeAdvice"></bean>
    
        <aop:config>
            <!--切面:你要对哪一个方法增强           expression:切点表达式匹配的-->
            <aop:pointcut id="pointcut" expression="execution(* *..service.*.*(..))"></aop:pointcut>
            <aop:advisor advice-ref="myBeforeAdvice" pointcut-ref="pointcut"></aop:advisor>
        </aop:config>
    
        <!--将Person类交给Spring容器管理(包括:实例化和给属性赋值)
            id:bean对象的唯一标识
            class:bean的全类名
            property:Person类的属性(可赋值)
        -->
        <!--<bean id="studentIoc" class="com.wdkseft.entity.StudentIoc">
            &lt;!&ndash;name:通过Person类中的属性名赋值&ndash;&gt;
            <property name="Stu_name" value="辨识度"></property>
            <property name="Stu_id" value="16"></property>
            <property name="teacher" ref="teacherIoc"></property>
        </bean>
    
        <bean id="teacherIoc" class="com.wdkseft.entity.TeacherIoc">
            &lt;!&ndash;name:通过Person类中的属性名赋值&ndash;&gt;
            <property name="t_id" value="16"></property>
            <property name="t_name" value="温柔的电"></property>
        </bean>
    
    
    
        &lt;!&ndash;彩色盒子&ndash;&gt;
        <bean id="colorInk" class="com.wdkseft.print.ink.ColorInk"></bean>
        &lt;!&ndash;黑白盒子&ndash;&gt;
        <bean id="grayInk" class="com.wdkseft.print.ink.GrayInk"></bean>
        &lt;!&ndash;A4纸张&ndash;&gt;
        <bean id="a4Paper" class="com.wdkseft.print.ink.paper.A4Paper"></bean>
        &lt;!&ndash;B5纸张&ndash;&gt;
        <bean id="b5Paper" class="com.wdkseft.print.ink.paper.B5Paper"></bean>
    
        &lt;!&ndash;打印机&ndash;&gt;
        <bean id="printer" class="com.wdkseft.print.ink.printer.Printer">
            <property name="ink" ref="colorInk"></property>
            <property name="paper" ref="a4Paper"></property>
        </bean>-->
    
    
    
    
    
    </beans>

        4.测试类

     

     

       //Aop
        @Test
        public void AopSpringTest(){
            ApplicationContext cyx = new ClassPathXmlApplicationContext("applicationContextTest.xml");
            IUserIntoService iUserIntoServiceImpl = (IUserIntoService) cyx.getBean("iUserIntoService");
            Integer integer = iUserIntoServiceImpl.addIUser(new StudentIoc());
            System.out.println(integer);
        }

     

     

     

     

    1. 基于Setter注入值:

  • 相关阅读:
    android 服务与多线程
    “产品级敏捷” 的这条路; 逐步的形成一高效的产品开发生态系统
    hdoj 1116 Play on Words 【并查集】+【欧拉路】
    辛星跟您玩转vim第四节之操作文本内容
    UVa 10828 Back to Kernighan-Ritchie 高斯消元+概率DP
    CMMI过程改进反例
    UVA 11077
    Yii 框架 URL路径简化
    交水费一波四折
    雷观(十五):提高生产力和程序员价值的2种方法
  • 原文地址:https://www.cnblogs.com/ws1149939228/p/11743509.html
Copyright © 2020-2023  润新知