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

可能大家在网上都应该搜索过在 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;
    }

    @Override
    public 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.xsd
   http://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;
    }

    @Override
    public 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.xsd
   http://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;
    @Autowired
    private 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;
    }
    @PostConstruct
    public void init(){
        System.out.println("初始化..");
    }
    @PreDestroy
    public void destroy(){
        System.out.println("销毁");
    }
    @Override
    public 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;
    }

    @Override
    public 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.xsd
   http://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);
    }

结果:

这里写图片描述

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

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

  • 19
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值