Sping--注解(一) 常用注解总结
Sping注解是很重要的一块。今天在这里对常用到的注解做个小结,会尽可能说得详细些。
推荐这个英文文档,官方文档当然是最好的。最近发现,学习东西,还是多看官方文档,最权威,最详细。
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html
1.@Component @Service @Controller @Repository
Component英文名组件,将它放在类的上方,表明要将这个类实例化到Sping容器之中,后面的三个是它的衍生类,后面的三个分工更加明确,可以立马知道这个类的作用。当不好划分这个类的区分时,可以用Component来修饰。
启用注解的方法:①XML文件:<context:component-scan base-package=”XXX.XXX.XXX(包名)”>
②注解的方式:@ComponentScan(basePackages = {"com.lee.demo.environment"})
2.@Configuration @Bean @PropertySource
Configuration 这个注解的意思:配置,你可以理解成资源文件,再说得通俗些,可以当成XML的配置文件。
Bean通常和Configuration搭配使用,Bean可以理解成xml配置文件中的<bean .... >
PropertySource作资源文件用,env.getProperty("bean.name") bean.name 就是在app.properties中定义的。
@Configuration
@PropertySource("classpath:/app.properties")
public class AppConfig { @Autowired
Environment env;
// 用bean修饰时,默认注入的名字就是方法名myBean
@Bean
public MyBean myBean() {
return new MyBean(env.getProperty("bean.name"));
}
}
public class MyBean {
public MyBean(String name) {
System.out.println("my bean name is :" + name);
}
}
1 @Configuration
2 @ComponentScan(basePackages = {"com.lee.demo.environment"})
public class Test { public static void main(String[] args) {
@SuppressWarnings("resource")
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(com.lee.demo.environment.AppConfig.class);
MyBean bean = context.getBean(MyBean.class); }
}
app.properties
bean.name=hello world
最后执行结果:
my bean name is :hello world
3.@Profile
它的作用相当于是分类。比如测试环境时是一套环境的参数设置,开发环境是另一套环境的参数设置,如果单纯的来回替换配置文件很麻烦,所以通过这个注解来解决这个问题。
(这个例子中,我将几个类都贴在一块了。)
public interface SpeakLanguage {
void doSpeak();
}
@Component
public class Person {
@Autowired
private SpeakLanguage speakLangtage;
public void speak() {
speakLangtage.doSpeak();
}
}
@Configuration
//@Profile("default") default的作用是,当没有制动profile的参数时,会默认执行带有default注解的类。
@Profile("dev")
@Component
public class Chinese implements SpeakLanguage {
@Override
public void doSpeak() {
// TODO Auto-generated method stub
System.out.println("I can speak Chinese");
}
}
@Configuration
@Profile("production")
@Component
public class EnglishMan implements SpeakLanguage {
@Override
public void doSpeak() {
// TODO Auto-generated method stub
System.out.println("I can speak English");
}
}
@Configuration
@ComponentScan(basePackages = {"com.lee.demo.environment"})
public class Test {
public static void main(String[] args) {
@SuppressWarnings("resource")
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(com.lee.demo.environment.Test.class);
Person p = context.getBean(Person.class);
p.speak();
}
}
执行的时候,需要指定Profile,指定可以通过设置环境变量(我尝试了,环境变量设置完之后,重启电脑才好用的,不知道为什么),JVM参数(-Dspring.profiles.active=production),servlet上下文参数定义.
执行结果:I can speak English
4.@Autowired @Qualifier @Resource
public class FooService {
// Autowired 自动绑定 分为类型匹配和名称匹配 它是Spring的注解
//Autowired 可以绑定要注册的类,它默认的是按照类型进行匹配,当一个类型有多个实现类,进而无法确定的时候,配合Qualifier指定实现类的名称,来绑定
@Autowired
4 @Qualifier("fooFormatter")
private Formatter formatter;
}
@Service
public class SequenceServiceImpl implements SequenceService {
// 默认是按照名称方式进行Bean的匹配,它是J2EE的注解
@Resource
private SequenceMapper sequenceMapper;
public void generateId(Map<String, String> map) {
sequenceMapper.generateId(map); } @Resource(name = "manImpl")//注意是manImpl不是ManImpl,因为使用@Service,容器为我们创建bean时默认类名首字母小写
private Human human; }
上述的两个代码段没有关系,单纯的拿出来展示注解用。
5.@PostConstruct @PreDestory
这部分我偷懒了,看到一边文章写的非常详细,所以贴出地址:
https://blog.csdn.net/wo541075754/article/details/52174900
Sping--注解(一) 常用注解总结的更多相关文章
- Spring + Mybatis 企业应用实战 第3章 Sping MVC的常用注解
注解(annotation) @Controller @Controller是扩展的@Component的,可以说基本一样,就是作为一种标志. @RequestMapping value: 指 ...
- [Spring学习笔记 3 ] spring 注解详解,完全注解,常用注解
.xml使用注解 xml 用来定义bean的信息,注解用来配置依赖信息 ) 在配置文件中配置bean )在javaBean中用注解来指定依赖注入 )在配置文件中开启注解扫描 @Resource标签 j ...
- Swagger2常用注解和使用方法
一 引入maven依赖 <!--整合Swagger2--> <dependency> <groupId>com.spring4all</groupId&g ...
- Spring系列之Spring常用注解总结
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...
- SpringMVC常用注解實例詳解3:@ResponseBody
我的開發環境框架: springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...
- SpringMVC常用注解實例詳解2:@ModelAttribute
我的開發環境框架: springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...
- Spring常用注解汇总
本文汇总了Spring的常用注解,以方便大家查询和使用,具体如下: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). <context:component- ...
- Spring常用注解,自动扫描装配Bean
1 引入context命名空间(在Spring的配置文件中),配置文件如下: xmlns:context="http://www.springframework.org/schema/con ...
- springmvc常用注解与类型转换
springmvc常用注解与类型转换 一:前置 spring -servlet.xml 注入 <!-- 启用spring mvc 注解 --> <context:annotation ...
- 【SSM 2】spring常用注解
声明:以下观点,纯依据个人目前的经验和理解,有不当之处,多指教! 一.基本概述 注解(Annotation):也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举 ...
随机推荐
- Win10系列:JavaScript图形
在页面中添加canvas元素会在页面上生成一个矩形的位图画布,可以使用JavaScript在画布上实时绘制图形图像.在绘制图形时,需要先调用画布的getContext函数获取与该画布相关的用于绘制图形 ...
- OO作业总结报告3
规格化设计的发展史 下面部分来源:https://www.cnblogs.com/eggert/p/9098446.html: 随着计算机硬件的飞速发展,以及应用复杂度越来越高,软件规模越来越大,原有 ...
- VirtualBox安装CentOS7的网络配置
VirtualBox安装CentOS7的网络配置 这几天在本机VirtualBox安装CentOS时遇到了网络的坑... VirtualBox的下载地址:https://www.virtualbox. ...
- intelij idea常用设置
1.genneral设置 2.自动导包 3.设置显示行号和方法分隔符 4.忽略大小写提示代码 比如:输入str会让其提示String 5.去掉单行显示类,让idea多行显示,容易找到类 6.设置字体及 ...
- asp.net mvc 获取ajax的 request payload 参数
注意事项: 传输的格式要设置城:"contentType": "application/x-www-form-urlencoded"
- js 循环遍历数组
var a =[1,3,4]; a.each(functiom{ .... }) or for (var x in a ){ .... }
- W phase 学习
W phase 的组成:(相关文献发现W phase适用于6级以上的地震) P, PP,S,SS,SP,PS等等长周期的震相: 它的传播机制和whispering gallery 相似. 从简振理论来 ...
- 中文datepicker控件
$(function() { $.datepicker.regional[, isRTL: !, showMonthAfterYear: !, yearSuffix: "年" } ...
- Ajax 以及 前端JSP页面如何查看数值
$.ajax({ url: ctx + "/unit/rsdl/qyjy/getDljgCode", type: "post", success: functi ...
- ChinaCock界面控件介绍-CCNavigateTitle
先看一下实际项目中的运行效果,如图,通过品牌的导航栏,显示不同品牌的商品列表. 完全基于ChinaCock控件包中CCNavigateTitle组件实现的,这是一个可视控件,从组件面板上拖放一个到Fo ...