【Spring框架二】Spring常用注解之Component、Repository、Service和Controller注解

在Spring常用注解第一步部分【Spring框架一】Spring常用注解之Autowired和Resource注解(http://bit1129.iteye.com/blog/2114084)中介绍了Autowired和Resource两个注解的功能,它们用于将依赖根据名称或者类型进行自动的注入,这简化了在XML中,依赖注入部分的XML的编写,但是UserDao和UserService两个bean仍然要在XML中进行注册,能否将UserDao和UserService两个bean(将它们进行一般后,就是泛指持久化层bean和业务逻辑层bean)也通过注解的方式而无需在XML中进行声明?

 

答案是可以的,Spring提供了一组通过自动检测将对应的类对象注册为bean的注解,

  • Component
  • Repository
  • Service
  • Contoller

Component是一个广泛的概念,泛指一个项目中某一个组件。通常,一个项目根据分层设计为请求控制层,业务逻辑层,持久化层和模型层。由于模型是变化的(或者它的作用于是Request),模型层基本不会让Spring进行管理。Component可以用于请求控制层,业务逻辑层,持久化层任意一层的组件,为了使注解更加清晰,Spring针对每一层提供了不同的注解,

请求控制层,业务逻辑层,持久化层分别对应Controller,Service和Repository三个注解,这只是一个分层的约定,Spring不会强制业务逻辑层的Service一定使用@Service注解,也不会强制持久化层一定使用@Repository注解,或者说使用了@Repository注解就一定表示持久化层。@Component,@Repository,@Service,@Controller是可以通用的,只不过用了@Repository,@Service,@Controller更清晰直观的表示注解的类是一个持久化层的组件,业务逻辑层的组件,请求控制组件

 

正如为了使用@Autowired和@Resource注解,在Spring的配置文件中,需要加上一行XML配置

 

 

 <context:annotation-config/>

 

为了使Spring自动识别项目中的自动注册bean的注解生效,需要在XML配置中添加如下一行:

 

 

    <context:component-scan base-package="com.tom.user"/>
 

 

 

实例

1. UserDao.java需要添加@Repository注解

2. UserService.java需要添加@Service注解,UserServer依赖的IUserDao字段需要添加@Autowired注解

3. Spring配置文件的内容:

 

 

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context.xsd
						http://www.springframework.org/schema/tx
						http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
    <!--使注解@Autowired,@Resource生效--> 
    <context:annotation-config/> 
    <!--使注解@Component,@Repository,@Service,@Controller生效-->
    <context:component-scan base-package="com.tom.user"/>
 </beans>
 

 

 4. 如果@Service和@Repository注解,全部替换为@Component注解,结果也是一样的

5. 此时的UserService没有bean的名称,如何获得UserService在Spring中注册的实例,通过ApplicationContext的根据类型获得bean实例的方法ApplicationContext.getBean(Class beanClazz)

6. @Component,@Repository,@Service,@Controller都有一个value属性,为value指定一个有意义的名义,比如@Service(value="userService"),那么可以通过userService这个名字来获取UserService在Spring配置的实例

 

 

        ClassPathXmlApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext-autowired.xml");
        IUserService userService = cxt.getBean(UserService.class);
//        IUserService userService = cxt.getBean("userService", UserService.class);

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值