spring中基于xml获取bean的三种方式

文章目录

  • spring获取bean对象的3钟方式
      • 1.根据id获取bean对象
      • 2.根据类型获取bean对象
      • 3.根据id和类型获取bean对象
      • @注意


spring获取bean对象的3钟方式

bean.xml:

"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    
    "user" class="com.guigu.User">

1.根据id获取bean对象

import com.guigu.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserTest {

    public static void main(String[] args) {
        // 获取spring配置文件bean.xml

        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

//获取bean.xml中创建的对象
        //第一中获取方式:根据id获取bean对象
        //

        User user = (User) context.getBean("user");


        System.out.println("根据id获取对象" + user);
        // 第二中方式:根据类型获取bean对象

        User user2 = context.getBean(User.class);

        System.out.println("根据类型获取对象" + user2);


        // 根据id和类型获取bean对象
        User user3 = context.getBean("user", User.class);
        System.out.println("根据id和类型获取对象" + user2);


    }
}

2.根据类型获取bean对象

import com.guigu.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserTest {

    public static void main(String[] args) {
        // 获取spring配置文件bean.xml

        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

//获取bean.xml中创建的对象
        //第一中获取方式:根据id获取bean对象
        //

        User user = (User) context.getBean("user");


        System.out.println("根据id获取对象" + user);
        // 第二中方式:根据类型获取bean对象

        User user2 = context.getBean(User.class);

        System.out.println("根据类型获取对象" + user2);


        // 根据id和类型获取bean对象
        User user3 = context.getBean("user", User.class);
        System.out.println("根据id和类型获取对象" + user2);


    }
}

3.根据id和类型获取bean对象

import com.guigu.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserTest {

    public static void main(String[] args) {
        // 获取spring配置文件bean.xml

        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

//获取bean.xml中创建的对象
        //第一中获取方式:根据id获取bean对象
        //

        User user = (User) context.getBean("user");


        System.out.println("根据id获取对象" + user);
        // 第二中方式:根据类型获取bean对象

        User user2 = context.getBean(User.class);

        System.out.println("根据类型获取对象" + user2);


        // 根据id和类型获取bean对象
        User user3 = context.getBean("user", User.class);
        System.out.println("根据id和类型获取对象" + user2);


    }
}

@注意

根据类型获取对象时候springIoc中bean对象有且只有一个,否则异常。
bean.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    
    <bean id="user1" class="com.guigu.User">bean>
    <bean id="user" class="com.guigu.User">bean>
beans>

测试:

// 第二中方式:根据类型获取bean对象

        User user2 = context.getBean(User.class);

        System.out.println("根据类型获取对象" + user2);

结果:
Exception in thread “main” org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type ‘com.guigu.User’ available: expected single matching bean but found 2: user1,user

当bean对象class同名的情况下可以根据id、id和类型等两种方式进行获取bean对象即可,

你可能感兴趣的:(spring,spring,xml,java)