在学习spring的时候,教程上说注解@Resource是根据类型来匹配的,但是有两个相同类型的bean也可以通过
我的配置文件中是<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.o...
我的配置文件中是<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<bean id="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>
<bean id="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>
<bean id="userService" class="com.bjsxt.service.UserService">
</bean>
</beans>
这样写的,其中用到注解的那个类是这样写的
package com.bjsxt.service;
import javax.annotation.Resource;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
public class UserService {
private UserDAO userDAO;
public void add(User user) {
userDAO.save(user);
}
public UserDAO getUserDAO() {
return userDAO;
}
@Resource
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}
驱动类是这样写的
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService) ctx.getBean("userService");
System.out.println(service.getUserDAO());
service.add(new User());
}
}
可是这样却依然可以通过,案例说不是应该报错吗,请教各位 展开
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<bean id="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>
<bean id="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>
<bean id="userService" class="com.bjsxt.service.UserService">
</bean>
</beans>
这样写的,其中用到注解的那个类是这样写的
package com.bjsxt.service;
import javax.annotation.Resource;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
public class UserService {
private UserDAO userDAO;
public void add(User user) {
userDAO.save(user);
}
public UserDAO getUserDAO() {
return userDAO;
}
@Resource
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}
驱动类是这样写的
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService) ctx.getBean("userService");
System.out.println(service.getUserDAO());
service.add(new User());
}
}
可是这样却依然可以通过,案例说不是应该报错吗,请教各位 展开
展开全部
@Resource注解注入是根据set方法后面的名字或者直接在注解后面增加属性来匹配的, 根据下面代码来看
@Resource
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
对应的应该是"userDAO"
<bean id="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>
如果你把方法改成
@Resource
public void setUserDAO2(UserDAO userDAO) {
this.userDAO = userDAO;
}
或者
@Resource(name="userDAO2")
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
那么就应该对应
<bean id="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>
所以有两个相同的类,但是注册ID不同,仍可以通过 ,你可以通过修改
<property name="daoId" value="2"></property>的value的值来测试下结果就知道了。
@Resource
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
对应的应该是"userDAO"
<bean id="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>
如果你把方法改成
@Resource
public void setUserDAO2(UserDAO userDAO) {
this.userDAO = userDAO;
}
或者
@Resource(name="userDAO2")
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
那么就应该对应
<bean id="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>
所以有两个相同的类,但是注册ID不同,仍可以通过 ,你可以通过修改
<property name="daoId" value="2"></property>的value的值来测试下结果就知道了。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询