1、spring_helloworld

01、spring IOC基本使用

(1)使用maven的方式来构建项目
定义项目的groupId、artifactId
(2)添加对应的pom依赖


4.0.0

<groupId>com.mashibing</groupId>
<artifactId>springHelloword23</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.9</version>
    </dependency>
</dependencies>
(3)编写配置

1、实体类创建
Person.java

package com.mashibing.bean;
public class Person {
private Long id;
private String name;
private Integer age;
private String gender;

public Person() {
}

public Person(Long id, String name, Integer age, String gender) {
    this.id = id;
    this.name = name;
    this.age = age;
    this.gender = gender;
}

@Override
public String toString() {
    return "Person{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", age=" + age +
            ", gender='" + gender + '\'' +
            '}';
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

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

public Integer getAge() {
    return age;
}

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

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

}
2、spring.xml配置

<!--
一个bean标签就表示一个对象
id:这个对象的唯一标识
class:注册对象的完全限定名
-->
<bean id="person" class="com.mashibing.bean.Person">
    <property name="id" value="3625221991********"></property>
    <property name="name" value="单强"></property>
    <property name="age" value="32"></property>
    <property name="gender" value="男"></property>
</bean>
3、编写测试类 测试类SpringDemoTest.java

public class SpringDemoTest {
public static void main(String[] args) {
//ApplicationContext:表示ioc容器
//ClassPathXmlApplicationContext:表示从当前classpath路径中获取xml文件的配置
//根据spring的配置文件来获取ioc容器对象
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Person person = context.getBean("person", Person.class);
System.out.println(person);
}
}
4,测试结果
Person{id=3625221991********, name='单强', age=32, gender='男'}

热门相关:总裁的天价小妻子   上将大叔,狼来了!   重生之将门毒后   嫡嫁千金   异能特工:军火皇后