2019独角兽企业重金招聘Python工程师标准>>>

Spring 配置管理 Bean XML .net
对Spring的注解标签刚刚接触,所以就找了几个常用的,记录下,感觉注解用了之后,会在*.xml文件中大大减少配置量。以前我们每个Bean都得到配置文件中配置关联下。spring2.5后,引入了完整的annotation配置注解,使得我们的程序配置更简单更容易维护。

@Component;@Controller; @Service ;@Repository

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

@Repository标签是用来给持久层的类定义一个名字,让Spring根据这个名字关联到这个类。

例如:

@Repository("userDao") 
public class UserDaoImpl  implements UserDao{

........................................

}

声明了UserDaoImpl  在Spring容器中叫userDao这个名字。

@Service是用于服务层的IServiceImpl类文件,功能与@Repository类似。

另外标签:@Autowired 用来注入。

例如:

@Autowired 
private UserDao userDao;

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

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

<?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:tx="http://www.springframework.org/schema/tx" 
       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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 
    <context:annotation-config/> 
    <context:component-scan base-package="com.zxr.manager"> 
        <context:include-filter type="regex" expression=".*DaoImpl"/> 
        <context:include-filter type="regex" expression=".*ServiceImpl"/> 
    </context:component-scan> 
</beans>

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

转载于:https://my.oschina.net/u/865921/blog/134250

解释@Component @Controller @Service @Repository相关推荐

  1. spring自动扫描的注解@Component @Controller @Service @Repository

    @Component @Controller @Service @Repository的作用 1.@controller 控制器(注入服务) 2.@service 服务(注入dao) 3.@repos ...

  2. @Controller,@Service,@Repository,@Component详解

    转载自 @Controller,@Service,@Repository,@Component详解 @Controller 用来表示一个web控制层bean,如SpringMvc中的控制器. @Ser ...

  3. r中rep_Spring中@ Component,@ Service,@ Controller和@Repository之间的区别

    r中rep 在了解Spring框架中@Repository @Component , @Service @Controller , @Repository @Controller和@Repositor ...

  4. Spring中@ Component,@ Service,@ Controller和@Repository之间的区别

    在了解Spring框架中@Repository @Component , @Service @Controller , @Repository @Controller和@Repository批注之间的 ...

  5. @service注解_Spring 中 @Component、@Service 等注解如何被解析的

    作者:温安适原文:https://juejin.im/post/6844904199688306702#heading-5 前言 @Component和@Service都是工作中常用的注解,Sprin ...

  6. Spring 中 @Component、@Service 等注解如何被解析?

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 来源 | http://8rr.co/EjqL 前言 @ ...

  7. 面试官:注解@Component,@Service是如何被解析的?

    点击上方 好好学java ,选择 星标 公众号重磅资讯,干货,第一时间送达 今日推荐:推荐19个github超牛逼项目!个人原创100W +访问量博客:点击前往,查看更多 来源: https://my ...

  8. @service注解_Spring 中 @Component、@Service 等注解如何被解析?

    前言 @Component和@Service都是工作中常用的注解,Spring如何解析? 1.@Component解析流程 找入口 Spring Framework2.0开始,引入可扩展的XML编程机 ...

  9. @bean注解和@component注解的区别_通过源码查看 @Component,@Service 等注解是如何被解析的...

    点击上方"匠心零度",选择"设为星标" 做积极的人,而不是积极废人 来源:my.oschina.net/floor/blog/4325651 前言 1.@Com ...

最新文章

  1. js请求结果拦截机器_js利用jquery的jsonp来解决ajax跨域请求被浏览器拦截结果的问题...
  2. 编写程序来模拟计算机LRU算法,操作系统-页式虚拟存储管理程序模拟.doc
  3. 2字段添加注释_Tableau学习系列(7):计算字段
  4. Python解决 Cannot uninstall 'pyparsing' 问题
  5. mysql之创建数据库,创建数据表
  6. android5.0+art模式,Android 5.0有哪些变化
  7. thinkphp5每周学习总结 1026
  8. 乘法分配律逆运算是什么意思_聚类,我们先操弄一下Kmeans,看看什么是非监督学习...
  9. 国际项目投标那些事(六)投标文件怎么写才能惊呆业主 WTSolutions
  10. matlab 16qam误码率图,16qam理论误码率与实际误码率matlab仿真程序.docx
  11. matlab 数字波束合成,dbf数字波束合成
  12. 直播短视频源码如何编写uni-app登录功能界面
  13. 得物购买截图生成_这应该是目前为止,iPhone上最好的长截图工具!
  14. 苹果wifi网速慢怎么办_技巧|iPhone 连接 Wi-Fi 网速慢解决方法
  15. js设计模式 -- 单例模式
  16. 计算机上的字体太小怎么办,电脑字体太小怎么调 电脑字体调整方法有哪些
  17. 10种方法缓解所显示器造成的眼睛疲劳
  18. 简单的WOW DKP系统
  19. 读书笔记--项亮《推荐系统实践》第四章
  20. Mapbox使用详解

热门文章

  1. JAVA面试题系列:如何解决Redis的并发竞争问题
  2. JSP_运维_JSP项目部署到server(适合0经验新手)
  3. js老生常谈之this,constructor ,prototype
  4. windows下一个,OracleServiceXXX和Oracle 关系实例
  5. struct2(四)编写Struct2 的Action
  6. App 运营的指标具体都有哪些?(二)
  7. 七彩岁月【我与51CTO一“七”成长】
  8. GdiPlus[44]: IGPGraphics (三) 关于文本绘制续 - IGPStringFormat
  9. 企业安全挑战 内网安全技术十大策略
  10. SQL Server误区30日谈-Day21-数据损坏可以通过重启SQL Server来修复