context:annotation-config vs context:component-scan

<context:annotation-config> 用来注入已经在上下文注册的bean,无论bean是定义在XML中还是被 package scanning。

<context:component-scan>仅scans packages 去注册应用上线文中的Bean。

example:

Lets start with a basic setup of three beans of type AB and C, with B and C being injected into A.

package com.xxx;
public class B {public B() {System.out.println("creating bean B: " + this);}
}package com.xxx;
public class C {public C() {System.out.println("creating bean C: " + this);}
}package com.yyy;
import com.xxx.B;
import com.xxx.C;
public class A { private B bbb;private C ccc;public A() {System.out.println("creating bean A: " + this);}public void setBbb(B bbb) {System.out.println("setting A.bbb with " + bbb);this.bbb = bbb;}public void setCcc(C ccc) {System.out.println("setting A.ccc with " + ccc);this.ccc = ccc; }
}

XML配置:

<bean id="bBean" class="com.xxx.B" />
<bean id="cBean" class="com.xxx.C" />
<bean id="aBean" class="com.yyy.A"><property name="bbb" ref="bBean" /><property name="ccc" ref="cBean" />
</bean>

加载环境产生输出:

creating bean B: com.xxx.B@c2ff5
creating bean C: com.xxx.C@1e8a1f6
creating bean A: com.yyy.A@1e152c5
setting A.bbb with com.xxx.B@c2ff5
setting A.ccc with com.xxx.C@1e8a1f6

这是正常的输出,但是这是老的spring 风格.,现在我们使用注解。

First, lets autowire the bbb and ccc properties on bean A like so:

package com.yyy;
import org.springframework.beans.factory.annotation.Autowired;
import com.xxx.B;
import com.xxx.C;
public class A { private B bbb;private C ccc;public A() {System.out.println("creating bean A: " + this);}@Autowiredpublic void setBbb(B bbb) {System.out.println("setting A.bbb with " + bbb);this.bbb = bbb;}@Autowiredpublic void setCcc(C ccc) {System.out.println("setting A.ccc with " + ccc);this.ccc = ccc;}
}

XML配置:

<bean id="bBean" class="com.xxx.B" /> <bean id="cBean" class="com.xxx.C" /> <bean id="aBean" class="com.yyy.A" />

加载结果:

creating bean B: com.xxx.B@5e5a50
creating bean C: com.xxx.C@54a328
creating bean A: com.yyy.A@a3d4cf

上面的结果并没有完成注入,这是因为@Autowired 只是一个注解,如果让它完成注入工作需要<context:annotation-config />

改变一下XML的配置:

<context:annotation-config />
<bean id="bBean" class="com.xxx.B" />
<bean id="cBean" class="com.xxx.C" />
<bean id="aBean" class="com.yyy.A" />

输出结果:

creating bean B: com.xxx.B@15663a2
creating bean C: com.xxx.C@cd5f8b
creating bean A: com.yyy.A@157aa53
setting A.bbb with com.xxx.B@15663a2
setting A.ccc with com.xxx.C@cd5f8b

现在,我们使用注解注释Bean 删除砸xml中的定义:

package com.xxx;
import org.springframework.stereotype.Component;
@Component
public class B {public B() {System.out.println("creating bean B: " + this);}
}package com.xxx;
import org.springframework.stereotype.Component;
@Component
public class C {public C() {System.out.println("creating bean C: " + this);}
}package com.yyy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.xxx.B;
import com.xxx.C;
@Component
public class A { private B bbb;private C ccc;public A() {System.out.println("creating bean A: " + this);}@Autowiredpublic void setBbb(B bbb) {System.out.println("setting A.bbb with " + bbb);this.bbb = bbb;}@Autowiredpublic void setCcc(C ccc) {System.out.println("setting A.ccc with " + ccc);this.ccc = ccc;}
}

xml中的配置:

<context:annotation-config />

加载后什么都没有输出。

<context:component-scan base-package="com.xxx" />
<context:annotation-config />
<context:component-scan base-package="com.xxx" />
<bean id="aBean" class="com.yyy.A" />

OK,得到了我们想要的结果。

 

总结:

The difference between the two is really simple!.

<context:annotation-config />

Enables you to use annotations that are restricted to wiring up properties and constructors only of beans!.

Where as

<context:component-scan base-package="org.package"/>

Enables everything that <context:annotation-config /> can do, with addition of using stereotypes eg.. @Component@Service , @Repository. So you can wire entire beans and not just restricted to constructors or properties!.

 

<context:annotation-config>: Scanning and activating annotations for already registered beans in spring config xml.

<context:component-scan>: Bean registration + <context:annotation-config>


@Autowired and @Required are targets property level so bean should register in spring IOC before use these annotations. To enable these annotations either have to register respective beans or include <context:annotation-config />. i.e. <context:annotation-config /> works with registered beans only.

@Required enables RequiredAnnotationBeanPostProcessor processing tool
@Autowired enables AutowiredAnnotationBeanPostProcessor processing tool

Note: Annotation itself nothing to do, we need a Processing Tool, which is a class underneath, responsible for the core process.


@Repository, @Service and @Controller are @Component, and they targets class level.

<context:component-scan> it scans the package and find and register the beans, and it includes the work done by <context:annotation-config />.

 

 

转载于:https://www.cnblogs.com/codeingthink/p/9257604.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/279975.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Cactiz中文版安装使用

#----------------------------------------------------------# # > 红色字体 -特指煮酒个人所见。加粗则为需要重点注意。 ## > 蓝色加粗 -特指与本文相关人员&#xff0c;包括参与修正的朋友。 ## > 煮酒品茶 -Http://cwtea.blog.51cto.com # #----------…

如何在OS X中打开或关闭鼠标定位器

OS X 10.11 El Capitan includes a new “mouse locator” feature. If you lose your mouse pointer, just shake the mouse or move your finger on the touch pad vigorously, and the mouse pointer will temporarily grow very large so you can see it. OS X 10.11 El Ca…

微软宣布 Win10 设备数突破8亿,距离10亿还远吗?

百度智能云 云生态狂欢季 热门云产品1折起>>> 微软高管 Yusuf Mehdi 昨天在推特发布了一条推文&#xff0c;宣布运行 Windows 10 的设备数已突破 8 亿&#xff0c;比半年前增加了 1 亿。 根据之前的报道&#xff0c;两个月前 Windows 10 的全球市场份额才首次超越 W…

UI自动化web端框架path.py代码

import os,sysBASE_PATH os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))sys.path.insert(0, BASE_PATH)# 配置文件CONF_PATH BASE_PATH os.path.sep conf os.path.sep config.json# 日志的路径WEB_LOG_PATH BASE_PATH os.path.sep lo…

snapchat为什么_我的Snapchat朋友旁边的表情符号是什么意思?

snapchat为什么Next to some of your Snapchat friends, you’ll see little emoji. 在您的某些Snapchat朋友旁边&#xff0c;您会看到小的表情符号。 Each of these emoji has a specific meaning. Let’s look at what they are. 这些表情符号都有特定的含义。 让我们看看它们…

暴力打表之hdu 2089

题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid2089 有两种方法&#xff1a; 1.数位DP算法 2.暴力打表——真是个好法子&#xff01;&#xff01;&#xff01; 接下来是注意点&#xff1a; 1.一般这种数组中的一个数减去一个数组的另一个数&#xff0c;sum[i…

最全的正则表达式大全

很多不太懂正则的朋友&#xff0c;在遇到需要用正则校验数据时&#xff0c;往往是在网上去找很久&#xff0c;结果找来的还是不很符合要求。所以我最近把开发中常用的一些正则表达式整理了一下&#xff0c;在这里分享一下。给自己留个底&#xff0c;也给朋友们做个参考。 一、校…

关于移动应用APP数据安全的一点见解

2019独角兽企业重金招聘Python工程师标准>>> 项目沟通中过程客户反复在强调&#xff0c;大数据的安全性&#xff0c; 言下之意&#xff0c;用了大数据&#xff0c;就不安全了&#xff0c;就有漏洞了。所以花了些时间&#xff0c;针对大数据的安全设计做了一个总结&a…

2022年底C# 解压zip文件遇到的一个Bug

本文由网友投稿。作者&#xff1a;江湖人士原文标题&#xff1a;2022年底C# 解压zip文件遇到的一个bug原文链接&#xff1a;https://jhrs.com/2022/46060.html最近在排查一个上传功能时&#xff0c;客户端上传的是zip文件&#xff0c;到服务器端后使用C# 解压zip文件的代码将上…

如何关闭mcafee软件_如何摆脱McAfee的通知和捆绑软件

如何关闭mcafee软件McAfee, like most other modern antivirus programs, doesn’t stay out of your way. It installs browser extensions and shows various alert messages you might not want to see. If McAfee came with your PC, you may regularly see messages that …

mfs使用指引

客户端工具集 mfsgetgoal #设定副本数mfssetgoal #获取副本数mfscopygoal # mfsgetsclass mfssetsclass mfscopysclass mfsxchgsclass mfslistsclass mfsgettrashtime #设定回收站时间 mfssettrashtime #设定回收站时间 mfscopytrashtime mfsgeteattr #设置权限 mfsseteattr…

【命名规范】.NET中的枚举类型,要以Enum结尾吗?

“首先要给它一个名字&#xff0c;然后你才能描述它。”图片&#xff1a;北京的晚霞 摄影师&#xff1a;刘先生这个话题源于公司《.NET技术规范》中的一条&#xff1a;【强制】枚举声明应以Enum结尾我对此并不认同&#xff1a;首先&#xff0c;引用一下微软官方文档中&#xff…

Linux中防火墙(一)

一&#xff1a;前言防火墙&#xff0c;其实说白了讲&#xff0c;就是用于实现Linux下访问控制的功能&#xff0c;它分为硬件的或者软件的防火墙两种。无论是在哪个网络中&#xff0c;防火墙工作的地方一定是在网络的边缘。而我们的任务就是需要去定义到底防火墙如何工作&#x…

macos安全性偏好设置_如何更改macOS系统偏好设置的布局

macos安全性偏好设置If you don’t care for the way the System Preferences appear in macOS, you can change them by hiding certain preference panels or by rearranging them alphabetically. 如果您不喜欢系统偏好设置在macOS中的显示方式&#xff0c;则可以通过隐藏某…

机器视觉技术在表面缺陷检测方面的发展趋势

导读&#xff1a;机器视觉技术在表面缺陷检测方面的发展趋势如何&#xff1f;很多人都不了解&#xff0c;据悉&#xff0c;目前工业中应用的机器视觉检测绝大部分执行的是二维检测任务&#xff0c;三维机器视觉检测仍处于理论研究和试验阶段。除此之外&#xff0c;机器视觉检测…

J.U.C之AQS

AQS是J.U.C的核心 AQS(AbstractQueuedSynchronizer)队列同步器&#xff0c;AQS是JDK下提供的一套用于实现基于FIFO等待队列的阻塞锁和相关的同步器的一个同步框架。 同步器面向的是锁的实现者&#xff0c;它简化了锁的实现方式&#xff0c;屏蔽了同步状态管理、线程的排队、等待…

.NET周报【12月第3期 2022-12-23】

由于众所周知的原因&#xff0c;大佬们纷纷加入羊群&#xff0c;笔者也未能幸免&#xff0c;体验下来这绝对不是普通感冒的症状&#xff0c;身体不适&#xff0c;熬了几天&#xff0c;所以本周更新比较晚&#xff1b;另外精力有限&#xff0c;对于国际板块只有链接没有简介&…

亚马逊的vps多少钱一个月_如何查看您在亚马逊上花了多少钱

亚马逊的vps多少钱一个月Have you ever wondered how much you’ve spent at Amazon during your lifetime? Whether you’re feeling curious or just plain brave, there’s an easy way to find out. 您是否想过一生在亚马逊上花了多少钱&#xff1f; 无论您是好奇还是勇敢…

JavaScript-client、offset、scroll、定时器

client offset scroll client、offset、scroll系列 他们的作用主要与计算盒模型&#xff0c;盒子的偏移量和滚动有关 clientTop 内容区域到边框顶部的距离&#xff0c; 说白了&#xff0c; 就是边框的高度 clientLeft 内容区域到边框左部的距离&#xff0c; 说白了&#xff0…

Java 五大原则

1、单一职责 不论是在设计类&#xff0c;接口还是方法&#xff0c;单一职责都会处处体现&#xff0c;单一职责的定义&#xff1a;我们把职责定义为系统变化的原因。所有在定义类&#xff0c;接口&#xff0c;方法的时候。定义完以后再去想一想是不能多于一个的动机去改变这个类…