依赖注入,从字面上理解,即是:以注入的方式实现依赖;

Spring 容器负责创建应用程序中的 bean,并通过 DI(依赖注入)来协调这些对象之间的关系。当描述 bean 如何进行装配(autowired)时,Spring 具有强大的灵活性,提供了以下三种主要的装配机制:

  • 在 XML 中显式配置;
  • 在 Java 中进行显式配置;
    • XXConfig(JavaConfig)
  • 隐式的 bean 发现机制和自动装配;

  • 组件扫描(Component scanning):Spring 会自动发现应用上下文中所创建的 bean;

    • 使用 @Component 注解(annotation)
    • 默认是不扫描的;
  • 自动装配(autowiring):Spring 自动满足 bean 之间的依赖;

0. basics

https://blog.csdn.net/zhang854429783/article/details/6785574

  • 属性加上@Autowired后不需要getter()和setter()方法,Spring也会自动注入。
  • @Component、@Repository、@Service、@Controller

    • @Service用于标注业务层组件
    • @Controller用于标注控制层组件(如struts中的action)
    • @Repository用于标注数据访问组件,即DAO组件
    • @Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
    @Service
    public class VentorServiceImpl implements iVentorService {
    }
    @Repository
    public class VentorDaoImpl implements iVentorDao {
    }
    • <context:component-scan base-package=”com.eric.spring”>component-scan标签默认情况下自动扫描指定路径下的包(含所有子包),将带有@Component、@Repository、@Service、@Controller标签的类自动注册到spring容器。
  • 接口存在多个实现类的时候必须使用@Qualifier指定注入哪个实现类,否则(只有一个实现类)可以省略,只写@Autowired。

    @Autowired
    @Qualifier("chinese")
    private Man man;

1. @Bean

  • @Bean 注解将会告诉 Spring 其修饰的方法将会返回一个对象,该方法要注册为 Spring 应用上下文的 bean。

    @Bean
    public Base getDerived() {
    return new Derived();
    }

    默认情况(default)下,bean 的 ID 与带有 @Bean 注解的方法名保持一致。上述代码中,bean 的名字将会是 getDerived,当然也可以指定其他名字:

    @Bean(name="anotherName")
    ...

2. @Configuration

  • @Configuration, 与 XML 文件描述配置不同,Spring 还支持使用 Java 代码描述配置,这里的 java 类需要被 @Configuration 修饰。

    <?xml ...>
    <bean id="knight" class="....BraveKnight">
    <constructor-arg ref="quest" />
    </bean> <bean id="quest" class="...SlayDragonQuest">
    <constructor-arg value="#{T(System.out)}" />
    </bean>

    上述 xml 可简单理解为:

    knight() ⇒ new BraveKnight(quest());
    quest() ⇒ new SlayDragonQuest(System.out);

    使用 Java 来描述配置:

    @Configuration
    public class KnightConfig {
    @Bean
    public Knight knight() {
    return new BraveKnight(quest());
    }
    @Bean
    public Quest quest() {
    return new SlayDragonQuest(System.out);
    }
    }

3. @Component 与 @ComponentScan

  • @Component,由该注解修饰的类,表明该类会作为组件类,并告知 Spring 为这个类创建 bean;
  • 组件扫描默认是不开启的,需要显示配置 Spring,以命令其去寻找带有 @Component 注解的类,有如下两种配置方式:

    • 配置文件:
    <?xml version="1.0" encoding="UTF-8" ?>
    <beans>
    <context:component-scan base-package="com.spring.jdbc"/>
    </beans>
    • 同一包下,由 @ComponentScan 修饰的类;

4. @Import

  • 运用的时候需要获取某类对应的bean,此时就需要用到@Import注解。

    // 先创建两个类,不注入到 IOC 容器,,在应用的时候在导入到当前容器中。
    public class Dog {}
    public class Cat {} @ComponentScan
    @Import({Dog.class, Cat.class})
    public class App {
    public static void main(String[] args) throws Exception {
    ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
    System.out.println(context.getBean(Dog.class));
    System.out.println(context.getBean(Cat.class));
    context.close();
    }
    }

Spring 各种注解(@)的含义与认识的更多相关文章

  1. Spring boot注解(annotation)含义详解

    Spring boot注解(annotation)含义详解 @Service用于标注业务层组件@Controller用于标注控制层组件(如struts中的action)@Repository用于标注数 ...

  2. Spring Boot常用的注解以及含义<持续更新>

    1.@RestController和@RequestMapping注解 @RestController 和 @RequestMapping 注解是Spring MVC注解(它们不是Spring Boo ...

  3. spring mvc 注解 学习笔记(一)

    以前接触过spring,但是没有接触spring mvc 以及注解的应用,特习之,记之: 注解了解 @Component 是通用标注, @Controller 标注web控制器, @Service 标 ...

  4. Spring JSR-250注解

    Java EE5中引入了“Java平台的公共注解(Common Annotations for the Java Platform)”,而且该公共注解从Java SE 6一开始就被包含其中. 2006 ...

  5. atititt.java定时任务框架选型Spring Quartz 注解总结

    atititt.java定时任务框架选型Spring Quartz 总结 1. .Spring Quartz  (ati recomm) 1 2. Spring Quartz具体配置 2 2.1. 增 ...

  6. Spring入门注解版

    参照博文Spring入门一,以理解注解的含义. 项目结构: 实现类:SpringHelloWorld package com.yibai.spring.helloworld.impl; import ...

  7. 品Spring:注解终于“成功上位”

    历史还是抛弃了XML,当它逐渐尝到注解的甜头之后. 尤其是在Spring帝国,到处充满着注解的气息. 注解从一个提供附属信息的“门客”,蜕变为颇具中流砥柱的“君侯”. 注解成功登上了帝国的舞台,定会像 ...

  8. Spring MVC注解的一些案列

    1.  spring MVC-annotation(注解)的配置文件ApplicationContext.xml <?xml version="1.0" encoding=& ...

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

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

  10. spring @condition 注解

    spring @condition注解是用来在不同条件下注入不同实现的 demo如下: package com.foreveross.service.weixin.test.condition; im ...

随机推荐

  1. Leetcode 239.滑动窗口最大值

    滑动窗口最大值 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口 k 内的数字.滑动窗口每次只向右移动一位. 返回滑动窗口最大值. 示例: ...

  2. ACboy needs your help(分组背包)

    ACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profit he ...

  3. 动手实操(一):如何用七牛云 API 实现相片地图?

    实操玩家: 在苹果手机上,我们只要打开定位服务,拍照后便能在相簿中找到地图,地图上显示着在各地拍摄的相片.网站上这种显示方式也并不少见,例如 Flickr.即将关闭的 Panoramio 等. 作为地 ...

  4. HDU4135容斥原理

    #include <cstdio> #include <string.h> #include <cmath> using namespace std; #defin ...

  5. 【带权并查集】HDU 3047 Zjnu Stadium

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 [题意] http://blog.csdn.net/hj1107402232/article/detail ...

  6. [USACO12MAR]拖拉机

    题目描述 After a long day of work, Farmer John completely forgot that he left his tractor in the middle ...

  7. linux上配置spark集群

    环境: linux spark1.6.0 hadoop2.2.0 一.安装scala(每台机器)   1.下载scala-2.11.0.tgz   放在目录: /opt下,tar -zxvf scal ...

  8. 通过分析system_call中断处理过程来深入理解系统调用

    通过分析system_call中断处理过程来深入理解系统调用 前言说明 本篇为网易云课堂Linux内核分析课程的第五周作业,上一次作业中我以2个系统调用(getpid, open)作为分析实例来分析系 ...

  9. IdHttp 资料

    http://blog.csdn.net/delphizhou/article/details/3085704 IdHttp 资料 网上找了些不过很不好找.今天找了些收藏在一起.以便他人查阅, idh ...

  10. 前端学习之-- JavaScript

    JavaScript笔记 参考:http://www.cnblogs.com/wupeiqi/articles/5602773.html javaScript是一门独立的语言,游览器都具有js解释器 ...