spring 常见的注解
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 常见的注解的更多相关文章
- (转)Spring常见注解总结
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文 ...
- Spring MVC常用注解
cp by http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由Di ...
- 利用spring AOP 和注解实现方法中查cache-我们到底能走多远系列(46)
主题:这份代码是开发中常见的代码,查询数据库某个主表的数据,为了提高性能,做一次缓存,每次调用时先拿缓存数据,有则直接返回,没有才向数据库查数据,降低数据库压力. public Merchant lo ...
- spring mvc 基于注解的使用总结
本文转自http://blog.csdn.net/lufeng20/article/details/7598801 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sprin ...
- 朱晔和你聊Spring系列S1E9:聊聊Spring的那些注解
本文我们来梳理一下Spring的那些注解,如下图所示,大概从几方面列出了Spring的一些注解: 如果此图看不清楚也没事,请运行下面的代码输出所有的结果. Spring目前的趋势是使用注解结合Java ...
- Spring是如何处理注解的
如果你看到了注解,那么一定有什么代码在什么地方处理了它. Alan Hohn 我教Java课程时强调的一点是注解是惰性的.换句话说,它们只是标记,可能具有某些属性,但没有自己的行为.因此,每当你在一段 ...
- spring : springmvc常用注解标签详解(转)
新的项目,新的学习,好久没用这些注解了,同时在学习使用shiro ,lucene 等等.在网上找了些博文,感谢作者的总结和分享. 欢迎交流,言归正传: 1.@Controller 在SpringMVC ...
- Java面试题集(七)--Spring常见面试问题【重要】
以下为spring常见面试问题: 1.什么是Spring框架?Spring框架有哪些主要模块? Spring框架是一个为Java应用程序的开发提供了综合.广泛的基础性支持的Java平台. Spring ...
- Java中常见的注解
Java中常见的注解 1.JDK自带的注解@Override @Deprecated @Suppvisewarnings 常见第三方注解 Spring:@Autowired @Service ...
随机推荐
- hdu-5719 Arrange(组合数学)
题目链接: Arrange Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...
- ubuntu 源、codename 与 sources.list 文件
查看 codename $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubunt ...
- Laravel 新增的Switch模板控制语句非常不错
切换语句switch语句可以使用来构建,,,和指令:@switch@case@break@default@endswitch @switch($i) @case(1) First case... @b ...
- Code-zabbix:zabbix-3.4-快速入门
ylbtech-Code-zabbix:zabbix-3.4-快速入门 1.返回顶部 1. 1 登陆和配置用户 登陆Zabbix,以及在Zabbix内建立一个系统用户. 用户名:Admin 或者 ad ...
- 如何用JavaScript实现获取验证码的效果
转自:http://www.php.cn/js-tutorial-411734.html HTML部分: 1 2 3 4 5 6 7 <body onload='createCode()'> ...
- kubeadm安装Kubernetes13.1集群-三
环境: master: 192.168.3.100 node01: 192.168.3.101 node02: 192.168.3.102 关闭所有主机防火墙,selinux: 配置主机互信: mas ...
- E20180404-ts
aero adj. 航空的,飞行的,飞机的; wheel n. 轮子; 旋转; Wheels 汽车; 有…轮子的汽车(或自行车等); vi. 转动,旋转; 突然转变方向; foil n. 箔,金 ...
- PTA 2-1 列出连通集【DFS+BFS基础】
给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点. 输入格式: 输入第1 ...
- 纯JS阻止浏览器默认滚动事件,实现自定义滚动方法
首先该方法兼容IE7+以上浏览器,可以实现页面上下滚动,而且也可以实现页面左右滚动,每次滚动的距离为屏幕的大小,滚动为加速滚动 javaScript代码如下: //滚动实现方法,使用鼠标滚轮每次滚动浏 ...
- 统计Apache或nginx日志里访问次数最多的前十个IP
1.根据访问IP统计UV awk '{print $1}' access.log|sort | uniq -c |wc -l 2.统计访问URL统计PV awk '{print $7}' access ...