1,普通bean
package org.lunzi;

public class User {

private String username;
private int age;
private String city;

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public void setUsername(String username){

this.username = username;
}

public String getUsername(){

return username;

}

}
2,spring的配置bean文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>


<bean id=
"user" class="org.lunzi.User" abstract="false"
singleton=
"true" lazy-init="default" autowire="default"
dependency-check=
"default">
<property name=
"username">
<value>lunzi</value>
</property>
<property name=
"age">
<value>23</value>
</property>
<property name=
"city">
<value>Beijing</value>
</property>
</bean>

</beans>
3,调用文件
package org.lunzi;

import org.springframework.context.*;
import org.springframework.context.support.*;

public class TestMain {

/**
* @param args
*/

public static void main(String[] args) {
// TODO 自动生成方法存根
ApplicationContext context = new FileSystemXmlApplicationContext(
"org/lunzi/applicationContext.xml");
User user = (User)context.getBean(
"user");

System.out.println(user.getUsername());
System.out.println(user.getAge());
System.out.println(user.getCity());
}

}

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

认证码:
(数字如不清楚,请刷新一次)


Copyright@2008 powered by YuLog