豆は、いくつかの方法で登録しました

1、引数なしのコンストラクタ

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

二、学生の内部

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 +
                '}';
    }

第二には、コンストラクタのパラメータがあります

この修正内部XML

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

 

要約:次のように二つの方法上記の試験方法


@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());

    }


}

 

公開された169元の記事 ウォンの賞賛8 ビュー80000 +

おすすめ

転載: blog.csdn.net/bluewelkin/article/details/104556538
おすすめ