当service有多个接口时,如果用
@Autowired
private TestService service;
就会报异常,告诉我们 他的实现只能有一个
所以需要自己去new不同的实现类,但是在这里因为我在serviceimpl中我们获取dao层也是用注解形式获取的,这里也会报异常,表示dao为空,这里dao层也不能用注解获取,这里在网上找到了获取dao的方法
@Component
public class SpringBeanUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
SpringBeanUtils.applicationContext=applicationContext;
}
public static Object getBean(String name) {
return applicationContext.getBean(name);
}
public static T getBean(String name,Class requiredType){
return applicationContext.getBean(name, requiredType);
}
}
创建这个工具类,然后在 spring-mvc.xml中添加
<bean id="springBeanUtils" class="com.dnkj.nms.util.SpringBeanUtils"/>
然后获取dao的方法修改一下,这里还需要注意,原先用的@service注解要去掉
//原先是这样写的
@Autowired
private test1 test;
//改为
private test1 test=(test1) SpringBeanUtils.getBean("test1");