我现在写好的Dao层,在做Service 也就是业务层吧。
我定义了一个userService(接口),和一个userServiceimpl(实现类)
在applicationContext.xml里面只是定义了userServiceimpl的bean
[code="xml"]
class="com.downloadmanage.service.impl.userServerImpl">
[/code]
剩下关于Dao和datesouce 以及SessionFactory的bean没有给出
我再做Junit测试时候
[code="java"]
public class userServiceImplTest extends
AbstractDependencyInjectionSpringContextTests {
private userService userService;
@Override
protected String[] getConfigLocations() {
return new String[]{"applicationContext.xml"};
}
public void testAddDisc(){
System.out.println("run addDisc");
DiscTable disc = new DiscTable();
disc.setDiscNumber("M16");
userService.addDisc(disc);
System.out.println("Finished");
}
public void setUserService(userService userService) {
this.userService = userService;
}
}
[/code]
仅仅给出了userService的seter方法。
这个Junit是可以编译过去,不过我就是很纳闷
既然我applicationContext.xml配置的只有userServiceImpl而没有userService,那Spring怎么找到是userServiceImpl实现了userService了呢?要是我再弄一个userServiceimpl2也实现了userService接口 Spring怎么找呢?
The AbstractDependencyInjectionSpringContextTests classes uses autowire by type.
Thus if you have multiple bean definitions of the same type, you cannot rely on this approach for those particular beans.
原来是autowire by type. [汗, 平时都没注意]
第二句话: 如果有多个bean定义的是相同类型,就不能使用这种方法来使用beans.
所以, 多个impl, 将不会注入.
LZ可以试下.
[code="java"] class="com.downloadmanage.service.impl.userServerImpl"> [/code]
改成:
[code="java"] class="com.downloadmanage.service.impl.userServerImpl"> [/code]
需要和
[code="java"]private userService userService;[/code]同名
我看错了.. 想当然了... 忽略上面的回答... :oops: