正常用ioc获取一个bean
接口
package shuai.spring.study.service;
public interface IHelloService {
public void sayHello();
}
实现类
package shuai.spring.study.service.impl;
import shuai.spring.study.service.IHelloService;
public class HelloServiceImpl implements IHelloService {
@Override
public void sayHello() {
System.out.println("============Hello World!");
}
}
配置文件
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="iHelloService" class="shuai.spring.study.service.impl.HelloServiceImpl"/>
</beans>
测试类
package shuai.spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import shuai.spring.study.service.IHelloService;
public class HelloTest {
@Test
public void testHelloWorld() {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("HelloWorld.xml");
IHelloService iHelloService = context.getBean("iHelloService", IHelloService.class);
iHelloService.sayHello();
}
}
测试结果:
============Hello World!
我们可以利用aop让指定的类实现其它的接口,就比如这个shuai.spring.study.service.impl.HelloServiceImpl类,让它实现INiHao接口
定义接口
package shuai.spring.study.service;
public interface INiHao {
public void sayNiHao(String param);
}
实现类
package shuai.spring.study.service.impl;
import shuai.spring.study.service.INiHao;
public class NiHaoImpl implements INiHao {
@Override
public void sayNiHao(String param) {
System.out.println("你好:" + param);
}
}
修改配置文件
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="iHelloService" class="shuai.spring.study.service.impl.HelloServiceImpl"/>
<bean id="iNiHao" class="shuai.spring.study.service.impl.NiHaoImpl"/>
<!-- <bean id="helloAspect" class="shuai.spring.study.aop.HelloAspect"/> -->
<!-- <aop:aspectj-autoproxy proxy-target-class="true"/> -->
<aop:config>
<!-- <aop:pointcut expression="execution(* shuai.spring.study.service..*.*(..))" id="pointcut"/> -->
<aop:aspect ref="helloAspect">
<!-- <aop:before
method="beforeHello(java.lang.String,java.lang.String)"
pointcut="execution(* shuai.spring.study.service..*.*(..)) and args(param0,param1)"
arg-names="param0,param1"/> -->
<aop:declare-parents
types-matching="shuai.spring.study.service.IHelloService+"
implement-interface="shuai.spring.study.service.INiHao"
delegate-ref="iNiHao"/>
</aop:aspect>
</aop:config>
</beans>
注意,有关引入新接口的内容都写在declare-parents里面
types-matching表示要为哪些类配置新的接口:shuai.spring.study.service.IHelloService+表示实现了IHelloService接口的类
implement-interface表示要为这些类配置什么接口
delegate-ref表示implement-interface所代表接口的默认实现,delegate-ref表示引用一个bean,也可以写成default-impl="shuai.spring.study.service.impl.NiHaoImpl"
测试类
package shuai.spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import shuai.spring.study.service.INiHao;
public class HelloTest {
@Test
public void testHelloWorld() {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("HelloWorld.xml");
INiHao iHelloService = context.getBean("iHelloService", INiHao.class);
iHelloService.sayNiHao("世界");
}
}
测试结果:
你好:世界
这个结果跟那个默认实现是有关系的。