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.事物,这么做有两个缺点 ...
随机推荐
- 转:数据标准化/归一化normalization
转自:数据标准化/归一化normalization 这里主要讲连续型特征归一化的常用方法.离散参考[数据预处理:独热编码(One-Hot Encoding)]. 基础知识参考: [均值.方差与协方差矩 ...
- 2-sat HDU 1814
题解来自于:http://www.cnblogs.com/kuangbin/archive/2012/10/05/2712622.html 和平委员会 根据宪法,Byteland民主共和国的公众和平委 ...
- CSS预处理器们
CSS预处理器有很多,最早的是2006年的Less,到后来2010年的SASS,还有现在也很出名的Stylus.不过要使用它们都要使用一些工具,比如Less的话要使用Grunt或者Gulp或者Node ...
- 关于Java泛型深入理解小总结
1.何为泛型 首先泛型的本质便是类型参数化,通俗的说就是用一个变量来表示类型,这个类型可以是String,Integer等等不确定,表明可接受的类型,原理类似如下代码 int pattern; //声 ...
- 使用Skyworking 作全链路api调用监控,Integration of Skyworking, auditing the whole chain circuit.
Applicable scenario: Structure Map ~ Skywalking uses elasticsearch to store data, don't mistake elas ...
- 【CF802C】 Heidi and Library (hard)(费用流)
题目链接 感觉跟餐巾计划问题有点像.费用流. 决定每天买不买不太好搞,不如先把所有东西都买进来,再卖掉不必要的. 拆点,每个点拆成\(x,y\). 源点向每个点的\(x\)连费用为当天的价格,流量为1 ...
- 使用wifite破解路由器密码
使用wifite破解路由器密码 发表于 2016-02-06 | 分类于 wifite | 暂无评论 | 10次阅读 简介 wifite是一款自动化wep.wpa破解工具,不支持w ...
- perl6正则 2: 字母,数字,空格,下划线, 字符集
数字, 字母, 下划线 在perl6中, 如果是 数字, 字母, 下划线, 在正则里可以正接写上. > so / True > so 'perl6_' ~~ /_/ True > 非 ...
- LAMP网站架构解释
对于大流量.大并发量的网站系统架构来说,除了硬件上使用高 性能的服务器.负载均衡.CDN等之外,在软件架构上需要重点关注下面几个环节:使用高性能的操作系统(OS).高性能的网页服务器(Web Serv ...
- angular项目中使用jquery的问题
1.使用npm命令往项目中添加jQuery. npm install jquery --save 2.在你想要用jQuery的组件中添加. import * as $ from "jquer ...