hello spring

来源:互联网 发布:淘宝上怎么延迟收货 编辑:程序博客网 时间:2024/05/29 18:19
package myspring;public class Age {       public  String chop(){               return "使用斧头砍柴" ;         }}
package myspring;public class Person {       private Age age ;       public void setAge(Age age) {            this.age = age;       }       public void useAge(){              System.out.println("我打算去砍点柴火");              System.out.println(age.chop()) ;       }}
<?xml version="1.0" encoding="UTF-8"?><beans  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xmlns="http://www.springframework.org/schema/beans"         xsi:schemaLocation="        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">       <bean id = "person" class = "myspring.Person">              <property name="age"  ref = "age"/>       </bean>       <bean id = "age" class = "myspring.Age"/></beans>
package myspring;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {       public static void main(String[] args) {              ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml")  ;              Person p = ctx.getBean("person" , Person.class) ;              p.useAge() ;       }}
0 0