簡述Spring 的核心: IOC(控制反轉)/AOP(面向切面)
IOC(控制反轉) 就是將物件的控制權從程式碼交給xml
學生類
public class Student{}
傳統的方法呼叫
Student st=new Student ();
對比
使用ioc 方法呼叫
Student st= xml.getBean();
具體程式碼:
applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
</beans>
Student類:
public Student(){
}
private String name;
private int num;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
private Teacher t;
public Teacher getT() {
return t;
}
public void setT(Teacher t) {
this.t = t;
}
public Student(String name,int num){
this.name=name;
this.num=num;
}
public void study(){
System.out.println("學生在學習"+"學生名字是"+name+"id是"+num);
}
test類:
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Student st = (Student) ac.getBean("stu");
st.study();
applicationContext.xml:
<!-- p標籤注值 -->
<bean id="stu" class="test.Student" p:name="小黑" p:num="100"></bean>
<!-- 構造注入-->(Student類要有構造方法)
<constructor-arg >
<value type="java.lang.String">張三</value>
</constructor-arg>
<constructor-arg >
<value type="int">1001</value>
</constructor-arg>
AOP(面向切面) 在不影響原有的程式碼基礎上 植入新程式碼
AOP的五種增強
1.前置2.後置3.環繞4.異常5.最終
詳細程式碼:
applicationContext.xml: <!-- 將aop類控制反轉為一個bean -->
<bean id="first" class="aop.FirstAop"></bean>
<!-- 配置切面 -->
<aop:config>
<!-- 宣告目標點 就是Student類的study方法 -->
<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="read" pointcut-ref="target"/>
<!--異常增強 -->
<aop:after-throwing method="throw_a" pointcut-ref="target" throwing="e"/>
<!-- 環繞增強 -->
<aop:around method="around" pointcut-ref="target"/>
</aop:aspect>
</aop:config>
FirstAop類:
//1.前置2.後置3.環繞4.異常5.最終
//前置增強
public void pre_study(JoinPoint jp) {
System.out.println("預習"+jp.getTarget());
}
//後置增強
public void do_work(JoinPoint jp,Object result){
System.out.println("學習完畢返回值是"+result+"做上機學習");
}
//最終增強
public void read(JoinPoint jp){
System.out.println("看書"+jp.getSignature().getName()+"方法結束執行");
}
//異常增強
public void throw_a(JoinPoint jp,RuntimeException e){
System.out.println(jp.getSignature().getName()+"方法發生異常"+e);
}
//環繞增強
public Object around(ProceedingJoinPoint pip){
System.out.println("我是環繞前置增強");
try {
//執行目標方法 並且獲取目標方法的返回值
Object result=pip.proceed();
result="改動之後的返回值";
System.out.println("我是環繞後置增強");
return result;//完美替換目標方法的返回值
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}