spring_note 注解注入属性

[b]一、spring注释注入属性[/b]
package:com.spring.service
public class AccountService {
@Resource // 按类型匹配
@Resource(name="accountDAOImpl") // 到配置文件中查找对应id的类
@Autowired //自动装配
@Qualifier("accountDAOImpl") //根据autowired来自动装配 查找在配置文件中对应的id的类
private IAccountDAO dao;

public IAccountDAO getDao() {
return dao;
}

public void setDao(IAccountDAO dao) {
this.dao = dao;
}
}

package:com.spring.dao
public interface IAccountDAO {
void select();
}

package:com.spring.dao.impl
public class AccountDAOImpl mplements IAccountDAO{
public void select() {
System.out.println("select account ........");
}
}

applicationContext.xml
<?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="accountService" class="com.spring.service.AccountService"></bean>
<bean id="accountDAOImpl" class="com.spring.dao.impl.AccountDAOImpl"></bean>
</beans>

package:com.spring.junit
public class AccountTest {
private AccountService service;

@Before
public void before(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
service = (AccountService) ctx.getBean("accountService");
}

@Test
public void testFooService(){
service.getDao().select();
}

@After
public void after(){
service = null;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值