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--注解(一) 常用注解总结的更多相关文章

  1. Spring + Mybatis 企业应用实战 第3章 Sping MVC的常用注解

    注解(annotation) @Controller @Controller是扩展的@Component的,可以说基本一样,就是作为一种标志. @RequestMapping value:     指 ...

  2. [Spring学习笔记 3 ] spring 注解详解,完全注解,常用注解

    .xml使用注解 xml 用来定义bean的信息,注解用来配置依赖信息 ) 在配置文件中配置bean )在javaBean中用注解来指定依赖注入 )在配置文件中开启注解扫描 @Resource标签 j ...

  3. Swagger2常用注解和使用方法

    一   引入maven依赖 <!--整合Swagger2--> <dependency> <groupId>com.spring4all</groupId&g ...

  4. Spring系列之Spring常用注解总结

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  5. SpringMVC常用注解實例詳解3:@ResponseBody

    我的開發環境框架:        springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...

  6. SpringMVC常用注解實例詳解2:@ModelAttribute

    我的開發環境框架:        springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...

  7. Spring常用注解汇总

    本文汇总了Spring的常用注解,以方便大家查询和使用,具体如下: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). <context:component- ...

  8. Spring常用注解,自动扫描装配Bean

    1 引入context命名空间(在Spring的配置文件中),配置文件如下: xmlns:context="http://www.springframework.org/schema/con ...

  9. springmvc常用注解与类型转换

    springmvc常用注解与类型转换 一:前置 spring -servlet.xml 注入 <!-- 启用spring mvc 注解 --> <context:annotation ...

  10. 【SSM 2】spring常用注解

    声明:以下观点,纯依据个人目前的经验和理解,有不当之处,多指教! 一.基本概述 注解(Annotation):也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举 ...

随机推荐

  1. C#中使用FFMPEG切割、合并视频。

    参考网址:https://blog.csdn.net/samwang_/article/details/70332924 使用前先确保电脑已经安装了FFMPEG,并且配置好环境变量.检测是否安装配置好 ...

  2. Kafka.net使用编程入门(三)

    这个世界既不是有钱人的世界,也不是有权人的世界,它是有心人的世界. 一些有用的命令 1.列出主题:kafka-topics.bat --list --zookeeper localhost:2181 ...

  3. 手动配置 Windows 时间服务

    手动配置 Windows 时间服务 要将内部时间服务器配置为与外部时间源同步,请按照下列步骤操作: 将服务器类型更改为 NTP. 为此,请按照下列步骤操作: 选择 “开始” . “运行”,键入 reg ...

  4. 图的拓扑排序,AOV,完整实现,C++描述

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  5. [Leetcode 135]糖果分配 Candy

    [题目] There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  6. 3-D crustal model transfer to cdl format

    The downloaded crustal model file, for example, its name is TW-PS-H14.nc The command is ncdump -b c ...

  7. Oracle存储过程基础

    http://blog.sina.com.cn/s/blog_67e424340100iyg1.html

  8. wait_activity

    wait_activity(self, activity, timeout, interval=1): android特有的 返回的True 或 False :Agrs: - activity - 需 ...

  9. 《RECURRENT BATCH NORMALIZATION》

    原文链接 https://arxiv.org/pdf/1603.09025.pdf Covariate 协变量:在实验的设计中,协变量是一个独立变量(解释变量),不为实验者所操纵,但仍影响实验结果. ...

  10. ios手动添加数组字典(NSMutableDictionary)

    @property (nonatomic,strong) NSArray *imageData;//定义一个数组 -(NSArray *)imageDate { if(_imageDate==nil) ...