java高级教程 实例化和非实例化 bean 学生信息

1、Stu.java

package com.yan;

public interface Stu {

    String toStrings();

}

2、Stuimp.java

package com.yan;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Service;

@Service("graduater")

public class Stuimp implements Stu {

    @Value("大盘鸡")

    private String teacher;

    @Resource(name="undergraduater")

    private Undergraduater s1;

    public String getTeacher() {

        return teacher;

    }

    public void setTeacher(String teacher) {

        this.teacher = teacher;

    }

    public Undergraduater getS1() {

        return s1;

    }

    public void setS1 (Undergraduater s1){

        this.s1 = s1;

    }

    public Stuimp(){}

    public Stuimp(String teacher, Undergraduater s1) {

        this.teacher = teacher;

        this.s1 = s1;

    }

    @Override

    public String toStrings() {

        String s2 = this.s1.toStrings();

        String Master = s2 + "\t导师:"+ teacher;

        return Master;

    }

}

3、Undergraduate.java

package com.yan;

import org.springframework.stereotype.Repository;

import org.springframework.beans.factory.annotation.Value;

@Repository("undergraduater")

public class Undergraduate implements Undergraduater {

    @Value("111333")

    private Integer longer;

    @Value("麻辣小香锅")

    private String stringer;

    @Value("硕士生")

    private String grade;

    public String getStringer() {

        return stringer;

    }

    public void setStringer(String stringer) {

        this.stringer = stringer;

    }

    public Integer getLonger() {

        return longer;

    }

    public void setLonger(Integer longer) {

        this.longer = longer;

    }

    public String getGrade() {

        return grade;

    }

    public void setGrade(String grade) {

        this.grade = grade;

    }

    public Undergraduate(){}

    public Undergraduate(String stringer, Integer longer, String grade) {

        this.stringer = stringer;

        this.longer = longer;

        this.grade = grade;

    }

    @Override

    public String toStrings() {

        String a = grade + "信息:\t"+ "名字:"+ stringer + "\t学号:"+ longer;

        return a ;

    }

}

 

4、Undergraduater.java

package com.yan;

public interface Undergraduater {

    public String toStrings();

}

5、Stu_Controller.java

package com.yan;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

@Controller("stu_Controller")

public class Stu_Controller {

    @Resource(name="graduater")

    private Stuimp graduate;

    public String show(){

        String a;

        a = this.graduate.toStrings();

        return a;

    }

 

}

 

6、StuTest.java

package com.yan;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StuTest {

    @SuppressWarnings({ "resource", "unused" })

    public static void main(String[] args) {

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

        String xmlPath1="com/yan/one.xml";

       ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);

       ApplicationContext applicationContext1 = new ClassPathXmlApplicationContext(xmlPath1);

        System.out.println("way one;");

        Undergraduater uge3 = (Undergraduater) applicationContext1.getBean("uge3");

        Undergraduater uge4 = (Undergraduater) applicationContext1.getBean("uge4");

        Stu gus1=(Stu) applicationContext1.getBean("gus1");

        Stu gus2=(Stu) applicationContext1.getBean("gus2");

        System.out.println(gus1.toStrings());

        System.out.println(gus2.toStrings());

        System.out.println("way two;");

        Stu_Controller stu_Controller = (Stu_Controller) applicationContext.getBean("stu_Controller");

        System.out.println(stu_Controller.show());

    }

}

7、one.xml

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

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

    "uge3" class ="com.yan.Undergraduate">

        "stringer" value="耀哥">

        "longer" value="133233">

        "grade" value="硕士生">

   

    "uge4" class = "com.yan.Undergraduate">

        "0" value="耀耀 "/>

        "1" value="233133"/>

        "2" value="硕士生"/>

   

    "gus1" class = "com.yan.Stuimp">

        "s1" ref ="uge3">

        "teacher" value="小明">

   

    "gus2" class="com.yan.Stuimp">

        "1" ref="uge4"/>

        "0" value="老王"/>

   

8、Two.xml

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

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

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

       xmlns:context="http://www.springframework.org/schema/context"

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

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

        http://www.springframework.org/schema/context

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

    "com.yan"/>

 

java高级教程 实例化和非实例化 bean 学生信息_第1张图片

 

 

 

你可能感兴趣的:(java高级教程 实例化和非实例化 bean 学生信息)