Spring开发流程()

Spring开发流程(),第1张

Spring开发流程() 一、创建一个maven项目

二、在pom.xml文件中配置spring

        
            org.springframework
            spring-webmvc
            4.3.17.RELEASE
        
        

配置完成后记得导包!

三、配置spring

在resources文件夹里新建一个xml文件






四、使用bean标签在spring中创建对象

在(三)中的xml文件中配置


    

        
        
    

但是到目前为止,如果我们直接运行spring还是不能给我们创建对象,我们还需要有一步 *** 作
就是在测试类里写:

//读取配置文件,创建spring容器对象
        String conf = "applicationContext.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(conf);

getBean方法使用bean标签配置的id来从容器中获取对象

 //getBean方法使用bean标签配置的id来从容器中获取对象
        HelloService h1 = (HelloService) context.getBean("hello");
        h1.sayHello();

注意:强制类型转换的原因是,getBean返回的是Object类型的对象!
为什么?
因为getBean方法是spring中的方法,而spring在定义getBean方法的时候是不知道我们需要什么类型的对象的,所以返回的是Object类型的对象。

当然,使用类型来获取对象的时候,是不用强转的
//使用类型来获取对象,使用类型来获取对象的时候,不需要强转
        //使用类型来获取对象的时候,要注意如果该类型的对象有多个,是无法使用的
        HelloService h3 =  context.getBean("hello2",HelloService.class);
        h3.sayHello();
五、代码

resources文件夹下的applicationContext.xml文件:





    



    

        
        
    


    




        
        
    


    


    
        
            
        
    

    

    


    


    

    


    

    
        
        

            
            
            
            
            
        
    
    

main/java/com.haina.spring/HelloService.java:

package com.haina.spring;

public class HelloService {

    private String name;

    private int age;

    public void sayHello(){
        System.out.println("hello spring" + name + age);
    }

    public HelloService(){

    }

    public HelloService(String name,int age){
        this.name = name;
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

main/java/com.haina.spring/SpringTest.java:

package com.haina.spring;

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

//spring测试类





public class SpringTest {
    public static void main(String[] args) {
        //传统的创建对象,调用方法的形式
        //当我们在项目中只需要该类的一个对象,不需要重复性的创建时
        //可以使用spring容器来帮我们管理对象
        HelloService hs = new HelloService();
        hs.sayHello();

        //读取配置文件,创建spring容器对象
        String conf = "applicationContext.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(conf);


        //getBean方法使用bean标签配置的id来从容器中获取对象
        HelloService h1 = (HelloService) context.getBean("hello");
        h1.sayHello();

        HelloService h2 = (HelloService) context.getBean("hello");

        System.out.println(h1 == h2);

        //使用类型来获取对象,使用类型来获取对象的时候,不需要强转
        //使用类型来获取对象的时候,要注意如果该类型的对象有多个,是无法使用的
        HelloService h3 =  context.getBean("hello2",HelloService.class);
        h3.sayHello();



    }
}

六、总结 Spring容器初始化流程

1、加载配置文件
2、Spring会读取配置文件中的bean标签
3、通过bean标签的class属性的值,找到该bean对应的类
4、使用反射技术创建出该类的对象,存储到spring容器中
5、spring容器我们可以理解为是一个HashMap
6、对象存储时,key是该bean的id,value是对象

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5717318.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存