初学spring,了解IoC的概念

初学spring,了解IoC的概念

配置环境:apache-maven-3.6.3 ,jdk1.8.0_231 ,IntelliJ IDEA 2019.2.4

1.新建一个spring Demo

在intelljIDEA中通过new->Project创建一个新的项目
在这里插入图片描述

选中Spring,由于是写个demo,按照如图默认的点Next
在这里插入图片描述

Project name:项目名称,最好写英文,我这里写的是springdemo_helloworld

Project location:当前项目存放的地址,自己可以自定义

设置好后点Finish
在这里插入图片描述

创建好之后目录如图,lib下会自动帮你下载spring所需要的依赖包
在这里插入图片描述

2.添加spring的配置文件

在src目录下新建spring所需要的的applicationContext.xml文件(配置文件),

鼠标选中src目录,右键,New->XML Configuration File->Spring Config

这里我顺便创建了package com.springdemo.helloworld存放之后的代码
在这里插入图片描述

创建好之后我这里会弹出提示

Application context not configured for this file:没有为此文件配置应用程序上下文

点右边的Configure application context
在这里插入图片描述
弹出如下对话框,点Create new application context
在这里插入图片描述
弹出框直接点ok即可
在这里插入图片描述
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">    
</beans>

3.编写spring demo

在src目录中,我在com.springdemo.helloworld包下创建Person类

public class Person {    
    private String name;    
    
    public Person() {    }   
    public Person(String name) {        
        this.name = name;     
    }    
    public String getName() {        return name;    }    
    public void setName(String name) {        this.name = name;    }

具有私有成员属性String的name,创建类的无参和有参构造方法,和属性的get,set方法

在相同目录下创建Test类

使用ClassPathXmlApplicationContext(“applicationContext.xml”),使用配置文件

ClassPathXmlApplicationContext()默认从src目录下寻找

ClassPathXmlApplicationContext ctx =  new ClassPathXmlApplicationContext("applicationContext.xml");

配置文件中还没配置信息,我们在配置文件中添加

<bean name="person" class="com.springdemo.helloworld.Person"></bean>

class中是spring需要控制的对象,name相当于通过spring new出对象的名称

将Person这个类的控制权交给spring,让spring来管理对象

这样我们就可以在Test中通过ctx.getBean()方法从spring中得到对象的实例

 Person person = ctx.getBean("person", Person.class);

"person"必须是与name中的相同,就是对象的名称,通过上面语句就可以得到spring new出来的实例给person

person.setName("HelloSpring");

可以使用set方法给name设置值,通过get方法查看实例中name的值

System.out.println(person.getName());

在这里插入图片描述
可以将配置文件中改写,bean中name添加个person1
name中可以添加多个名称,需要用逗号隔开

<bean name="person,person1" class="com.springdemo.helloworld.Person"></bean>

Test得到person1这个实例,不对它进行set方法,通过get方法查看name值

Person person = ctx.getBean("person", Person.class);
Person person1 = ctx.getBean("person1",Person.class);
person.setName("HelloSpring");
System.out.println(person.getName());
System.out.println(person1.getName());

在这里插入图片描述
发现person1也有name的值,与person一致,打印person,person1

System.out.println(person);
System.out.println(person1);

在这里插入图片描述
两者打印出来的值是一样的.可以看出通过spring管理的对象,默认创建的对象是单例

简单的写了个没有使用maven的spring的demo,用来了解下spring的IoC概念,将对象的创建等工作交给spring来操作,spring默认下创建的对象是单例的

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">

    <bean name="person,person1" class="com.springdemo.helloworld.Person"></bean>
</beans>
Test类中的内容
public class Test {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        Person person = ctx.getBean("person", Person.class);
        Person person1 = ctx.getBean("person1", Person.class);
        person.setName("HelloSpring");
        System.out.println(person.getName());
        System.out.println(person1.getName());
        System.out.println(person);
        System.out.println(person1);
    }
}

第一次写,有很多不足的地方,如果有哪里出错的地方欢迎指正,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值