spring2.5注解注入

package net.itdos.service.impl;

import javax.annotation.Resource;

import net.itdos.dao.PersonDao;
import net.itdos.service.PersonService;

public class PersonServiceBean implements PersonService {

	@Resource
	private PersonDao personDao;
	public void save(){
		personDao.save();
	}
}






<?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="personDao" class="net.itdos.dao.impl.PersonDaoBean"></bean>
           <bean id="personService" class="net.itdos.service.impl.PersonServiceBean">
           </bean>

</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring注入注解是一种在Spring框架中进行依赖注入的方式。通过使用注解,我们可以方便地将依赖对象注入到需要使用它们的类中,从而实现解耦和灵活性。 Spring框架提供了多种注入注解,常用的有: 1. `@Autowired`:自动装配,通过类型进行匹配注入。可以用在构造方法、属性、方法上。当需要注入的对象在容器中有多个实例时,可以结合`@Qualifier`使用。 2. `@Inject`:与@Autowired类似,也是进行自动装配,通过类型进行匹配注入。不过@Inject是Java规范中定义的注解,需要导入`javax.inject`包才能使用。 3. `@Resource`:按名称进行注入。可以通过`name`属性指定要注入的对象名称,也可以通过`type`属性指定要注入的对象类型。 4. `@Value`:用于从配置文件中读取属性值并注入到类的字段中。可以使用`${}`表达式读取配置文件中的值。 5. `@Qualifier`:与@Autowired或@Resource配合使用,通过指定bean的名称来消除自动装配时的歧义性。 6. `@Primary`:与@Autowired或@Resource配合使用,当需要注入的对象在容器中有多个实例时,优先选择带有@Primary注解的实例。 除了上述常用的注入注解Spring还提供了其他一些注解,如`@Component`、`@Service`、`@Repository`等,用于标识组件、服务、仓库等不同的类,并将其纳入到Spring容器中进行管理。 通过使用这些注解,我们可以在Spring框架中方便地实现依赖注入,提高代码的可读性和可维护性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值