@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容器中去了。

//TODO

spring中@注解的相关解释的更多相关文章

  1. spring中注解的通俗解释

    我们在没有用注解写spring配置文件的时候,会在spring配置文件中定义Dao层的bean,这样我们在service层中,写setDao方法,就可以直接通过接口调用Dao层,用了注解写法后,在配置 ...

  2. spring中注解式事务不生效的问题

    常用的解决方法可以百度,我针对我的问题描述一下 Mysql中InnoDB引擎才支持事务, MyISAM不支持事务. 当你尝试了各种方法解决spring中注解式事务不生效时, 一定要查看一下数据库中表的 ...

  3. Spring中注解的使用详解

    一:@Rsource注解的使用规则 1.1.案例演示 Spring的主配置文件:applicationContext.xml(因为我这里将会讲到很多模块,所以我用一个主配置文件去加载各个模块的配置文件 ...

  4. spring中注解的实现原理

    @Autowired和@Resource的区别: 在Java中使用@Autowired和@Resource注解进行装配,这两个注解分别是:1.@Autowired按照默认类型(类名称)装配依赖对象,默 ...

  5. Spring中注解大全和应用

    @Controller@RestController:@Service@Autowired@RequestMapping@RequestParam@ModelAttribute@Cacheable@C ...

  6. Spring中注解事务方面的问题

    我们可以在spring的配置文件beans.xml中对事务进行注解配置,这样在相应的类中就不用对事务进行管事,对于某个类,想单独交给spring来管理,那么就在相应的类上加入@Transactiona ...

  7. spring中注解事务认识

    1.配置事务管理器 <!-- 设定transactionManager事务管理器 --> <bean id="txManager" class="org ...

  8. spring中注解注入 context:component-scan 的使用说明

    通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个标签,配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的java文件,如果扫描 ...

  9. Spring中注解

    @Autowired :spring注解 @Resource :J2EE注解 @Transactional(rollbackFor=Exception.class):指定回滚 @RequestMapp ...

随机推荐

  1. Netty 如何实现心跳机制与断线重连?

    作者:sprinkle_liz www.jianshu.com/p/1a28e48edd92 心跳机制 何为心跳 所谓心跳, 即在 TCP 长连接中, 客户端和服务器之间定期发送的一种特殊的数据包, ...

  2. vue证明题一,vue全家桶的构成

    简单说下vue的构成,当然是简单为主,网上这东西满天飞,简单说几句就ok 1.vue是什么 vue读作view,是一种js框架.只关注于视图层,操作内容包括js,html,css 2.vue全家桶是什 ...

  3. microtime函数用法

    /** * microtime函数 * 返回:如果参数为空,则返回字符串 "微妙部分(单位:秒) 秒",字符串的两部分都是以"秒"为单位返回的 * 如果参数为 ...

  4. c# 编程--方法(函数)

    方法(函数) 能够独立完成某项功能的模块    函数的四要素:函数名.输入.输出.函数体    函数定义.函数的调用 函数就是将一堆代码进行重用的一种机制,函数就是一段代码,这段代码可能有输入的值(参 ...

  5. SHOW - 显示运行时参数的数值

    SYNOPSIS SHOW name SHOW ALL DESCRIPTION 描述 SHOW 将显示当前运行时参数的数值. 这些变量可以通过 SET 语句来设置,或者通过编辑 postgresql. ...

  6. Win32 API 多线程编程——一个简单实例(含消息参数传递)

    Win32 API进行程序设计具有很多优点:应用程序执行代码小,运行效率高,但是他要求程序员编写的代码较多,且需要管理所有系统提供给程序的资源,要求程序员对Windows系统内核有一定的了解,会占用程 ...

  7. go语言从例子开始之Example13.函数多返回值

    Go 内建多返回值 支持.这个特性在 Go 语言中经常被用到,例如用来同时返回一个函数的结果和错误信息. Example: package main import "fmt" // ...

  8. vue 项目中使用阿里巴巴矢量图标库iconfont

    原文:https://www.jianshu.com/p/38262f18eee2 1.打开iconfont阿里巴巴官网https://www.iconfont.cn 2.新建项目(这样方便后期维护图 ...

  9. Requests使用

    Requests 简介 Requests库 requests是一个很实用的Python HTTP客户端库,编写爬虫和测试服务器响应数据时经常会用到.可以说,**Requests 完全满足如今网络的需求 ...

  10. MariaDB 创建表

    在本章中,我们将学习如何创建表. 在创建表之前,首先确定其名称,字段名称和字段定义. 以下是表创建的一般语法: CREATE TABLE table_name (column_name column_ ...