@Component @Service @Controller @Repository注解使用

@Component 相当于实例化类的对象。

 

通过在 classpath 中通过自动扫描方式把组建纳入 spring 容器管理。

要使用自动扫描机制我们需要打开一下配置信息:

 

Bean.xml代码    收藏代码
  1. <?xml version= "1.0"  encoding= "UTF-8" ?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.        xmlns:context="http://www.springframework.org/schema/context"   
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans-2.5 .xsd  
  7.            http://www.springframework.org/schema/context  
  8.            http://www.springframework.org/schema/context/spring-context-2.5 .xsd">  
  9.            <context:component-scan base-package="com.zchen" />  
  10. </beans>  

 

 

注:前面讲要使用注解需要配置: <context:annotation-config />但如果使用了@Component就不需要加它了,因为:<context:component-scan base-package="com.zchen">里面默认了<context:annotation-config />。

Java代码    收藏代码
  1. @Component ( "userService" )  
  2. public   class  UserService {  

 @Service用于标注业务层组件、

 @Controller用于标注空竹曾组件(如Struts中的action)

 @Repository用于标注数据访问组件即DAO组件

 @Component泛指组件,当组件不好归类的时候我们可以使用这个注解进行标注,(现在可以都用此注解)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值