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. JS事件大全及兼容

    一般事件 事件 浏览器支持 描述 onClick IE3|N2|O3 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击 onDblClick IE4|N4|O 鼠标双击事件 onMouseDown  ...

  2. KMP next表模板

    void makeNext(const char P[],int next[]) { int q,k;//q:模版字符串下标:k:最大前后缀长度 int m = strlen(P);//模版字符串长度 ...

  3. Spring Boot 使用IntelliJ IDEA创建一个web开发实例(一)

    .新建项目File-->New-->Project-->Spring Initializr 点击Finish,一个Spring Boot web应用就创建好了.

  4. ⑥ 设计模式的艺术-06.建造者(Builder)模式

    场景 我们要建造一个复杂的产品.比如:神州飞船,Iphone.这个复杂的产品的创建.有这样一个问题需要处理: 装配这些子组件是不是有个步骤问题? 实际开发中,我们所需要的对象构建时,也非常复杂,有很多 ...

  5. 解决gridview row 左边序列号 显示不完全的技巧

    放在主程序 入口处, public Form1() { InitializeComponent(); gridView1.IndicatorWidth = ; //<宽度值>官方推荐常用是 ...

  6. 【Foreign】Melancholy [线段树]

    Melancholy Time Limit: 10 Sec  Memory Limit: 256 MB Description DX3906星系,Melancholy星上,我在勘测这里的地质情况. 我 ...

  7. 垂直水平居中--css3

    在移动前端制作中,很多新的css3特性能够帮助我们更好的制作.例如这个垂直水平居中问题,就有一个简单的代码可以解决: 利用CSS3的transform:translate .center{ width ...

  8. python初步学习-python函数(一)

    python 函数 函数是组织好的,可重复使用的,用来实现单一或者相关联功能的代码段. 函数能提高应用的模块性和代码的重复利用率. 函数定义 python中函数定义有一些简单的规则: 函数代码块以de ...

  9. Vue笔记之props验证

    使用props 在Vue中父组件向子组件中传送数据是通过props实现的,一个简单的使用props的例子: <!DOCTYPE html> <html> <head> ...

  10. javascript中的数组去重

    1.方法一:双层循环,外层循环元素,内层循环做比较,若相同则跳过,不同则加入结果集中,获取没重复的最右侧的值放入数组中 Array.prototype.distinct = function(){ v ...