• Java学习之Spring框架基础篇


    Java学习之Spring框架基础篇

    0x00 前言

    续上篇文章,继续更新Spring框架内容。

    0x01 Bean 自动装载

    注解自动装载

    在spring框架里面可以自动装配Bean。我们只需要在bean标签里面加上 autowire就可以了。
    autowire属性:

    no :缺省情况下,自动配置是通过“ref”属性手动设定
       	
    byName:根据属性名称自动装配。如果一个bean的名称和其他bean属性的名称是一样的,将会自装配它。
       	
    byType:按数据类型自动装配。如果一个bean的数据类型是用其它bean属性的数据类型,兼容并自动装配它。
       	
    constructor:在构造函数参数的byType方式。
       	
    autodetect:如果找到默认的构造函数,使用"自动装配用构造"否则,使用“按类型自动装配
    
    
    

    下面还是来看代码
    person类:

    package com.test.doamin;
    
    
    
    public class Perpon {
       private String name;
       private Dong dog;
    
       public Perpon() {
       }
    
       private Cat cat;
    
       public Perpon(String name, Dong dog, Cat cat) {
           this.name = name;
           this.dog = dog;
           this.cat = cat;
       }
    
       public String getName() {
           return name;
       }
    
       public void setName(String name) {
           this.name = name;
       }
    
       public Dong getDog() {
           return dog;
       }
    
       public void setDog(Dong dog) {
           this.dog = dog;
       }
    
       public Cat getCat() {
           return cat;
       }
    
       public void setCat(Cat cat) {
           this.cat = cat;
       }
    }
    

    cat与dog类:

    package com.test.doamin;
    
    public class Cat {
    
        public void method(){
            System.out.println("cat");
        }
    }
    

    这两个实体类代码基本一样,就不复制多份了。

    这些基本写好后,就可以来配置一下beans.xml文件了

    beans.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="person" class="com.test.doamin.Perpon" autowire="byName">
            <property name="name" value="xiaoming"/>
    
        </bean>
    
        <bean id="cat" class="com.test.doamin.Cat"/>
        <bean id="dog" class="com.test.doamin.Dong"/>
    
    </beans>
    

    写好后,我们再来写一些test类

        public static void main(String[] args) {
             ApplicationContext cl = new ClassPathXmlApplicationContext("bean.xml");  //传入xml文件
            Perpon person = (Perpon) cl.getBean("person");
            System.out.println(person.getName());
            person.getCat().method();
            person.getDog().method();
        }
    

    这样的配置就更简单写了,在使用byName的时候会自动在容器上下文查找和自己对象set方法后面的值对应的bean id。

    注解自动装载

    如果使用注解自动装载bean的话,我们需要对xml进行一个配置,加上context:annotation-config标签,并且需要导入约束。

    bean.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
           https://www.springframework.org/schema/context/spring-context.xsd">
        <context:annotation-config/>
        <bean id="cat" class="com.test.doamin.Cat"/>
        <bean id="dog" class="com.test.doamin.Dong"/>
        <bean id="person" class="com.test.doamin.Perpon">
            <property name="name" value="xiaoming"/>
        </bean>
        </beans>
    
    

    person类:

        private String name;
        @Autowired
        private Dong dog;
        @Autowired
        private Cat cat;
    

    其他地方一般无二,但是在成员变量出加入了 @Autowired注解声明。

    0x02 Spring Aop实现

    Aop概念:

    在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

    Aop核心概念:

    1、横切关注点
    
    对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点
    
    2、切面(aspect)
    
    类是对物体特征的抽象,切面就是对横切关注点的抽象
    
    3、连接点(joinpoint)
    
    被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器
    
    4、切入点(pointcut)
    
    对连接点进行拦截的定义
    
    5、通知(advice)
    
    所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类
    
    6、目标对象
    
    代理的目标对象
    
    7、织入(weave)
    
    将切面应用到目标对象并导致代理对象创建的过程
    
    8、引入(introduction)
    
    在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段
    
    

    Aop 实现方式一

    在使用前需要导入一个依赖包

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.5</version>
        <scope>runtime</scope>
    </dependency>
    
    
    

    定义一个serivce的接口:

    package com.test.service;
    
    public interface serivce {
        void add();
        void delete();
        void update();
        void qurey();
    }
    
    
    

    serivce实现类:

    package com.test.service;
    
    import org.springframework.aop.MethodBeforeAdvice;
    
    import java.lang.reflect.Method;
    
    public class serivceimpl implements serivce {
    
    
        public void add() {
            System.out.println("添加用户");
        }
    
        public void delete() {
            System.out.println("删除用户");
        }
    
        public void update() {
            System.out.println("修改用户");
    
        }
    
        public void qurey() {
            System.out.println("查询用户");
    
        }
    }
    
    

    定义完成后,我们需要在不修改代码的情况下,增强一下他的功能的话,以前会直接使用动态代理来实现。但是那也定义起来很麻烦。这里就可以使用到aop。

    这里再来定义2个方法,分别是方法执行前执行的方法,和方法执行后执行的方法。也叫做前置增强和后置增强

    前置增强方法:

    package com.test.log;
    import org.springframework.aop.MethodBeforeAdvice;
    
    import java.lang.reflect.Method;
    
    public class log  implements MethodBeforeAdvice {
    
    
        public void before(Method method, Object[] objects, Object o) throws Throwable {
            System.out.println(o.getClass().getName()+"De"+method.getName()+"方法执行了");
        }
    }
    
    

    我们这直接让他继承了MethodBeforeAdvice接口,继承该接口后面配置完aop后会在方法执行前进行执行。

    方法参数:

     1.method 要被激发的方法
     2.args 方法的参数
     3.target 目标对象,可能为null
    

    定义后置增强方法:

    package com.test.log;
    
    import org.springframework.aop.AfterAdvice;
    import org.springframework.aop.AfterReturningAdvice;
    
    import java.lang.reflect.Method;
    
    public class Afterlog implements AfterReturningAdvice {
        public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
            System.out.println("执行了"+method.getName()+"执行结果为"+o);
    
        }
    }
    

    定义完这样后,我们就可以来配置beans.xml文件了。

    beans.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
           https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <bean id="serivce" class="com.test.service.serivceimpl"/>
        <bean id="log" class="com.test.log.log"/>
        <bean id="afterlog" class="com.test.log.Afterlog"/>
    
        <aop:config>
    <!--        声明切入点,并且使用expression表达式设置需要执行的位置-->
            <aop:pointcut id="pointcut" expression="execution(* com.test.service.serivceimpl.*(..))"/>
    <!--        advice-ref:设置需要切入的功能,pointcut-ref:设置需要切入的点                      -->
            <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
            <aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>
        </aop:config>
    
    </beans>
    

    Aop 实现方式二

    前面需要定义并且继承接口,现在可以用了另一种方式,直接定义为一个类,然后在bean.xml里面配置就可以了。

    定义一个增强类,写上前置增强和后置增强的方法。

    package com.test.log;
    
    public class logp {
        public void before(){
            System.out.println("方法执行前执行");
        }
        public void after(){
            System.out.println("方法执行后执行");
        }
    }
    

    配置beans.xml文件

     <bean id="serivce" class="com.test.service.serivceimpl"/>
        <bean id="log" class="com.test.log.log"/>
        <bean id="afterlog" class="com.test.log.Afterlog"/>
        <bean id="logs" class="com.test.log.logp"/>
        <aop:config>
            <aop:aspect ref="logs">
                <aop:pointcut id="point" expression="execution(* com.test.service.serivceimpl.*(..))"/>
    <!--            使用前置增强 引用point-->
                <aop:before method="before" pointcut-ref="point"/>
                <!--            使用后置增强 引用point-->
                <aop:after method="before" pointcut-ref="point"/>
            </aop:aspect>
        </aop:config>
    

    0x03 结尾

    下篇更新Spring整合Mybatis框架,感觉还是得多写多记笔记,不然前面学习的内容容易忘。

  • 相关阅读:
    stone brook Introduction to Mathematical Statistics
    mongodb python
    CodesBay
    人机交互的本质是画图
    Latex模版–Review and Response Letters
    mongo with python
    OpenStack架构学习与解析
    Python类方法
    python装饰器
    java.sql.SQLException: Io 异常: Connection reset by peer: socket write error
  • 原文地址:https://www.cnblogs.com/nice0e3/p/13591289.html
Copyright © 2020-2023  润新知