spring中的注解都必须在配置文件中进行如下的配置:

<context:component-scan base-package="com.shanjin.oxm.service.impl,com.shanjin.oxm,com.shanjin.interceptorh,com.shanjin.oxm.service" />

1、@Component

@Component
是所有受Spring 管理组件的通用形式,@Component注解可以放在类的头上,@Component不推荐使用。

2、@Controller

@Controller对应表现层的Bean,也就是Action,例如:

3、@ Service

@Service对应的是业务层Bean,例如:

@Service("elasticsearchService")注解是告诉Spring,当Spring要创建elasticsearchServiceImpl的的实例时,bean的名字必须叫做"elasticsearchService",这样当Action需要使用elasticsearchServiceImpl的的实例时,就可以由Spring创建好的"elasticsearchService",然后注入给Action:在Action只需要声明一个名字叫“elasticsearchService”的变量来接收由Spring注入的"elasticsearchServicee"即可

4、@ Repository

@Repository对应数据访问层Bean

/**
 * @author 何婷婷
 *
 */
@Repository("requireDao")
public interface RequireDao {
    /**
     * 获取索引文档的信息
     * @return
     */
public List<Require> getAllNeed();
/**
 * 获取附近的订单用户
 * @return
 */
public List<UserEntity> getAllUser();
}

@Repository(value="requireDao")注解是告诉Spring,让Spring创建一个名字叫“requireDao”的requireDaoImpl实例。 当Service需要使用Spring创建的名字叫“requireDao”的requireDaoImpl实例时,就可以使用@Resource(name = "requireDao")注解告诉Spring,Spring把创建好的requireDao注入给Service即可

5. Spring中为了减少xml中配置,可以生命一个配置类(例如SpringConfig)来对bean进行配置。

//@Configuration
public class Connection {
    
//      此种通过spring方法容易出现链接异常,抛出read time out 错误,而找不到任何的错误
//    public @Bean HttpClientConfig httpClientConfig() {  
//
//        String connectionUrl = "http://192.168.1.60:9200";  
//        String username="shanjin";
//        String passwd="shanjin";
////        try {
////            Properties prop = PropertiesLoaderUtils.loadAllProperties("elasticsearch.properties");
//////            connectionUrl = StringUtil.null2Str(prop.get("elasticsearch.address"));
////            if (connectionUrl==""){
////                connectionUrl="http://192.168.1.60:9200";
////            }
////            username = StringUtil.null2Str(prop.get("username"));
////            passwd = StringUtil.null2Str(prop.get("password"));
////
////        } catch (Exception e) {
////            System.out.println(e.getMessage());
////        }        
//        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder(connectionUrl).defaultCredentials(username, passwd)
//                .connTimeout(Integer.MAX_VALUE)
//                .readTimeout(Integer.MAX_VALUE).multiThreaded(true).build();  
//        return httpClientConfig;  
//    }

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

  1. (转)Spring常见注解总结

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

  2. Spring MVC常用注解

    cp by http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由Di ...

  3. 利用spring AOP 和注解实现方法中查cache-我们到底能走多远系列(46)

    主题:这份代码是开发中常见的代码,查询数据库某个主表的数据,为了提高性能,做一次缓存,每次调用时先拿缓存数据,有则直接返回,没有才向数据库查数据,降低数据库压力. public Merchant lo ...

  4. spring mvc 基于注解的使用总结

    本文转自http://blog.csdn.net/lufeng20/article/details/7598801 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sprin ...

  5. 朱晔和你聊Spring系列S1E9:聊聊Spring的那些注解

    本文我们来梳理一下Spring的那些注解,如下图所示,大概从几方面列出了Spring的一些注解: 如果此图看不清楚也没事,请运行下面的代码输出所有的结果. Spring目前的趋势是使用注解结合Java ...

  6. Spring是如何处理注解的

    如果你看到了注解,那么一定有什么代码在什么地方处理了它. Alan Hohn 我教Java课程时强调的一点是注解是惰性的.换句话说,它们只是标记,可能具有某些属性,但没有自己的行为.因此,每当你在一段 ...

  7. spring : springmvc常用注解标签详解(转)

    新的项目,新的学习,好久没用这些注解了,同时在学习使用shiro ,lucene 等等.在网上找了些博文,感谢作者的总结和分享. 欢迎交流,言归正传: 1.@Controller 在SpringMVC ...

  8. Java面试题集(七)--Spring常见面试问题【重要】

    以下为spring常见面试问题: 1.什么是Spring框架?Spring框架有哪些主要模块? Spring框架是一个为Java应用程序的开发提供了综合.广泛的基础性支持的Java平台. Spring ...

  9. Java中常见的注解

    Java中常见的注解 1.JDK自带的注解@Override  @Deprecated  @Suppvisewarnings 常见第三方注解 Spring:@Autowired  @Service  ...

随机推荐

  1. SPOJ:Eagle and Dogs(求树上每个点最远可以走到哪里---树的直径||DP)

    Eagle (AKA Mohamed Ahmed) lives in a city consists of n intersections connected by n-1 roads, in a w ...

  2. 「LuoguP3381」【模板】最小费用最大流

    Description 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. Input 第一行包含四个正整数N.M.S.T,分别表 ...

  3. bzoj1207 [HNOI2004]打鼹鼠——LIS

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1207 这题和求LIS有点像,打这一只鼹鼠一定可以从打上一只鼹鼠转移过来: 所以不用考虑机器人 ...

  4. Cocos2d-X对常用Object-C特性的替换

    平台的转换,总是让我们不自觉的去寻找两者的相同处,不过Cocos2d-X的确对很多Object-C的特性进行了模仿性质的封装,使熟悉Object-C的人能够在其中看到很多类似的概念而感到亲切.     ...

  5. ASP.NET Core:目录

    ylbtech-ASP.NET Core:目录 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http:// ...

  6. VC链接hid.lib出错问题解决方案

    写一个HID的客户端小程序,调用了一些HID API,但是链接时出现了奇怪的现象. 尽管我已经把头文件和lib文件加入到了VC的Include和Lib目录中,但不管我用VC链接,还是在DDK环境下链接 ...

  7. Streamline Your App with Design Patterns 用设计模式精简你的应用程序

    Back to Design Patterns Streamline Your App with Design Patterns 用设计模式精简你的应用程序 In Objective-C progra ...

  8. CodeForces 712A Memory and Crow (水题)

    题意:有一个序列,然后对每一个进行ai = bi - bi + 1 + bi + 2 - bi + 3.... 的操作,最后得到了a 序列,给定 a 序列,求原序列. 析:很容易看出来,bi = ai ...

  9. Thrift 使用TNonblockingServer模型时调用PosixThreadFactory出错。

    Thrift 使用TNonblockingServer模型时调用PosixThreadFactory出错.   我定位到shared_ptr<PosixThreadFactory> thr ...

  10. bzoj 1385: [Baltic2000]Division expression【脑洞】

    加括号再去括号就是除变加,显然尽可能多的除变加是最优的,然后发现唯一不能变成乘数的是第二个数,所以把其他数乘起来mod第二个数,如果是0就是YES,否则说明最后不能除尽,就是NO #include&l ...