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. redis服务启动脚本

    /etc/rc.d/init.d/redis #!/bin/sh# chkconfig: 2345 80 90 # description: Start and Stop redis REDISPOR ...

  2. CSS常用标签-手打抄录-感谢原未知博主-拜谢了

    CSS常用标签   CSS常用标签 一 CSS文字属性 color : #999999; /*文字颜色*/ font-family : 宋体,sans-serif; /*文字字体*/ font-siz ...

  3. sublime wrong

    Q1: sublime报错: There are no packages available for installation A1: window下的:C:\Windows\System32\dri ...

  4. cin.getline()与getline()

    C++中有两个getline函数, cin.getline()与getline()  这两个函数相似,但是 这两个函数分别定义在不同的头文件中.   cin.getline()属于istream流,而 ...

  5. Web中的宽和高

    不同的宽高定义 //网页可见区域宽 document.body.clientWidth //网页可见区域高 document.body.clientHeight //网页可见区域宽(包括边线和滚动条的 ...

  6. Stat2—主成分分析(Principal components analysis)

    最近在猛撸<R in nutshell>这本课,统计部分涉及的第一个分析数据的方法便是PCA!因此,今天打算好好梳理一下,涉及主城分析法的理论以及R实现!come on…gogogo… 首 ...

  7. 20155117王震宇 2016-2017-2 《Java程序设计》第十周学习总结

    教材学习内容总结 Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd) 第22章 网络 {{屏幕快照 2017-04-30 下午8.38.06.pn ...

  8. [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)

    [POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...

  9. docker 升级后,配置 idea 连接 docker

    [root@A01-R02-I188-87 ~]# docker version Client: Version: 18.06.1-ce API version: 1.24 Go version: g ...

  10. 使用showplan.sql分析sql Performance

    在HelloDBA网站找到一个分析sql性能的工具-showplan,记录一下 showplan.sql下载路径:http://www.HelloDBA.com/Download/showplan.z ...