首页 > 代码库 > Spring的配置

Spring的配置

 

1、

 1 package com.helen.bean; 2  3 public class Student { 4     private int stuid; 5     private String name; 6     private String mess; 7  8     public int getStuid() { 9         return stuid;10     }11 12     public void setStuid(int stuid) {13         this.stuid = stuid;14     }15 16     public String getName() {17         return name;18     }19 20     public void setName(String name) {21         this.name = name;22     }23 24     public String getMess() {25         return mess;26     }27 28     public void setMess(String mess) {29         this.mess = mess;30     }31 32     @Override33     public String toString() {34         return "Student [stuid=" + stuid + ", name=" + name + ", mess=" + mess35                 + "]";36     }37 38 }

beans.xml:

<bean id="stu" class="com.helen.bean.Student" scope="prototype">        <property name="mess" value="Hello World!!!" />    </bean>

 

Test.java:

 1 package com.helen.bean; 2  3  4  5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7  8 public class Test { 9 10     public static void main(String[] args) {11     12         ApplicationContext   context = new ClassPathXmlApplicationContext("Beans.xml");13         Student stu=(Student)context.getBean("stu");14         Student stu1=(Student)context.getBean("stu");15         System.out.println("stu==stu1:"+(stu==stu1));16         stu.setName("helen");17         stu.setStuid(15);18         System.out.println("Infor:"+stu.getStuid()+" "+stu.getName()+" "+stu.getMess());19 }20 }

 

stu==stu1:true
Infor:15 helen Hello World!!!

 

Spring的配置