Spring 常用的注解
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 常用的注解的更多相关文章
- spring常用的注解
一.使用注解之前要开启自动扫描功能,其中base-package为需要扫描的包(含子包). <context:component-scan base-package="cn.test& ...
- 关于Spring常用的注解
参考文献:http://www.cnblogs.com/xdp-gacl/p/3495887.html 使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationC ...
- Spring 常用注入注解(annotation)和其对应xml标签
使用注解需要修改bean.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...
- spring ,springmvc的常用标签注解
一:spring常用的注解: @Configuration把一个类作为一个IoC容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean.@Scope注解 作用域@Lazy ...
- Spring系列之Spring常用注解总结
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...
- 【SSM 2】spring常用注解
声明:以下观点,纯依据个人目前的经验和理解,有不当之处,多指教! 一.基本概述 注解(Annotation):也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举 ...
- Spring MVC常用的注解类
一.注解类配置 要使用springmvc的注解类,需要在springmvc.xml配置文件中用context:component-scan/扫描:  二.五大重要的注解类 1.RequestMapp ...
- Spring常用注解介绍【经典总结】
Spring的一个核心功能是IOC,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用Spring注解方式或者Spring XML配置方式. Spring注解方式减少了配置文件内容 ...
- Spring常用注解总结
转载自:https://www.cnblogs.com/xiaoxi/p/5935009.html 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点 ...
随机推荐
- [USACO07MAR]黄金阵容均衡Gold Balanced L…
https://www.luogu.org/problem/show?pid=1360 题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many s ...
- 2017北京国庆刷题Day6 afternoon
期望得分:100+100+40=240 实际得分:100+0+40=140 二进制拆分.二进制前缀和 #include<cstdio> #include<iostream> u ...
- Unicode/UTF-8/GBK/ASCII 编码简介
转载:http://blog.csdn.net/u014785687/article/details/73928167 一.字符编码简介 1.ASCII编码 每一个ASCII码与一个8位(bit)二进 ...
- 使用纯 CSS 实现响应式的图片显示效果
有许多方法可以实现页面里图像的响应式显示(Responsive).然而,我碰到的所有方案都使用了JavaScript.这使我疑惑不用 JavaScript 实现图像响应是否可行. 我提出了下面纯CSS ...
- 说一说ASP.NET web.config 加密及解密方法 (代码)
/// <summary> /// 保护web.config的加密和解密 /// </summary> public class ProtectHelper { /// < ...
- 【洛谷 P4291】 [HAOI2008]排名系统(Splay,Trie)
题目链接 不是双倍经验我会去\(debug\)一上午? 一开始我是用的\(map+string\),跑的太慢了,T了4个点. 后来我手写了\(string\),重载了小于号,依然用的\(map\),T ...
- 机器学习-kNN-寻找最好的超参数
一 .超参数和模型参数 超参数:在算法运行前需要决定的参数 模型参数:算法运行过程中学习的参数 - kNN算法没有模型参数- kNN算法中的k是典型的超参数 寻找好的超参数 领域知识 经验数值 实验搜 ...
- centos6.5 导入matplotlib报错 No module named '_tkinter
1.解决方案 在centos系统下,导入matplotlib时,出现ImportError: No module named ‘_tkinter’的错误,首先 yum list installed | ...
- linux ip白名单、防火墙白名单 设置
http://blog.csdn.net/catoop/article/details/50476099 登录信息在 /var/log/secure linux ip白名单 配置文件:/etc/hos ...
- curl: (6) Couldn’t resolve host ‘www.ttlsa.com’【转】
上周, 部分站点出现Couldn't resolve host.....问题, 导致公司所有走api的程序都无法正常使用(系统redhat 6.3的都出现问题, redhat 5一切OK). 最后解 ...