Spring第二天

类扫描注解

案例

1、创建实例(Persion和Student)

@Component("persion")//相当于<bean id = "persion" class = "../Persion"></bean>
public class Persion {
    @Resource(name = "student")
    private Student student;
    public void say(){
        student.say();
    }
}


@Component("student")
public class Student {
    public void say(){
        System.out.println("student");
    }
}

2、配置文件进行配置

<?xml version="1.0" encoding="UTF-8"?>
<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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
      <context:component-scan base-package="spring.com.bxp.bean"></context:component-scan>
</beans>

3、测试

public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Persion persion = (Persion) context.getBean("persion");
        persion.say();
    }  
步骤:

1、启动Spring容器
2、Spring容器解析到类扫描注解解析器<context:component-scan base-package="spring.com.bxp.bean"></context:component-scan>的时候,在base-package指定的包及其子包下查找所有的类
3、查看哪些类上面含有@Component
4、如果注解的value属性的值为空,则把类名的第一个字母变为小写,作为id值,放入到Spring容器中
5、如果value属性的值不为空,则使用value的值作为id值,放入到spring容器中。
6、在查找spring中的类的所有属性,按照@Resource的规则进行查找。
综上所述,使用了类扫描机制的做法,配置文件中配置很简单,但是效率越来越低。

继承

案例1

1、创建实例(Book, Persion, Student)

public class Book {
    public void read(){
        System.out.println("阅读书籍");
    }
}


public class Persion {
    private Book book;
    
    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public void read(){
        book.read();
    }
}


public class Student extends Persion{
    
}

2、配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">


        <bean id="persion" class="spring.com.bxp.bean.Persion">
            <property name="bok" ref="book"></property>
        </bean>
        <bean id="student" class="spring.com.bxp.bean.Student"></bean>
        <bean id="book" class="spring.com.bxp.bean.Book"></bean>
</beans>

3、测试

public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) context.getBean("student");
        student.read();
    }  

运行结果出错,空指针,说明在student中没有继承persion中book属性的值。
解决办法,在配置文件的student的bean上添加属性parent

<bean id="student" class="spring.com.bxp.bean.Student" parent="persion"></bean>
案例二

1、创建实例

@Component
public class Book {
    public void read(){
        System.out.println("阅读书籍");
    }
}

@Component
public class Persion {
    @Resource(name = "book")
    private Book book;

    public void read(){
        book.read();
    }
}


@Component
public class Student extends Persion{
    
}

2、配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:component-scan base-package="spring.com.bxp.bean"></context:component-scan>
</beans>

3、测试

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) context.getBean("student");
        student.read();
    }  
}

运行结果正确

综上连个案例:在使用继承的过程中,在配置文件中给属性赋值,需要使用parent属性。使用注解给属性赋值,则不需要。

AOP

切入点表达式
切入点表达式对应图

execution(public * *(..)):所有的公共方法
execution(* set*(..)) :代表所有的以set开头的方法
execution(* com.xyz.server.AccountServer.*(..)):代表com.xyz.server包下的AccountServer类中的所有方法
execution( * com.xyz.server.*.*(..)):代表con.xyz.server包下的所有类中的所有方法
execution( * com.xyz.server..*.*(..)):代表con.xyz.server包及其子包下的所有类中的所有方法

第一个AOP程序

引入两个jar包

aspectjrt.jar
aspectjweaver.jar

1、创建对象

public interface Dao {
    void save();
    void update();
}


public  class DaoImpl implements Dao{
    @Override
    public void save() {
        System.out.println("保存数据");
        /**
         * 此处书写保存全部数据数据的程序
         */
    }

    @Override
    public void update() {
        /**
         * 此处书写更新数据的程序
         */
    }

}


public class Transaction {
    public void open(){
        System.out.println("开启事物");
    }
    public void commit(){
        System.out.println("提交事物");
    }
}

2、创建配置文件
引入以下名称空间:

xmlns:aop="http://www.springframework.org/schema/aop"

http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop"
       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-2.5.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
           
    <bean id="daoimpl" class="spring.com.bxp.proxy.DaoImpl"></bean>
    <bean id="transaction" class="spring.com.bxp.proxy.Transaction"></bean>
    
    
    <aop:config>
    <!-- 切入点 -->
        <aop:pointcut expression="execution(* spring.com.bxp.proxy.DaoImpl.*(..))"
         id="perform"/>
         <!-- 切面 -->
        <aop:aspect ref="transaction" >
            <!-- aop:before:前置通知 
            aop:after-returning:后置通知
            pointcut-ref:指向切入点表达式
            -->
            <aop:before pointcut-ref="perform" method="open"/>
            <aop:after-returning pointcut-ref="perform" method="commit"/>
        </aop:aspect>
    </aop:config>


</beans>

3、测试类

public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        Dao dao = (Dao) context.getBean("daoimpl");
        dao.save();
    }
AOP步骤:

1、启动spring容器
2、实例化bean对象
3、spring容器 解析aop配置
(1)切入点表达式:把id对应的类包含进spring容器
(2)前置通知,后置通知
4、spring容器会按照切入点表达式所匹配的类在spring容器中进行查找,如果找到该类,则会创建代理对象,如果找不到,则报错。
5、spring容器产生代理对象的方法:通知+目标方法
6、客户端通过context.getBean()得到一个对象,如果该对象有代理,则返回代理对象,如果没有代理,则返回对象本身。

说明:1、客户端通过context.getBean()得到一个对象,如果该对象有代理,则返回代理对象,如果没有代理,则返回对象本身
2、如果目标类实现了接口,则采用jvm生成代理对象,如果没有实现接口,则采用cglib生成代理对象,而生成代理对象时候spring来做。

spring的通知:

前置通知:具有JoinPoint参数
    /**
     * 连接点,代表一个方法,客户端调用那个方法,那个方法就是连接点
     * 
     * 通过参数可以获取连接点的一些信息
     * @param joinPoint
     */
    public void open(JoinPoint joinPoint){
        //获取目标类
        joinPoint.getTarget();
        //获取连接点参数
        joinPoint.getArgs();
        //获取连接点的名称
        joinPoint.getSignature().getName();
        System.out.println("开启事物");
    }

    <!-- aop:before:前置通知 
    pointcut-ref:指向切入点表达式
    -->
    <aop:before pointcut-ref="perform" method="open"/>
后置通知:
    /**
     * @param joinPoint:连接点
     * @param val:接受目标参数的返回值
     */
    public void commit(JoinPoint joinPoint, Object val){
        System.out.println("提交事物");
    }

    <!-- 
    aop:after-returning:后置通知
    val:和后置通知方法的参数名称保持一致
    -->
    <aop:after-returning pointcut-ref="perform" method="commit" returning="val"/>
        
如果目标方法遇到异常,后置通知将不执行
最终通知:
    /**
     * 
     * @param joinPoint:连接点
     */
    public void finallyMythod(JoinPoint joinPoint){
        
    }

    <aop:after method="finallyMythod" pointcut-ref="perform"/>
不管目标方法是否遇到异常,最终通知都会执行
异常通知:
    /**
     * 
     * @param joinPoint
     * @param throwable:获取目标方法抛出的异常
     */
    public void thowingMythod(JoinPoint joinPoint, Throwable throwable){
        
    }

    <!-- 
    throwing:和通知中的异常参数名称相同
     -->
    <aop:after-throwing method="thowingMythod" pointcut-ref="perform" throwing="throwable"/>
环绕通知:
    public void arroundMythod(ProceedingJoinPoint joinPoint) throws Throwable{
        joinPoint.proceed();//执行目标方法
    }

    applicationContext.xml
环绕通知可以控制目标方法的执行
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,873评论 4 370
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,483评论 1 306
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,525评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,595评论 0 218
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,018评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,958评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,118评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,873评论 0 208
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,643评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,813评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,293评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,615评论 3 262
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,306评论 3 242
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,170评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,968评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,107评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,894评论 2 278

推荐阅读更多精彩内容