Spring注解开发

1.开启注解扫描

<!--开启注解扫描-->
<context:component-scanbase-package="com.qf"/>

2.Component 注解

用于替换自建类型组件的 <bean...> 标签;可以更快速的声明 bean
@Component 注解的含义:创建类的对象,相当于 xml 中配置的 bean 标签

        @Controller 针对于控制器层的类的注解

        @Service 针对于业务层的类的注解        
        @Repository 针对持久层的类的注解
@Component
//@Service("empService")
//@Controller("empService")
//@Repository("empService")
public class EmpServiceImplimplementsEmpService{
}

 3.Autowired注解

在Spring框架中,使用 @Autowired注解按照类型注入,首先,确保你的类上有 @Component@Service等注解,这样Spring才能识别并管理这个类
主要用于属性注入
@Autowired 自动装配:将对象属性自动进行注入 ( 类型注入 )
@Qualifier("empDaoImpl1") 限定要自动注入的 bean id ,一般和 @Autowired 联用
注意: @Autowired 先会按照类型注入,如果这个类有多个实例,那么再按照名称注入,如果有多
个实例那么需要使用 Qualifier 进行指定
@Component
public class EmpServiceImplimplementsEmpService{
  @Autowired
  @Qualifier("empDaoImpl1")
  //@Resource(name="empDaoImpl2")  
  private EmpDao empDao;
  public void addEmp(){
    empDao.addEmp();
    }
}

 4.Resource注解

主要用于属性注入
@Resource 自动装配:将对象属性自动进行注入(名称注入)
注意:默认按照名称进行装配,如果找到指定的名称则直接注入。如果没有找到,那么再按类型称
注入,如果有多个实例那么需要使用 name 属性进行指定
@Component//@Controller @Service @Repository
public class EmpServiceImpl implements EmpService{
    @Resource(name="empDaoImpl2")  
    private EmpDao empDao;
    public void addEmp(){
        empDao.addEmp();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xiao Jian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值