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

Spring开发流程()

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

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是对象

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

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

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