依赖注入,由传统的 has a,改为在配置文件中注入,降低依赖。
传统方式:依赖性大
public class A{ private B data1; ..... } public class B{ ... }
注入
public class A{ private BParent data1; public A(){...} public A(int i, B b){...} ..... } public interface BParent{ ... } public class B implements BParent{ ... }
<beans> <bean id="A" class ="xx.xxx.aaa"> <constructor-arg value="15">//构造注入简单类型 <constructor-arg ref="B">//构造注入一个类 <property name="data1" ref="B"> //第二种注入方法set注入 <property name="xx"> <list>//或者set <value>13</value>//或者<ref bean="xxx"> </list> </property>
<property name="xx"> <map> <entry key="xxx" value="xxx"/> //或者<entry key="xxx" value_ref="xxx"/> </map> </property>
<property name="xx"> <props> <prop key="xxx">"xxx"</prop> <prop key="xxx"><null/></prop>//值为null的时候 </props> </property>
</bean> <bean id="B" class ="xx.xxx.bbb"> </bean>
</beans>
自动匹配注入
byName 根据data名自动匹配id相同的bean
byType 根据data类型自动匹配id相同的bean
byConstructor 如果使用构造函数注入
autodetect 先尝试使用byConstructor,失败尝试使用byType
aop: 在xml中配置元素
package com.springinaction.springidol;
public class Audience {
public void takeSeats() {
System.out.println("The audience is taking their seats.");
}//Before performance
public void turnOffCellPhones() {
System.out.println("The audience is turning off their cellphones");//Before performance
}
public void applaud() {
System.out.println("CLAP CLAP CLAP CLAP CLAP");
}//After performance
public void demandRefund() {
System.out.println("Boo! We want our money back!");
}//After bad performance
}
<bean id="audience" class="com.springinaction.springidol.Audience" /> <aop:config> <aop:aspect ref="audience"> <aop:pointcut id="performance" expression= "execution(* com.springinaction.springidol.Performer.perform(..))" /> <aop:before pointcut-ref="performance" method="takeSeats" /> <aop:before pointcut-ref="performance" method="turnOffCellPhones" /> <aop:after-returning pointcut-ref="performance" method="applaud" /> <aop:after-throwing pointcut-ref="performance" method="demandRefund" /> </aop:aspect> </aop:config>
within 在这个包内
周围通知:befor,after-returning,after-throwing的结合
public void watchPerformance(ProceedingJoinPoint joinpoint) { try { System.out.println("The audience is taking their seats."); System.out.println("The audience is turning off their cellphones");//Before performance long start = System.currentTimeMillis(); joinpoint.proceed();//Proceed to advised method  long end = System.currentTimeMillis(); System.out.println("CLAP CLAP CLAP CLAP CLAP"); System.out.println("The performance took " + (end - start) + " milliseconds.");//After performance } catch (Throwable t) { System.out.println("Boo! We want our money back!");//After bad performance } }
<aop:config> <aop:aspect ref="audience"> <aop:pointcut id="performance2" expression= "execution(* com.springinaction.springidol.Performer.perform(..))" /> <aop:around pointcut-ref="performance2" method="watchPerformance()" /> </aop:aspect> </aop:config>
传递参数给通知:使用agrs
package com.springinaction.springidol; public interface MindReader { void interceptThoughts(String thoughts); String getThoughts(); } package com.springinaction.springidol; public interface MindReader { void interceptThoughts(String thoughts); String getThoughts(); } package com.springinaction.springidol; public interface Thinker { void thinkOfSomething(String thoughts); } package com.springinaction.springidol; public class Volunteer implements Thinker { private String thoughts; public void thinkOfSomething(String thoughts) { this.thoughts = thoughts; } public String getThoughts() { return thoughts; } }
<aop:config> <aop:aspect ref="magician"> <aop:pointcut id="thinking" expression="execution(* com.springinaction.springidol.Thinker.thinkOfSomething(String)) and args(thoughts)" /> //agrs将参数定义为thoughts <aop:before pointcut-ref="thinking" method="interceptThoughts" arg-names="thoughts" /> </aop:aspect> </aop:config>
给通知的对象添加方法:
<aop:aspect> <aop:declare-parents types-matching="com.springinaction.springidol.Performer+" //要通知的对象(实现了performer接口) implement-interface="com.springinaction.springidol.Contestant" //要添加的接口 default-impl="com.springinaction.springidol.GraciousContestant" //实现接口方法的类 /> </aop:aspect>
或者使用
<aop:declare-parents
types-matching="com.springinaction.springidol.Performer+"
implement-interface="com.springinaction.springidol.Contestant"
delegate-ref="contestantDelegate"
/>
<bean id="contestantDelegate"
class="com.springinaction.springidol.GraciousContestant" />
使用注解方法:
package com.springinaction.springidol;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
@Aspect
public class ContestantIntroducer {
@DeclareParents(
value = "com.springinaction.springidol.Performer+",
defaultImpl = GraciousContestant.class)
public static Contestant contestant;
}
<bean class="com.springinaction.springidol.ContestantIntroducer" />
<aop:aspectj-autoproxy/> //spring提供自动代理创建器类,名为AnnotationAwareaspectJAutoProxyCreator,可在spring上下文里注册为一个bean,为简化,spring在sop命名空间里提供了此自定义配置元素,自动创建bean,根据@pointcut注视定义的切点来自动代理相匹配的bean
aop方法2:注解
package com.springinaction.springidol; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; Annotating aspects 103 @Aspect public class Audience { @Pointcut( "execution(* com.springinaction.springidol.Performer.perform(..))") public void performance() { } Define pointcut @Before("performance()") Before public void takeSeats() { performance System.out.println("The audience is taking their seats."); } @Before("performance()") Before public void turnOffCellPhones() { performance System.out.println("The audience is turning off their cellphones"); }  @AfterReturning("performance()") public void applaud() { System.out.println("CLAP CLAP CLAP CLAP CLAP"); } @AfterThrowing("performance()") public void demandRefund() { System.out.println("Boo! We want our money back!"); } @Around("performance()") public void watchPerformance(ProceedingJoinPoint joinpoint) { try { System.out.println("The audience is taking their seats."); System.out.println("The audience is turning off their cellphones"); long start = System.currentTimeMillis(); joinpoint.proceed(); long end = System.currentTimeMillis(); System.out.println("CLAP CLAP CLAP CLAP CLAP"); System.out.println("The performance took " + (end - start) + " milliseconds."); } catch (Throwable t) { System.out.println("Boo! We want our money back!"); } } } <bean id="audience" class="com.springinaction.springidol.Audience" />
<aop:aspectj-autoproxy/>