栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

Spring工厂类的其他方法:

引入依赖:pom.xml:



    4.0.0

    com.itheima
    spring02
    1.0-SNAPSHOT

    
        
            junit
            junit
            4.11
            test
        

        
            org.springframework
            spring-context
            5.1.14.RELEASE
        


    

先创建Person类:

package com.itheima;

public class Person {
}

 创建配置文件applicationContext.xml:






    

    


测试类:

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);
    }

    
    @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工厂配置文件中只能有一个







    

    

    

 测试4

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/865203.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号