初学spring,了解IoC的概念

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

1.新建一个spring Demo

在intelljIDEA中通过new->Project创建一个新的项目

01

选中Spring,由于是写个demo,按照如图默认的点Next

02

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

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

设置好后点Finish

03

创建好之后目录如图,lib下会自动帮你下载spring所需要的依赖包

04

2.添加spring的配置文件

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

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

这里我顺便创建了package com.springdemo.helloworld存放之后的代码

05

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

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

点右边的Configure application context

06

弹出如下对话框,点Create new application context

07

弹出框直接点ok即可

08

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>

3.编写spring demo

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

public class Person {

    private String name;
    private int age;

    public Person() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

具有私有成员属性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());

09

可以将配置文件中改写,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());

10

发现person1也有name的值,与person一致,打印person,person1

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

11

两者打印出来的值是一样的.可以看出通过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);
    }
}
第一次写,有很多不足的地方,如果有哪里出错的地方欢迎指正,谢谢!



猜你喜欢

转载自www.cnblogs.com/yangqihang/p/12047707.html