使用注解构造IOC、替代传统的applicationContext.xml配置<bean/>和<property/>

传统的spring要在applicationContext.xml中配置:①<bean>类   ②<property>属性   如果有100个类和若干个属性,那么我们就要写成百上千个<bean>、<property>,这种就不利于维护,使用注解就能够解决这个问题。(项目中的方法也可以使用注解来替代)

在使用类注解前要在applicationContext.xml文件中添加:

<context:component-scan base-package="news"></context:component-scan>

注意:base-package可以指定一个包也可指定多个包,多个包逗号隔开

使用了<context:component-scan/>之后就可以把<context:annotation-config/>移除,因为<context:component-scan/>也能实现扫描包路径。

1.(类)@Controller

@Controller是对应控制层的<bean>,也就是action类

用法:

1 @Controller
2 @Scope("prototype")
3 public class NewsAction extends ActionSupport{
4 ……
5 }

注意:如果@Controller不指定其value【@Controller】,则默认的bean名字为这个类的类名首字母小写,如果指定value【@Controller(value="UserAction")】或者【@Controller("UserAction")】,则使用value作为bean的名字。

2、(类)@ Service

@Service对应的是业务层Bean

用法:

1 @Service("NewsService")
2 public class NewsServiceImpl implements NewsService {
3 ………
4 }

3、(类)@ Repository

@Repository对应数据访问层Bean,部分技术人员也喜欢叫dao类

用法

1 @Repository(value="NewsDao")
2 public class NewsDaoImpl extends NewsDaoImpl {
3 ………
4 }

注意:以上三种(@Controller、@Service、@Repository)是针对不同层的类所使用的注解,下面的是针对属性所使用的注解:

4.(属性)@ Autowired (不推荐使用,建议使用@Resource)

@Qualifier("...")

用法(在实现类中封装一个属性并私有化,在属性上面添加@Autowired @Qualifier)

如下:

public class NewsDaoImpl implements NewsDao {
   //这种方式是spring注解
   @Autowired
@Qualifier("mySessionFactory")
private SessionFactory sf;
}

 注意:加入@Qualifier是为了让spring准确地知道要注入的对象,括号里自定义属性名字

5.(属性)@Resource(jdk)

用法:

public class NewsDaoImpl implements NewsDao {
@Resource(name="mySessionFactory")
private SessionFactory sf;
}

通过@Resource注解来给属性sf注入一个名字为mySessionFactory的值

spring常用注解的更多相关文章

  1. Spring系列之Spring常用注解总结

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  2. Spring常用注解介绍【经典总结】

    Spring的一个核心功能是IOC,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用Spring注解方式或者Spring XML配置方式. Spring注解方式减少了配置文件内容 ...

  3. Spring常用注解总结

    转载自:https://www.cnblogs.com/xiaoxi/p/5935009.html 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点 ...

  4. Spring注解 系列之Spring常用注解总结

    参考:Spring系列之Spring常用注解总结 (1) Resource 默认是byName的方式进行bean配置,@AutoWired默认是按照byType的方式进行装配bean的:(2)Comp ...

  5. Spring系列之Spring常用注解总结 转载

    Spring系列之Spring常用注解总结   传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.x ...

  6. spring常用注解笔记

    spring常用注解解释: 1. Mybatis的映射文件xxxMapper.xml中resultMap标签的作用 resultMap标签是为了映射select查询出来结果的集合,其主要 作用是将实体 ...

  7. SpringBoot+Spring常用注解总结

    为什么要写这篇文章? 最近看到网上有一篇关于 SpringBoot 常用注解的文章被转载的比较多,我看了文章内容之后属实觉得质量有点低,并且有点会误导没有太多实际使用经验的人(这些人又占据了大多数). ...

  8. Spring常用注解汇总

    本文汇总了Spring的常用注解,以方便大家查询和使用,具体如下: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). <context:component- ...

  9. Spring常用注解,自动扫描装配Bean

    1 引入context命名空间(在Spring的配置文件中),配置文件如下: xmlns:context="http://www.springframework.org/schema/con ...

  10. 【SSM 2】spring常用注解

    声明:以下观点,纯依据个人目前的经验和理解,有不当之处,多指教! 一.基本概述 注解(Annotation):也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举 ...

随机推荐

  1. Python的平凡之路(12)

    一.数据库介绍 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据.我们也可以将数据存储在文件中,但 ...

  2. 一些Layout的坑

    iOS这个东西,初学感觉,还好还好,然后一年之后再来修复一下初学的时候的代码,我只是感觉头很晕- - 别扶我. AutoLayout的坑,明明以前都没有的!!!升了iOS10就突然发现了这个坑,其实也 ...

  3. PYTHON 链接 Oracle

    一.  cx_Oracle Python 连接Oracle 数据库,需要使用cx_Oracle 包. 该包的下载地址:http://cx-Oracle.sourceforge.net/ 下载的时候,注 ...

  4. SQLAutoCode - error when attempting to generate schema

    I'm trying to auto generate a schema for use in SQLalchemy, I'm using sqlautocode to do this, I use ...

  5. 强大反调试cm的奇葩破解

    系统 : Windows xp 程序 : Crackme-xp 程序下载地址 :http://pan.baidu.com/s/1slUwmVr 要求 : 编写注册机 使用工具 : OD & I ...

  6. Oracle监听小问题

    啥情况 宁波_弈风 15:17:40   宁波_弈风 15:17:49 默认配置路径 宁波_弈风 15:18:09   宁波_弈风 15:19:13 我原来一直都在SAMPLE文件里面配置和保存,因为 ...

  7. PHP乱码完美解决

    文章来源  http://www.lupaworld.com/forum.php?mod=viewthread&tid=148807 A.首先说下HTML中文乱码问题的解决方法. 比如有个in ...

  8. [.NET] CErrStack 方便地管理错误或异常

    Option Explicit On Option Strict On Imports System.Reflection Imports System.Diagnostics Public Stru ...

  9. HTML5新增标签及具体用法

    html5自从推广普及以来,迅速被各大浏览器支持.采用html5设计的网页逐渐成为网页设计的时尚.下面就温习下html5的新增标签. HTML 5 中的新特性包括了嵌入音频.视频和图形的功能,客户端数 ...

  10. iOS 微信分享

    1.注册微信开放平台账号:https://open.weixin.qq.com 2.创建应用 设置图片可以使用一个小工具,详情http://www.cnblogs.com/czq1989/p/5073 ...