Spring 2.5 入门小例子
程序员文章站
2022-04-09 09:14:24
...
main 函数
package Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Animal animal = (Animal) ac.getBean("Animal");
animal.Speack();
}
}
Animal.java
package Hello;
public class Animal {
private String name; //动物名字
private int age;//动物年龄
private String myClass;//动物类别
/**
* 动物介绍
* */
public void Speack() {
System.out.println("我是:" + this.myClass + ",我的名字叫:" + this.name + ",我:"
+ this.age + "岁!");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getMyClass() {
return myClass;
}
public void setMyClass(String myClass) {
this.myClass = myClass;
}
}
applicationContext.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-2.5.xsd"> <bean id="Animal" class="Hello.Animal"> <property name="myClass"> <value>猪</value> </property> <property name="name"> <value>姗姗</value> </property> <property name="age"> <value>1</value> </property> </bean> </beans>
加入必要的spring2.5的包,则一个小例子就完成了.
上一篇: 把对象转换成JSON形式的html代码
下一篇: (转)照片卡通化