spring配置中id和name属性的区别

曾经终败给现在 2022-07-21 11:17 312阅读 0赞

可能大家在网上都应该搜索过在 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都没有指定,且实例类都一样的,如:

代码

  1. <bean class="com.stamen.BeanLifeCycleImpl"/>
  2. <bean class="com.stamen.BeanLifeCycleImpl"/>
  3. <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:

  1. @Component("car2")
  2. public class Car {
  3. private String name;
  4. private double price;
  5. public Car(){
  6. }
  7. public Car(double price, String name) {
  8. this.price = price;
  9. this.name = name;
  10. }
  11. public double getPrice() {
  12. return price;
  13. }
  14. public void setPrice(double price) {
  15. this.price = price;
  16. }
  17. public String getName() {
  18. return name;
  19. }
  20. public void setName(String name) {
  21. this.name = name;
  22. }
  23. @Override
  24. public String toString() {
  25. return "Car [name=" + name + ", price=" + price + "]";
  26. }
  27. }

这里写图片描述

annotation.xml

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

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

  1. getBean("car");
  2. getBean("car2")

例子2:

  1. @Component
  2. public class Car {
  3. private String name;
  4. private double price;
  5. public Car(){
  6. }
  7. public Car(double price, String name) {
  8. this.price = price;
  9. this.name = name;
  10. }
  11. public double getPrice() {
  12. return price;
  13. }
  14. public void setPrice(double price) {
  15. this.price = price;
  16. }
  17. public String getName() {
  18. return name;
  19. }
  20. public void setName(String name) {
  21. this.name = name;
  22. }
  23. @Override
  24. public String toString() {
  25. return "Car [name=" + name + ", price=" + price + "]";
  26. }
  27. }

这里写图片描述

annotation.xml

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

main:

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

结果:

这里写图片描述

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

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

例子:

  1. @Component("b")
  2. public class Boss {
  3. private String name;
  4. @Autowired
  5. private Car car;
  6. public Boss(){
  7. }
  8. public Boss(String name, Car car, Office office) {
  9. this.name = name;
  10. this.car = car;
  11. this.office = office;
  12. }
  13. public Boss( Car car, Office office) {
  14. this.car = car;
  15. this.office = office;
  16. }
  17. public String getName() {
  18. return name;
  19. }
  20. public void setName(String name) {
  21. this.name = name;
  22. }
  23. public Car getCar() {
  24. return car;
  25. }
  26. public void setCar(Car car) {
  27. this.car = car;
  28. }
  29. @PostConstruct
  30. public void init(){
  31. System.out.println("初始化..");
  32. }
  33. @PreDestroy
  34. public void destroy(){
  35. System.out.println("销毁");
  36. }
  37. @Override
  38. public String toString() {
  39. return "Boss [name=" + name + ", car=" + car + " + "]"; } }

这里写图片描述

  1. @Component
  2. public class Car {
  3. private String name;
  4. private double price;
  5. public Car(){
  6. }
  7. public Car(double price, String name) {
  8. this.price = price;
  9. this.name = name;
  10. }
  11. public double getPrice() {
  12. return price;
  13. }
  14. public void setPrice(double price) {
  15. this.price = price;
  16. }
  17. public String getName() {
  18. return name;
  19. }
  20. public void setName(String name) {
  21. this.name = name;
  22. }
  23. @Override
  24. public String toString() {
  25. return "Car [name=" + name + ", price=" + price + "]";
  26. }
  27. }

annotation.xml

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

main

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

结果:

这里写图片描述

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

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

发表评论

表情:
评论列表 (有 0 条评论,312人围观)

还没有评论,来说两句吧...

相关阅读

    相关 NameID区别

    HTML文本是由HTML命令组成的描述性文本,HTML命令可以说明文字、图形、动画、声音、表格、链接等。HTML的结构包括头部(Head)、主体(Body)两大部分,其中头部描