Spring 常用的注解

前言

最近才体会到Spring注解配置的遍历,总结一下。

SpringMVC配置

@Configuration
@EnableWebMvc
@ComponentScan("cn.fjhdtp.maventest.controller")
public class SpringMvcConfig {
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
internalResourceViewResolver.setViewClass(JstlView.class);
internalResourceViewResolver.setPrefix("/WEB-INF/views/");
internalResourceViewResolver.setSuffix(".jsp");
return internalResourceViewResolver;
}
}

@Configuration表明这是一个配置类;@EnableWebMvc启用SpringMVC。

web配置

public class WebInitializer implements WebApplicationInitializer{
public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
ctx.setServletContext(servletContext);
ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}

实现__WebApplicationInitializer__的类的__onStartup__方法会在Spring启动之后被执行,并且这个优先级在listener之前。可以用@Order(100)注解来配置执行的顺序,没有这个注解则代表是最先执行。

@ComponentScan

类似<context:component-scan />

@ComponentScan(value = "cn.fjhdtp.maventest", excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Configuration.class, Controller.class}),
})

@PropertySource

@Configuration结合使用,读取properties文件

@PropertySources

@PropertySources({
@PropertySource(value = "classpath:jedis.properties")
})

@Value

@PropertySource或者<context:property-placeholder/>结合使用,注入属性值。

@Value("${redis.hostName:127.0.0.1}")
private String hostName; // 主机名
@Value("${redis.port:6379}")
private int port; // 监听端口
@Value("${redis.auth:}")
private String password; // 密码
@Value("${redis.timeout:2000}")
private int timeout; // 客户端连接时的超时时间(单位为秒)

@Controller

@Controller表明这个类是一个controller,和@RequestMapping结合来配置映射的路径。

@Component

@Component表明这是一个Bean,但是如果知道属于那一层最好还是用@Service或者@Repository

@Service

用在Service层。

@Repository

用在Dao层。

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

  1. spring常用的注解

    一.使用注解之前要开启自动扫描功能,其中base-package为需要扫描的包(含子包). <context:component-scan base-package="cn.test& ...

  2. 关于Spring常用的注解

    参考文献:http://www.cnblogs.com/xdp-gacl/p/3495887.html 使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationC ...

  3. Spring 常用注入注解(annotation)和其对应xml标签

    使用注解需要修改bean.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...

  4. spring ,springmvc的常用标签注解

    一:spring常用的注解: @Configuration把一个类作为一个IoC容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean.@Scope注解 作用域@Lazy ...

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

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

  6. 【SSM 2】spring常用注解

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

  7. Spring MVC常用的注解类

    一.注解类配置 要使用springmvc的注解类,需要在springmvc.xml配置文件中用context:component-scan/扫描:  二.五大重要的注解类 1.RequestMapp ...

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

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

  9. Spring常用注解总结

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

随机推荐

  1. centos7 mysql5.7.17源码安装

    **安装前准备 操作系统环境:Centos 7.2 1.解决依赖包并下载源码包至/home/soft/目录下 1 2 3 4 5 6 7 [root@node03 ~]# yum -y install ...

  2. ④ 设计模式的艺术-10.装饰(Decorator)模式

    职责 装饰模式是在不必改变原类文件和使用继承的情况下,动态的扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 装饰模式是一种用于代替继承的技术,无需通过继承增加子类就能扩展对 ...

  3. 关于jQuery UI 使用心得及技巧

    1 jQuery UI 有时你仅仅是为了实现一个渐变的动画效果而不得不把javascrip 重新学习一遍然后书写大量代码.直到jQuery的出现,让开发人员从一大堆繁琐的js代码中解脱,取而代之几行j ...

  4. 微信小程序迁移到头条小程序工具

    最近公司需要将微信小程序迁移到头条小程序,比较得知微信和头条小程序的写法类似,只有文件名称不同,相关的指令不同,以及头条在ttml绑定的数据不可使用function,于是就写了node脚本来实现这些重 ...

  5. Vue 传递

    今天刷了一遍Vue的API,做个小笔记 父子传递数据时,父组件里标记要传的数据,子组件里用props获取,子组件用$emit('func',args)发布事件,父组件用@func接收. 方法一 par ...

  6. bzoj 2741 可持久化trie

    首先我们设si为前i个数的xor和,那么对于询问区间[i,j]的xor和,就相当于si-1^sj,那么对于这道题的询问我们可以处理处si,然后对于询问[l,r],可以表示为在区间[l-1,r]里找两个 ...

  7. UML相关说明

    在java开发中,有很多时候我们是在不断的处理类与类之间关系,其中这六种关系是:依赖.关联.聚合.组合.继承.实现. 它们的强弱关系是没有异议的:依赖 < 关联 < 聚合 < 组合& ...

  8. 【Eclipse】Elipse自定义library库并导入项目

    1.定义像JRE System Library之类的库 (1)点击UserLibrary (2)如果没有就点击new新建一个user library,否则进行4  (3)向user library添加 ...

  9. thinkphp中的验证器

  10. nc-使用方法

    nc-远程克隆硬盘 A 接收端:  nc -lp 333 | dd of=/dev/sda          #用nc开启333监听端口  将收到的数据 写入到sda的硬盘上(前提是挂一块硬盘) B ...