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. python ----字符串基础练习题30道

    1.执行python脚本的两种方式 一种是点开始--运行--cmd 方式(这个操作需要先配置好环境变量path路径)之后运行python 二是直接进安装目录 运行tython软件运行.pycharm ...

  2. 房间安排 (nyoj 168)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=168 分析:找到一天中需要最多的房间即可 #include<iostream> ...

  3. 【Appium】Appium工作原理

    参考:http://www.cnblogs.com/zhjsll/p/5698878.html 原作者写的很好,所以直接放在这里. 一.什么是Appium Appium是一个开源.跨平台的测试框架,可 ...

  4. TBody scrollbar 设置

    由于scrollbar自身有宽度 对于tbody来说可能会挤压与thead不对齐下面办法能够解决大致问题 1.设置tbody display:block :  overflow-y:auto:(并且修 ...

  5. 在JS文件中,不需要<script>标签

    在JS文件中,不需要<script>标签\

  6. POJ - 2635 E - The Embarrassed Cryptographer

    The young and very promising cryptographer Odd Even has implemented the security module of a large s ...

  7. netty ------------ 如果selector检测到一个channel可以读了

    -----------------一个NioEventLoopGroup 的初始化的时候,会初始化一个 NioEventLoop数组,每个NioEventLoop在初始化的时候,会open一个sele ...

  8. L259

    Few things can feel as crushing as being rejected by someone who you're either dating or romanticall ...

  9. MySQL:安装mysqld系统及基础应用

    MySQL篇 第一章.安装mysqld系统及基础应用 一.安装 注意:mysql的标点符号只能是英文的标点符号. 1.设置配置文件. 文件格式:文本格式 文件位置:Mysql的主目录下 文件名称:my ...

  10. C++面试常见考点

    这两周参加了3家公司的面试,一家是做嵌入式的外企,一家是做智能家居的初创公司,一家是做网络分析的公司. 通过参加面试,越发的觉得语言只是基础,虽然都是计算机领域,但是不同的业务肯能用到的技术不同,所以 ...