Bean registered in several ways

1, the constructor with no arguments

xml inside

<?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="student" class="com.tx.ioc.xml.Student">
        <property name="name" value="zhangsan"/>
        <property name="age" value="13"/>
        <property name="classList">
            <list>
                <value>math</value>
                <value>enlish</value>
            </list>
        </property>
    </bean>
        <bean id="helloService" class="com.tx.ioc.xml.HelloService">
            <property name="student" ref="student"/>
        </bean>
</beans>

Two, student inside

public class Student {


    private String name;

    private Integer age;

    private List<String> classList;
    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 List<String> getClassList() {
        return classList;
    }

    public void setClassList(List<String> classList) {
        this.classList = classList;
    }

    public Student() {
        System.out.println("Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", classList=" + classList +
                '}');
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", classList=" + classList +
                '}';
    }

Second, there is the constructor parameter

xml inside this modification

  <bean id="student" class="com.tx.ioc.xml.Student">
        <constructor-arg index="0" value="zhangsan"></constructor-arg>
        <constructor-arg index="1" value="13"></constructor-arg>
        <!--<property name="name" value="zhangsan"/>-->
        <!--<property name="age" value="13"/>-->
        <property name="classList">
            <list>
                <value>math</value>
                <value>enlish</value>

            </list>
        </property>

    </bean>
        <bean id="helloService" class="com.tx.ioc.xml.HelloService">
            <property name="student" ref="student"/>
        </bean>
</beans>

 

Summary: Test Method above two methods as follows


@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(locations = "classpath:ioc/demo.xml")
public class HelloServiceTest {

    @Autowired
    private HelloService helloService;

    @Test
    public void contextLoads() {
    }

    @Test
    public void testHello(){
        System.out.println("haha"+helloService.getStudent());
        System.out.println(helloService.hello());

    }


}

 

Published 169 original articles · won praise 8 · views 80000 +

Guess you like

Origin blog.csdn.net/bluewelkin/article/details/104556538