Sping框架的两大核心技术知识

Spring框架的两大核心技术分别为:

         一.IOC(Inversion of Control, 控制反转)把创建对象的操作交给框架,亦被称为 DI(Dependency Injection, 依赖注入)

         二.AOP(面向切面编程)

IOC

        控制反转

           最基础的调用的对象是通过new一个对象出来,例如:People p=new People

           我们Spring框架中的IOC即改变这种方式的调用,将后面“new People”转换为xml文件去调用,即使用第三者调用

           举个生活中的小例子说明这样的好处:

           People p=new People   相当于两个人之前的直接调用,假如张三和李四,张三需要用到只有李四电脑里面的一份文件,张三就必须直接向李四要,假如李四的电脑坏了,那么张三也就拿不到这份文件,然而IOC就相当于张三叫王五去拿这份文件,李四将文件拷贝给了王五,在这过程中即使李四的电脑坏了,那么张三也能从王五那拿到文件,张三不会受到影响。这就是控制反转的最大好处。即用来降低程序代码之间的耦合度

          依赖注入

              实现依赖注入的三种方式:setter方式注入,构造注入,使用P命名实现注入

              setter方式注入

              创建学生类,给予学生类一个学号,一个姓名的属性(此两个属性必须生成set方法,才能完成setter方式的注入)

public class Student {

	//学号
	private int num;
	//姓名
	private String name;

	public void setNum(int num) {
		this.num = num;
	}

	public void setName(String name) {
		this.name = name;
	}
}

  在applicationContext.xml文件中赋值

<!-- setter方法注入 -->
<bean id="stu" class="Student" >
		<property name="num" value="1"></property>
		<property name="name" value="李四"></property>
		
</bean>

              构造注入

               给学生类写个带参的构造函数(写完带参的构造函数后,必须再定义一个无参的构造函数)

//构造器注入
public class Student {
	
	public Student(){
		
		
	}
	public Student(int num,String name){
		
	}
    //学号
	private int num;
	//姓名
	private String name;
}

  在applicationContext.xml文件中赋值

	<!-- 构造器注入 -->
	<bean id="st" class="Student">
		<constructor-arg>
			<value type="java.lang.int">101</value>
		</constructor-arg>
		<constructor-arg> 
            <value type="java.lang.String">张三</value>
		</constructor-arg>
	</bean> 

                使用P命名空间注入

                在Student最基础类的基础上,给ApplicationContext.xml文件中导入一个头的包

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"


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

</beans>

                之后在xml用bean标签赋值

<!-- 使用P命名注入 -->
<bean id="stu" class="test.Student" p:name="张三" p:num="1001">

		
</bean>

AOP

              面向切面编程,简单地说就是在不改变原程序的基础上为代码段增加新功能,对代码段进行增强处理

              前置增强、后置增强、最终增强、异常增强、环绕增强(定义一个用于增强的FirstAop类)

public class FirstAop {
	/*
	 * 
	 * 1.前置 2.后置 3.环绕  4.异常 5.最终
	 */
	//前置增强
	public void pre_study(JoinPoint jp){
		System.out.println("我是前置增强!");
	}
	//后置增强
	public void do_work(JoinPoint jp,Object result){
		//这个result是目标方法的返回值,
		System.out.println("我是后置增强!");
	}
	//最终增强
	public void do_final(JoinPoint jp){
		System.out.println("我是最终增强");
	}
	//异常增强
	public void do_exception(JoinPoint jp,Exception e){
		System.out.println("我是异常增强");	
	}
	//环绕增强
	public Object around(ProceedingJoinPoint pjp){
		System.out.println("我是环绕增强的前值增强");
		try {
			Object result=pjp.proceed();//执行目标方法并且获取目标方法的返回值
			result="改造之后的返回值";
			System.out.println("我是环绕增强的后置");
			return result;  //这个result会完美的替换目标方法的返回值
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return  null;
	}
}

               Student类中定义一个study方法用作于切口

   public lass Student{
    //学号
	private int num;
	//姓名
	private String name;
	
	//学习的方法,当做于切口
	public void study(){
		
		System.out.println("学生"+name+"学号"+num+"在学习");
	}
}

              ApplicationContext.xml文件中配置的代码如下

<!-- 配置切面 -->
	<aop:config>
		<!-- 声明目标点 -->
		<aop:pointcut expression="execution(public void study())" id="target"/>
		<!-- 连接切点跟被切点 -->
		<aop:aspect ref="first">
			<!-- 前置增强 -->
			<aop:before method="pre_study" pointcut-ref="target"/>
			<!-- 后置增强 -->
			<aop:after-returning method="do_work" returning="result" pointcut-ref="target"/>
			<!-- 最终增强 -->
			<aop:after method="do_final" pointcut-ref="target"/>
			<!-- 异常增强 -->
			<aop:after-throwing method="do_exception" throwing="e"  pointcut-ref="target"/>
			<!-- 环绕增强 -->
			<aop:around method="around" pointcut-ref="target"/>
		</aop:aspect>
	</aop:config>

              测试类中的关键代码(包括IOC以及AOP的测试)

public class Test {
		public static void main(String[] args) {
			//第一步:读取Spring的核心文件
			ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
			//第二步:根据ac对象获得里面所有的bean
			Student st=(Student) ac.getBean("stu");
			//调用study的方法
			st.study();
			 
		}
}

来自小小程序员的归纳与具体的实现步骤。学艺不精,还请其他各位大佬多多指教!!!

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员xiaoQ

你的鼓励是我无比的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值