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

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. loadrunner使用随机值

    用户登录设置:系统用1000000001.1000000002等可以登录系统,这个代表登录的用户名

  2. XV6陷入,中断和驱动程序

    陷入,中断和驱动程序 运行进程时,cpu 一直处于一个大循环中:取指,更新 PC,执行,取指…….但有些情况下用户程序需要进入内核,而不是执行下一条用户指令.这些情况包括设备信号的发出.用户程序的非法 ...

  3. Codeforces Round #269 (Div. 2)-D. MUH and Cube Walls,KMP裸模板拿走!

    D. MUH and Cube Walls 说实话,这题看懂题意后秒出思路,和顺波说了一下是KMP,后来过了一会确定了思路他开始写我中途接了个电话,回来kaungbin模板一板子上去直接A了. 题意: ...

  4. HDU1757-A Simple Math Problem,矩阵快速幂,构造矩阵水过

    A Simple Math Problem 一个矩阵快速幂水题,关键在于如何构造矩阵.做过一些很裸的矩阵快速幂,比如斐波那契的变形,这个题就类似那种构造.比赛的时候手残把矩阵相乘的一个j写成了i,调试 ...

  5. [Vijos1512] SuperBrother打鼹鼠 (二维树状数组)

    传送门 直接搞就行. 注意下表re从零开始,而树状数组搞不了0,所以统一增加一个偏移量1. (话说数据随机是什么鬼?) # include <iostream> # include < ...

  6. 【模拟】2017 Multi-University Training Contest 1 The Battle of Chibi

    acm.hdu.edu.cn/showproblem.php?pid=5542 [Accepted] #include<iostream> #include<cstdio> # ...

  7. The Doors--poj1556(最短路+判断点与线段的关系)

    http://poj.org/problem?id=1556 题目大意:从(0,5)走到(10,5)走的最短距离是多少 中间有最多18个隔着的墙  每个墙都有两个门  你只能从门通过 我的思路是  只 ...

  8. Copy List with Random Pointer (Hash表)

    A linked list is given such that each node contains an additional random pointer which could point t ...

  9. CF821E(多次矩阵快速幂)

    题意: 冈伦从二维平面上(0,0)走到(k,0),(k<=1e18),每次有三个行动方向:右上一格.右方一格.右下一格,问一共有多少种走的方案 限制:每段x都有一个天花板,一共有n段天花板(n& ...

  10. Spring Boot中验证码实现kaptcha

    要生成验证码网上的方案比较多,基本是基于两大类:1为自定义生成,操作用Image类,2为kaptcha生成,有模糊算法. 当然也可以直接交由前端进行处理 1.基于kaptcha 首先不要怀疑的是报名是 ...