Spring context:component-scan代替context:annotation-config

XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"><context:component-scan base-package="com.stono.sprtest" />
</beans>

AppBean:

package com.stono.sprtest;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class AppBeans6 {@SuppressWarnings("resource")public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("appbeans6.xml");Singer2 singer2 = (Singer2) context.getBean("singer");System.out.println(singer2);}
}

POJO:

package com.stono.sprtest;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component("singer") // 这里可以指定Bean的ID
public class Singer2 {@Autowired// @Component标注的类,其Id是camel-casing类名;两个Intrument接口实现类,必须指定其中的一个;@Qualifier("saxophone")private InstrumentI instrument;@Value("justValue")private String name;@Overridepublic String toString() {return "Singer2 [instrument=" + instrument + ", name=" + name + "]";}
}

package com.stono.sprtest;import org.springframework.stereotype.Component;@Component
public class Saxophone implements InstrumentI {private Integer age;private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}
}

package com.stono.sprtest;import org.springframework.stereotype.Component;@Component
public class Cymbal implements InstrumentI {
}

Spring context:component-scan代替context:annotation-config相关推荐

  1. spring源码分析之context:component-scan/vsannotation-config/

    1.<context:annotation-config/> xsd中说明: <xsd:element name="annotation-config"> ...

  2. context:annotation-config / 和context:component-

    为什么80%的码农都做不了架构师?>>>    1. <context:annotation-config /> 它的作用是隐式地向 Spring 容器注册   - Au ...

  3. spring源码分析之context

    重点类: 1.ApplicationContext是核心接口,它为一个应用提供了环境配置.当应用在运行时ApplicationContext是只读的,但你可以在该接口的实现中来支持reload功能. ...

  4. spring security reactive获取security context

    为什么80%的码农都做不了架构师?>>>    序 本文主要研究下reactive模式下的spring security context的获取. ReactiveSecurityCo ...

  5. Swagger报错,Unable to scan document context default java.lang.NullPointerException: null

    最近改完代码之后,启动项目,发现有以下error报错: Unable to scan document context default java.lang.NullPointerException: ...

  6. Spring中@ Component,@ Service,@ Controller和@Repository之间的区别

    在了解Spring框架中@Repository @Component , @Service @Controller , @Repository @Controller和@Repository批注之间的 ...

  7. Spring AOP源码(1)—<aop:config/>AOP配置标签解析【一万字】

      基于最新Spring 5.x,对Spring AOP中的<aop:config/>标签的解析源码进行了详细分析,这是Spring AOP源码的入口!   此前我们已经详细学习了Spri ...

  8. Spring注解@Component、@Repository、@Service、@Controller,@Autowired、@Resource用法

    一.Spring定义bean,@Component.@Repository.@Service 和 @Controller Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥 ...

  9. Spring注解@Component、@Repository、@Service、@Controll

    2019独角兽企业重金招聘Python工程师标准>>> 很长时间没做web项目都把以前学的那点框架知识忘光了,今天把以前做的一个项目翻出来看一下发现用·@Component标记一个组 ...

  10. Java单元测试实践-11.Mock后Stub Spring的@Component组件

    Java单元测试实践-00.目录(9万多字文档+700多测试示例) https://blog.csdn.net/a82514921/article/details/107969340 1. Sprin ...

最新文章

  1. 批量设置 style
  2. javascript等待异步线程完成_前端:什么是单线程,同步,异步?彻底弄懂 JavaScript 执行机制...
  3. Stack/Queue与Vector/List的联系
  4. java中Map ListE的用法
  5. 计算MACD(指数平滑移动平均线)Moving Average Convergence / Divergence
  6. 【宽度优先搜索笔记】BFS输出最短路径
  7. ORACLE成果,天天10问(四)
  8. 【PHP大马】定义、下载、使用、源码
  9. IDEA的short command line 的作用
  10. html页面常见布局
  11. 服务器受到DDOS攻击怎么办
  12. 老徐 MYSQL 宝典
  13. android触摸屏映射,解决android4.0 触摸屏分辨率映射不准
  14. 云天视界传媒浅谈无人机航拍技巧
  15. 关于音频情感分类的随笔(5)
  16. 8421码,5421码,2421码和余3码的分类及转换
  17. 边缘提取——Prewitt算子和Sobel算子
  18. 求多项式浮点java思路,求多项式函数实数根的方法
  19. android判断是否root或者是否能被root的方法
  20. tomcat警告setting property 'debug' to '0' did not find a matching property

热门文章

  1. Linux快速复制T级数据或删除大量小文件
  2. oracle解析失败事件,ORACLE诊断事件及深入解析10053事件
  3. c2665 “initgraph”: 2 个重载中没有一个可以转换所有参数类型_Python 命令行之旅:深入 click 之参数篇...
  4. 查询a表有但是b表没有的数据_牛逼!一个上亿数据的报表竟然能做到秒查~
  5. calender获取本月最后一天的日期_怕忘记2020法考重点日期?这份法考日历为你护航,信息量巨大...
  6. android socket通信如何抓取,安卓Socket通信实例(客户端、服务端)
  7. python处理csv文件缺失值_Python:如何处理CSV中的缺失值?
  8. linux平台的office文档转pdf的实例(程序员的菜)
  9. 使用CDN之后APACHE日志记录中IP地址不正确的解决方案
  10. Jmeter添加断言