Spring 中@Component @Controller @Service @Repository注解的意思

    在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spring容器管理的类。即就是该类已经拉入到spring的管理中了。而@Controller, @Service, @Repository是@Component的细化,这三个注解比@Component带有更多的语义,它们分别对应了控制层、服务层、持久层的类。 

@Repository标签是用来给持久层的类定义一个名字,让Spring根据这个名字关联到这个类。 
例如: 
[java]  view plain  copy
  1. @Repository("userDao")   
  2. public class UserDaoImpl  implements UserDao{   
  3.   
  4.    ........................................   
  5.   
  6. }  
声明了UserDaoImpl  在Spring容器中叫userDao这个名字。 



@Service是用于服务层的IServiceImpl类文件,功能与@Repository类似。 
另外标签:@Autowired 用来注入。 

例如: 
[java]  view plain  copy
  1. @Autowired   
  2. private UserDao userDao;  


这样就注入进去了,相当于我们new了个实现类,我们就无需写setter方法了。 

我们还得有配置文件进行配置: 

[html]  view plain  copy
  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:aop="http://www.springframework.org/schema/aop"   
  5.        xmlns:tx="http://www.springframework.org/schema/tx"   
  6.        xmlns:context="http://www.springframework.org/schema/context"   
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  8.      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
  9.      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  10.      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">   
  11.     <context:annotation-config/>   
  12.     <context:component-scan base-package="com.syx.manager">   
  13.         <context:include-filter type="regex" expression=".*DaoImpl"/>   
  14.         <context:include-filter type="regex" expression=".*ServiceImpl"/>   
  15.     </context:component-scan>   
  16. </beans>  


这样就把com.syx.manager包下的所有.*DaoImpl,.*ServiceImpl都注册关联到Spring容器中去了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值