博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring核心点总结
阅读量:7013 次
发布时间:2019-06-28

本文共 4394 字,大约阅读时间需要 14 分钟。

hot3.png

1. IoC

ApplicationContext  context =new ClassPathXmlApplicationContext("applcationcontext.xml")

//ApplicationContext接口包括:ClassPathXmlApplicationContext. FilesystemXmlApplicationContext,

//根据名称或者id获取,bean的id和名称是唯一的

SequenceGenerator  generator=context.getBean("sequencegenerator")

2.依赖注入分为:设置注入和构造函数注

2.1  设置注入的属性可以是基本类型,也可以是对象关键配置示例如下:

 <bean  id=""  class="">

    <property   name="age"  value="28"/>   

    <property  name="stone"  ref="bigStone"/>

     此处也可以写成 

     <property  name="stone">  <ref  bean=""/>  </property>

      如果指引的bean在一个xml文件中,也可以写成:

      <property  name="stone">  <ref  local=""/>  </property>

</bean>

<bean  id="bigStone"  class="com.kerry.BigStone"/>

2.2 构造注入,通过构造函数注入,用index区分多个函数

  <bean  id=""  class="">

             <constructor-arg  index="0">

                 <ref  bean="bigStone"/>

              <constructor-arg/>

              <constructor-arg index="1">

                   <value> This is second parameter </value>   

              <constructor-arg/>

              使用constructor-arg的type属性,避免构造子冲突

           <constructor-arg  index="3"  type="string">

              <value>This is third parameter</value>

           <constructor-arg/>           

      </bean>

也可以这样简单写成:

             <bean  id=""  class="">

                   <constructor-arg    value="" /> 

                    <constructor-arg    value="" /> 

              </bean>

3 AOP

 1  /**  2  *  3   */  4  package  com.dragon.study; 5   6  /**  7  *  @author  dragon 8  * 9   */ 10  public   interface  IStudent  {11     12      public   void  addStudent(String name);13 } 14

 目标类StudentImpl

 2  *  3   */  4  package  com.dragon.study.Impl; 5   6  import  com.dragon.study.IStudent; 7   8  /**  9  *  @author  dragon10  *11   */ 12  public   class  StudentImpl  implements  IStudent {13  14        public   void  addStudent(String name) {15          System.out.println( " 欢迎  " + name + "  你加入Spring家庭! " );16      } 17 } 18

   前置通知:BeforeAdvice.java

   

 package  com.dragon.Advice; 5   6  import  java.lang.reflect.Method; 7   8  import  org.springframework.aop.MethodBeforeAdvice; 9  10  /** 11  *  @author  dragon12  *13   */ 14  public   class  BeforeAdvice  implements  MethodBeforeAdvice {15  16        public   void  before(Method method,Object[] args, Object target)17                  throws  Throwable {18           19           System.out.println( " 这是BeforeAdvice类的before方法. " );20           21       } 22 }

  后置通知:AfterAdvice.java

 

package com.dragon.Advice; 5 6import java.lang.reflect.Method; 7 8import org.springframework.aop.AfterReturningAdvice; 910/**11 * @author dragon12 *13 */14public class AfterAdvice implements AfterReturningAdvice{15    16    public void afterReturning(Object returnValue ,Method method,17                   Object[] args,Object target) throws Throwable{18        System.out.println("这是AfterAdvice类的afterReturning方法.");19    }20      2122}

  

环绕通知:CompareInterceptor.java

package com.dragon.Advice; 5 6import org.aopalliance.intercept.MethodInterceptor; 7import org.aopalliance.intercept.MethodInvocation; 8 910/**11 * @author dragon12 *13 */14public class CompareInterceptor implements MethodInterceptor{1516      public Object invoke(MethodInvocation invocation) throws Throwable{17          Object result = null;18         String stu_name = invocation.getArguments()[0].toString();19         if ( stu_name.equals("dragon")){20             //如果学生是dragon时,执行目标方法,21              result= invocation.proceed();22              23         } else{24             System.out.println("此学生是"+stu_name+"而不是dragon,不批准其加入.");25         }26        27          return result;28      }29}

配置文件applicationContext.xml

 1
 2 3 4
 5 6
 7
 8
 9
1011
12  
13    
com.dragon.study.IStudent
14  15  
16    
17     
beforeAdvice
18     
afterAdvice
19    
compareInterceptor
  20    
21  22  
23    
24  25262728293031

现在开始写测试类,Test.java

4package com; 5 6import org.springframework.context.ApplicationContext; 7import org.springframework.context.support.FileSystemXmlApplicationContext; 8 9import com.dragon.study.IStudent;1011/**12 * @author dragon13 *14 */15public class Test {1617    /**18     * @param args19     */20    public static void main(String[] args) {21        // TODO Auto-generated method stub22      ApplicationContext ctx = 23          new FileSystemXmlApplicationContext("/com/dragon/applicationContext.xml");24      25      IStudent person = (IStudent)ctx.getBean("student");26      person.addStudent("dragon");27      28//      person.addStudent("javadragon");29    }3031}

AOP部分代码来自 :http://www.blogjava.net/javadragon/archive/2006/12/03/85115.html

转载于:https://my.oschina.net/hanruikai/blog/340068

你可能感兴趣的文章
中间件事务码R3AC1里Block Size的含义
查看>>
实战Android 上推下拉——隐藏、显示ActionBar
查看>>
GDB 调试 Mysql 实战(二)GDB 调试打印
查看>>
Spring AOP(三) Advisor类架构
查看>>
LeetCode 316. Remove Duplicate Letters
查看>>
第十三课时:递归组件的使用
查看>>
【跃迁之路】【712天】程序员高效学习方法论探索系列(实验阶段469-2019.2.2)...
查看>>
SpiderData 2019年2月18日 DApp数据排行榜
查看>>
react-refetch的使用小例子
查看>>
周末游攻略 - 南昌之行
查看>>
tcpdump查看Nginx长连接还是短连接
查看>>
Vue+thinkJs博客网站(二)之thinkJs的使用
查看>>
Electron学习笔记:主进程与渲染进程的通信方式
查看>>
JVM(六)为什么新生代有两个Survivor分区?
查看>>
Spark是一种基本的开源大数据技术
查看>>
Iterator 和 for...of 循环
查看>>
Font-face目前浏览器的兼容性
查看>>
Etcd超全解:原理阐释及部署设置的最佳实践
查看>>
聊聊flink的NetworkBufferPool
查看>>
MySQL主从同步机制和同步延时问题追查
查看>>