Spring --- código de demostración para comprender la inversión de control de IOC

En primer lugar, se recomienda utilizar herramientas IDE para el desarrollo, primero comprenda el contenedor IOC de Spring:

Cree una buena herramienta IDE:

Las representaciones son las siguientes:

El código específico es el siguiente:

package bean;

public class Person {
    private String name;
    private String age;
    private String gender;

    public Person() {
    }

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

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

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

    public String getGender() {
        return gender;
    }

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }
}
<?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 id="person" class="bean.Person">
        <property name="name"  value="kobe"></property>
        <property name="age"  value="24"></property>
        <property name="gender"  value="male"></property>
    </bean>

</beans>

El código de prueba es el siguiente:

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

public class Mytest {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("ioc.xml");
        Person p = ac.getBean(Person.class);
        System.out.println("得到person类:"+p);
    }
}

 

 

 

Supongo que te gusta

Origin blog.csdn.net/LB_Captain/article/details/114297382
Recomendado
Clasificación