可能大家在网上都应该搜索过在 Spring 配置中 id 和 name 属性的区别,可能你会搜索到有一大堆的区别,不过在我这里可能不一样了。

我这里 Spring 的版本为 3.2.4,区别不是很大,这里总结一下。

1.id 和 name 的命名规范不是很严格。

2.id的时候用分号(“;”)、空格(“ ”)或逗号(“,”)分隔开就只能当成一个标识,name的时候用分号(“;”)、空格(“ ”)或逗号(“,”)分隔开就要当成分开来的多个标识(相当于别名 alias 的作用)。

如:

name=“1 2 3 4”等同于 name=“1,2,3,4” 这样写相当于有 1 2 3 4(4个)个标识符标识当前bean

id=“1 2 3 4” 这样写相当于有 “1 2 3 4”(1个)个标识符标识当前bean

3.配置两个相同的 id 或者 name 都不能通过。

4.如果既配置了 id ,也配置了 name ,则两个都生效。

5.如果id和name都没有指定,则用类全名作为name,如<bean class="com.stamen.BeanLifeCycleImpl">,则你可以通过

getBean("com.stamen.BeanLifeCycleImpl")返回该实例。

6.如果存在多个id和name都没有指定,且实例类都一样的,如:

代码

<bean class="com.stamen.BeanLifeCycleImpl"/><bean class="com.stamen.BeanLifeCycleImpl"/><bean class="com.stamen.BeanLifeCycleImpl"/>

则第一个bean通过getBean(“com.stamen.BeanLifeCycleImpl”)获得,

第二个bean通过getBean(“com.stamen.BeanLifeCycleImpl#1”)获得,

第三个bean通过getBean(“com.stamen.BeanLifeCycleImpl#2”)获得,以此类推。

7.注解和配置文件都存在的时候

如果配置基本类的时候,注解和配置文件都使用的时候,注解和配置文件中 name 不相同的时候, 则两个不冲突,都能够生效。

如果配置基本类的时候,注解和配置文件都使用的时候,注解和配置文件中 name 相同的时候, 则两个冲突,配置文件生效。
例子1:

@Component("car2")
public class Car {private String name;private double price;public Car(){}public Car(double price, String name) {this.price = price;this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Car [name=" + name + ", price=" + price + "]";}}

annotation.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"><!--  <context:annotation-config/> Spring版本更新后就不需要这个了--><!-- 设置扫描的包 --><context:component-scan base-package="com.briup.ioc.annotation" /><bean name="car" class="com.briup.ioc.annotation.Car"><property name="name"><value>宝马</value></property></bean>
</beans>

当然这两个都能够得到的。

getBean("car");
getBean("car2")

例子2:

@Component
public class Car {private String name;private double price;public Car(){}public Car(double price, String name) {this.price = price;this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Car [name=" + name + ", price=" + price + "]";}}

annotation.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"><!--  <context:annotation-config/> Spring版本更新后就不需要这个了--><!-- 设置扫描的包 --><context:component-scan base-package="com.briup.ioc.annotation" /><bean name="car" class="com.briup.ioc.annotation.Car"><property name="name"><value>宝马</value></property></bean>
</beans>

main:

public void ioc_annotation() {String path = "com/briup/ioc/annotation/annotation.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(path);Car car = (Car) ac.getBean("car");System.out.println(car);}

结果:

如果该类作为引用类的时候,并且自动注入的时候,注解和配置文件都配置的时候,如果 name 相同的话,配置文件生效。

如果该类作为引用类的时候,并且自动注入的时候,注解和配置文件都配置的时候,如果 name 不相同的话,就按照 Autowired 的匹配规则去匹配。(不清楚 Autowired 的用法的同学去看我 Spring(2) 这篇文章的介绍)

例子:

@Component("b")
public class Boss {private String name;@Autowiredprivate Car car;public Boss(){}public Boss(String name, Car car, Office office) {this.name = name;this.car = car;this.office = office;}public Boss( Car car, Office office) {this.car = car;this.office = office;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Car getCar() {return car;}public void setCar(Car car) {this.car = car;}@PostConstructpublic void init(){System.out.println("初始化..");}@PreDestroypublic void destroy(){System.out.println("销毁");}@Overridepublic String toString() {return "Boss [name=" + name + ", car=" + car + "+ "]";}}

@Component
public class Car {private String name;private double price;public Car(){}public Car(double price, String name) {this.price = price;this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Car [name=" + name + ", price=" + price + "]";}}

annotation.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"><!--  <context:annotation-config/> Spring版本更新后就不需要这个了--><!-- 设置扫描的包 --><context:component-scan base-package="com.briup.ioc.annotation" /><bean name="car" class="com.briup.ioc.annotation.Car"><property name="name"><value>宝马</value></property></bean>
</beans>

main

public void ioc_annotation() {String path = "com/briup/ioc/annotation/annotation.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(path);   Car car = (Car) ac.getBean("car");System.out.println(car);}

结果:

此时得到的是配置文件中的配置的。

当然注解和配置文件同时配置的几率不大。

spring配置中id和name属性的区别相关推荐

  1. Spring配置中的classpath:与classpath*:的区别研究

    文章目录 概念解释及使用场景 classpath:和classpath*:区别 概念解释及使用场景 classpath是指WEB-INF文件夹下的classes目录. 通常我们一般使用这种写法实在we ...

  2. 4、Spring配置中的classpath:与classpath*:的区别

    2019独角兽企业重金招聘Python工程师标准>>> 一.概念解释及使用场景: classpath是指WEB-INF文件夹下的classes目录. 通常我们一般使用这种写法实在we ...

  3. Spring配置中的classpath:与classpath*:的区别研究(转)

    概念解释及使用场景: classpath是指WEB-INF文件夹下的classes目录. 通常我们一般使用这种写法实在web.xml中,比如spring加载bean的上下文时,如下: <!--系 ...

  4. HTML中id、name、class 区别

    HTML 中 id与name 区别 一个name可以同时对应多个控件,比如checkbox和radio 而id必须是全文档中唯一的 id的用途  1) id是HTML元素的Identity,主要是在客 ...

  5. HTML5中id、name、class 区别

    转载地址:http://www.2cto.com/kf/201210/161751.html 网上找不到专门针对html5的,只能找到以前HTML的.看了一下,它们差不多,尤其是本文后面专门讲的区别讲 ...

  6. Spring配置中的bean直接引用其它bean的属性值

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! prin ...

  7. spring配置中加载properties文件方法

    首先,遇到一个问题,spring配置中加载properties文件配置如下: <context:property-placeholder ignore-unresolvable="tr ...

  8. spring框架中JDK和CGLIB动态代理区别

    转载:https://blog.csdn.net/yhl_jxy/article/details/80635012 前言 JDK动态代理实现原理(jdk8):https://blog.csdn.net ...

  9. 细说HTML元素的ID和Name属性的区别

    细说HTML元素的ID和Name属性的区别 可以说几乎每个做过Web开发的人都问过,到底元素的ID和Name有什么区别阿?为什么有了ID还要有Name呢?! 而同样我们也可以得到最classical的 ...

最新文章

  1. c++ lambda函数_C++ Lambda表达式
  2. 利用SimpleHttpServer+urllib传文件
  3. 10远程连接连接不上华为云_从云手机到云游戏,5G会在多大程度上改变我们的生活?...
  4. Java的三大结构理解
  5. 如何搭建Docker私有仓库
  6. JdbcPagingItemReader多线程的Step
  7. 浅说动态生成Class实现MVC
  8. h5 兑换商品 页面模版_H5页面制作工具编辑功能对比:木疙瘩、微吾、云
  9. Saiku Table展示数据合并bug修复(二十五)
  10. 611. 有效三角形的个数
  11. bzoj 2178 圆的面积并 —— 辛普森积分
  12. ipv6地址格式_IPV6与IPV4的差异
  13. subline3插件html,Sublime Text3与html的插件
  14. 网络管理员考试试题分类精解电子书
  15. [Java学习] BFS算法示例
  16. 温度对二极管伏安特性的影响
  17. java 多态(重写和重载)
  18. java计算机毕业设计-智慧农业水果销售系统源码+mysql数据库+系统+lw文档+部署
  19. 数模笔记(四):插值与拟合1.0
  20. 《管理学》第八章 领导

热门文章

  1. 利用IPv6实现公网访问远程桌面
  2. javascript百炼成仙 第一章 掌握JavaScript基础1.5基础考核
  3. python操作excel 2016
  4. 胡润首次发布《2019胡润全球独角兽榜》,11家区块链公司入选!
  5. 打造高效能研发团队的 5 个关键步骤
  6. DeepMind再爆性骚扰丑闻!内部邮件泄露,色狼自夸猛男
  7. 【Discuz】原系统进入论坛自动注册并进行登录
  8. 立法机关从83辆减0辆
  9. 求二阶系统输入单位斜坡响应matlab,二阶系统的斜坡响应教程.docx
  10. 【正则】1302- 快速上手常用正则表达式