Spring-第一个程序和工厂类的方法等总结01

本文介绍了Spring工厂类的多个方法,包括通过id获取Bean、使用泛型获取Bean、获取所有Bean的id、判断Bean是否存在以及检查特定类型的Bean等。在测试过程中,演示了不同方法的使用场景和可能遇到的问题,如当配置文件中有多个相同类型的Bean时可能出现的异常。此外,还讨论了无id属性的Bean如何处理及其name属性的使用。
摘要由CSDN通过智能技术生成

 

 

Spring工厂类的其他方法:

 

引入依赖:pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>spring02</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
<!--spring依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.14.RELEASE</version>
        </dependency>


    </dependencies>

</project>

先创建Person类:

package com.itheima;

public class Person {
}

 创建配置文件applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


<!--
  id:名字唯一
  class属性:类的全限定名
-->
    <bean id="person" class="com.itheima.Person"/>

    <bean id="person1" class="com.itheima.Person"/>

<!--    <bean class="com.itheima.Person"/>-->
</beans>

测试类:

package com.itheima;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    //Spring的第一个程序
    @Test
    public void test1(){
        //1.获得spring的工厂
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
        //2.通过工厂类获得对象
        Person person =(Person) ctx.getBean("person");
        System.out.println("Person="+person);
    }

    /*
      Spring工厂提供的其他方法:
       getBean():有多个重载的方法
     */
    @Test
    public void test2(){
        //1.获得spring的工厂
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
        //2.通过工厂类获得对象的重载方法2 不清强制类型转换
//        Person person = ctx.getBean("person", Person.class);
//        System.out.println("Person="+person);

        //这个方法,需要Spring工厂配置文件中只能有一个<bean class是Person类型,否则会报异常
//        Person bean = ctx.getBean(Person.class);
//        System.out.println("person="+bean);

        //(2)获得Spring工厂配置文件中,所有的bean标签的id值 person person1
//        String[] beanDefinitionNames = ctx.getBeanDefinitionNames();
//        for (String beanDefinitionName:beanDefinitionNames){
//            System.out.println("beanDefinitionName="+beanDefinitionName);
//        }

        //(3)获得特定类型获取Spring配置文件中对应的bean的id值
//        String[] beanNamesForType = ctx.getBeanNamesForType(Person.class);
//        for (String id:beanNamesForType){
//            System.out.println("id="+id);
//        }

        //(4)用于判断是否存在指定id值得bean ,存在放回true,不存在返回false  containsBeanDefinition()只能判断id不能用判断name
//        boolean person = ctx.containsBeanDefinition("person");
//        System.out.println(person);

        //(5)用于判断是否存在指定id值得bean  containsBean():可以用id判断,也可以判断name属性判断
        if (ctx.containsBean("a")){
            System.out.println("true="+true);
        }else{
            System.out.println("false="+false);
        }

    }

    //测试Spring配置文件bean标签没有id属性是否含有默认属性
    @Test
    public void test3(){
        //1.获得spring的工厂
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
        String[] beanDefinitionNames = ctx.getBeanDefinitionNames();
        for (String beanDefinitionName:beanDefinitionNames){
            System.out.println("beanDefinitionName="+beanDefinitionName);
        }
    }
    //用于测试name属性
    @Test
    public void test4(){
        //1.获得spring的工厂
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
        //2.通过工厂类获得对象
        Person person =(Person) ctx.getBean("p1");
        System.out.println("Person="+person);
    }
}

第一个测试结果:

 

第二个方法的测试结果:

1.

2.因为配置问价有2个beanPerson类型的类,所以会报异常

3.

4.

5.

6.配置问价没有a类

 方法3测试结果:

需要把配置文件中创建一个没有id的bean标签,并把有id的bean注释:

<?xml version="1.0" encoding="UTF-8"?>
<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">


<!--
  id:名字唯一
  class属性:类的全限定名
-->
<!--    <bean id="person" class="com.itheima.Person"/>-->

<!--    <bean id="person1" class="com.itheima.Person"/>-->
    
<!--    <bean class="com.itheima.Person"/>-->
    
<!--beand的name属性-->
    <bean id="person" name="p,p1" class="com.itheima.Person"/>
</beans>

 测试4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵俺第一专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值