spring核心点总结

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

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

<bean  id=""  class="">

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

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

</bean>

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

1.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>

2 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  dragon
10  *
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  dragon
12  *
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;
 9
10/**
11 * @author dragon
12 *
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      
21
22}

环绕通知:CompareInterceptor.java

package com.dragon.Advice;
 5
 6import org.aopalliance.intercept.MethodInterceptor;
 7import org.aopalliance.intercept.MethodInvocation;
 8
 9
10/**
11 * @author dragon
12 *
13 */
14public class CompareInterceptor implements MethodInterceptor{
15
16      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<?xml version="1.0" encoding="UTF-8"?>
 2<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 3
 4<beans>
 5
 6<bean id="beforeAdvice" class="com.dragon.Advice.BeforeAdvice"></bean>
 7<bean id="afterAdvice" class="com.dragon.Advice.AfterAdvice"></bean>
 8<bean id="compareInterceptor" class="com.dragon.Advice.CompareInterceptor"></bean>
 9<bean id="studenttarget" class="com.dragon.study.Impl.StudentImpl"></bean>
10
11<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
12  <property name="proxyInterfaces">
13    <value>com.dragon.study.IStudent</value>
14  </property>
15  <property name="interceptorNames">
16    <list>
17     <value>beforeAdvice</value>
18     <value>afterAdvice</value>
19    <value>compareInterceptor</value>  
20    </list>
21  </property>
22  <property name="target">
23    <ref bean="studenttarget"/>
24  </property>
25
26</bean>
27
28
29
30
31</beans>

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

4package com;
 5
 6import org.springframework.context.ApplicationContext;
 7import org.springframework.context.support.FileSystemXmlApplicationContext;
 8
 9import com.dragon.study.IStudent;
10
11/**
12 * @author dragon
13 *
14 */
15public class Test {
16
17    /**
18     * @param args
19     */
20    public static void main(String[] args) {
21        // TODO Auto-generated method stub
22      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    }
30
31}

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

时间: 2024-08-08 13:38:54

spring核心点总结的相关文章

Spring ——AOP

AOP是OOP的延续,是软件开发中的一个热点. AOP技术,是OOP补充.OOP引入封装,继承和多态建立一种对象层次结构模拟公共行为集合,而对从左到右的关系则显得无能为力.对于AOP则恰恰适应这种横切技术. 简单说,就与业务无关,却为了业务模块所共同调用的逻辑封装起来,便于减少系统重复代码,降低模块间耦合度,利用维护和可操作性 横切技术将软分为两部分:核心关注点和横切关注点:业务处理流程为核心关注,与之关系不大的是横切关注.如:系统中各处都相似的日志,事务,权限成为横切关注点.AOP作用是将核心

Spring框架[一]——spring概念和ioc入门(ioc操作xml配置文件)

Spring概念 spring是开源的轻量级框架(即不需要依赖其他东西,可用直接使用) spring核心主要两部分 aop:面向切面编程,扩展功能不是修改源代码来实现: ioc:控制反转,比如:有一个类,在类中有个方法(非静态的方法),要调用类中的这个方法,则需要创建类的对象,使用对象调用方法.创建类对象的过程,需要new出来对象:而ioc则是将对象的创建不是通过new方式实现,而是交给spring配置来创建对象(即,将对象的创建交给spring来管理): spring是一站式框架 spring

Spring MVC基础知识整理?View与Controller数据交互

概述 Spring MVC是由View—Controller—Model组成,其中View和Controller的数据交互,成为了关注的核心点.MVC中,我们将View中的数据传递到Controller,可以采用POST或者Get,传递相应的参数.Controller通过绑定来,匹配前台传递的参数.后台Controller也可以将值传递到前台页面. View值传递Controller 绑定传值的常用方式有如下 @RequestParam,绑定单个请求数据,可以是URL中的数据,表单提交的数据或上

[转]Spring Cloud在国内中小型公司能用起来吗?

原文地址:http://www.cnblogs.com/ityouknow/p/7508306.html 今天吃完饭休息的时候瞎逛知乎,突然看到这个一个问题Spring Cloud在国内中小型公司能用起来吗?,吸引了我的注意.仔细的看了题主的问题,发现这是一个好问题,题主经过了一番思考,并且用图形全面的将自己的疑问表达了出来,作为一个研究并使用Spring Boot和Spring Cloud近两年的程序员,看的我手痒痒不答不快呀. 好问题 好问题必须配认真的回答,仔细的看了题主的问题,发现这个问

Spring Core Container 源码分析三:Spring Beans 初始化流程分析

前言 本文是笔者所著的 Spring Core Container 源码分析系列之一: 本篇文章主要试图梳理出 Spring Beans 的初始化主流程和相关核心代码逻辑: 本文转载自本人的私人博客,伤神的博客: http://www.shangyang.me/2017/04/01/spring-core-container-sourcecode-analysis-beans-instantiating-process/ 本文为作者的原创作品,转载需注明出处: 源码分析环境搭建 参考 Sprin

Spring Core Container 源码分析七:注册 Bean Definitions

前言 原本以为,Spring 通过解析 bean 的配置,生成并注册 bean defintions 的过程不太复杂,比较简单,不用单独开辟一篇博文来讲述:但是当在分析前面两个章节有关 @Autowired.@Component.@Service 注解的注入机制的时候,发现,如果没有对有关 bean defintions 的解析和注册机制彻底弄明白,则很难弄清楚 annotation 在 Spring 容器中的底层运行机制:所以,本篇博文作者将试图去弄清楚 Spring 容器内部是如何去解析 b

JavaIOC框架篇之Spring Framework

欢迎查看Java开发之上帝之眼系列教程,如果您正在为Java后端庞大的体系所困扰,如果您正在为各种繁出不穷的技术和各种框架所迷茫,那么本系列文章将带您窥探Java庞大的体系.本系列教程希望您能站在上帝的角度去观察(了解)Java体系.使Java的各种后端技术在你心中模块化:让你在工作中能将Java各个技术了然于心:能够即插即用.本篇我们一起来了解SpringIOC相关知识以及其核心点. 目前Java体系常见的IOC框架有Google Guice,PicoContainer,Dagger,Spri

手写Spring事务框架

Spring事务基于AOP环绕通知和异常通知 编程事务 声明事务 Spring事务底层使用编程事务+AOP进行包装的   = 声明事务 AOP应用场景:  事务 权限 参数验证 什么是AOP技术 AOP技术应用场景 面向切面编程  解决代码复用问题 AOP编程核心点: 在方法之前或者之后处理事情 AOP底层实现原理:代理设计模式 Spring事务基于AOP的环绕通知 为什么用AOP: 复用 解耦 AOP: 静态代需要生成目标代理对象 动态代理不需要生成目标代理对象 动态代理分为:JDK动态代理 

【spring源码分析】IOC容器初始化(总结)

前言:在经过前面十二篇文章的分析,对bean的加载流程大致梳理清楚了.因为内容过多,因此需要进行一个小总结. 经过前面十二篇文章的漫长分析,终于将xml配置文件中的bean,转换成我们实际所需要的真正的bean对象. 总结 [spring源码分析]IOC容器初始化(一):主要分析了Spring是如何解析占位符以及BeanFactory的最终实现类DefaultListableBeanFactory. [spring源码分析]IOC容器初始化(二):以loadBeanDefinitions函数为切