java高级教程 注入装配问题

Stu.java

 

package com.yan;

import java.util.List;

public class Stu {

    private String username;

    private Integer xuehao;

    private List list;

    public Stu(String username,Integer xuehao,List list) {

       super();

       this.username = username;

       this.xuehao = xuehao;

       this.list = list;

    }

    public Stu() {

       super();

    }

    public void setUsername(String username) {

       this.username = username;

    }

    public void setXuehao(Integer xuehao) {

       this.xuehao = xuehao;

    }

    public void setList(List list) {

       this.list = list;

    }

    public String toString() {

       return "User {学生=" + username + ", 学号=" + xuehao + ", list=" + list + "}";

    }

 

}

 

GraStu.java

 

package com.yan;

import com.yan.Stu;

public class GraStu {

    private String teacher;

    private Stu m;

    private Stu n;

    public void setStu1(Stu n){

       this.n = n;

    }

    public void setStu(Stu m){

       this.m = m;

    }

    public String teacher() {

       return "teacher";

    }

    public GraStu(String teacher) {

       super();

       this.teacher = teacher;

    }

    public GraStu() {

       super();

    }

    public void setTeacher(String teacher) {

       this.teacher = teacher;

    }

    public void setN(Stu n) { this.n = n;}

    public void setM(Stu m) { this.m = m;}

    public String toString1() {

       return "User {" + n + " 老师:" + teacher + "}";

    }

    public String toString() {

       return "User {" + m + " 老师:" + teacher + "}";

    }

 

}

 

abc.xml

 

"1.0" encoding="UTF-8"?>

"http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">

   

    "stu1" class="com.yan.Stu" >

        "0" value="耀哥" />

        "1" value="133233" />

        "2">

          

              "constructorvalue1"

              "constructorvalue2"

          

       

   

    "stu2" class="com.yan.Stu">

         "username" value="小耀耀">

         "xuehao" value="11111111">

         "list">

            

                 "setlistvalue1"

                 "setlistvalue2"

                     

        

   

    "userGrad" class="com.yan.GraStu">

        "m" ref="stu1" />

        "teacher" value="小明">

   

        "useGrad" class="com.yan.GraStu">

        "m" ref="stu2" />

        "teacher" value="小王">

   

 

 

Text.java

 

package com.yan;

import org.springframework.context.ApplicationContext;

import

     org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.context.support.AbstractApplicationContext;

@SuppressWarnings("unused")

public class Text {

    public static void main(String[] args) {

       String xmlPath = "com/yan/abc.xml";

       ApplicationContext applicationContext =

                    new ClassPathXmlApplicationContext (xmlPath);

       System.out.println(applicationContext.getBean("stu1"));

       System.out.println(applicationContext.getBean("stu2"));

       System.out.println(applicationContext.getBean("userGrad"));

       System.out.println(applicationContext.getBean("useGrad"));

    }

}

java高级教程 注入装配问题_第1张图片

 

你可能感兴趣的:(java高级教程 注入装配问题)