z****n 发帖数: 1933 | 1 定义一个接口
public interface DataAccessDao
实现
public class DataAccessDaoImpl implements DataAccessDao
单元测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/DataAccessTest.xml")
public class DataAccessTest
{
@Autowired
private DataAccessDaoImpl dataAccessDao;
spring配置
然后出错, org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type[com.phalanxim.data.dao.impl.DataAccessDaoImpl]
found for dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations: {@org.springframework
.beans.factory.annotation.Autowired(required=true)}
经过研究,在单元测试里把
private DataAccessDaoImpl dataAccessDao; 改为
private DataAccessDao dataAccessDao; 成功
或者在实现中,把
public class DataAccessDaoImpl implements DataAccessDao改为
public class DataAccessDaoImpl, 去掉接口也成功。
但是autowire="byType" 或者 autowire="byName" 都不行。
请问为什么spring autowire 倾向于匹配接口而不是class本身。我在spring配置里指
定的是
class="com.phalanxim.data.dao.impl.DataAccessDaoImpl"啊。根本没提接口。
谢谢 |
s******e 发帖数: 493 | 2 it is related to how spring proxies your class.
Google search is your best friend. |
z****e 发帖数: 54598 | 3 autowire + qualifier
但是推荐使用
resource |
z****n 发帖数: 1933 | 4 谢谢了,我想这篇文章说明了问题
http://blog.springsource.org/2012/05/23/understanding-proxy-usa
At startup time, a new class is created, called proxy. This one is in charge
of adding Transactional behavior as follows:
【在 s******e 的大作中提到】 : it is related to how spring proxies your class. : Google search is your best friend.
|
z****n 发帖数: 1933 | 5 谢谢,还不熟悉这两个annotation的用法,回去好好研究。
【在 z****e 的大作中提到】 : autowire + qualifier : 但是推荐使用 : resource
|