在利用spring执行代码获取代理类时,getBean方法中必须是接口类名,否则将会出现错误,例如
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
IMathService mathService = applicationContext.getBean(MathService.class);
在getBean中写入的是MathService,而不是IMathService,此时控制台将会出现错误:
此时出现错误的原因是由于此时是由JDK进行代理,其中必须是接口类,否则将不能正常运行,Spring中有JDK代理和CGLib动态代理两种代理方式,两种方式所产生的代理类有着不同。
在具体的例子实现中,新建接口和实现类:
package com.jd.math;
public interface IMathService {
int add(int a,int b);
int div(int a,int b);
}
package com.jd.math;
import org.springframework.stereotype.Service;
@Service
public class MathService implements IMathService {
@Override
public int add(int a