Spring

1.User.java
package org.myspring.beans;

public class User {
private String username;

private String password;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public void setPassword(String password) {
this.password = password;
}

public String getPassword() {
return password;
}

}


###################
2.applicationContext.xml
<?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.myspring.beans.User" abstract="false"
singleton="true" lazy-init="default" autowire="default"
dependency-check="default">
<property name="username">
<value>zwm</value>
</property>
<property name="password">
<value>pwd</value>
</property>
</bean>
</beans>

3.TestMain.java 测试类编写。通过XML文件去读取内容
package org.myspring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class TestMain {
public static void main(String args[]){
// User user=new User();
// user.setUsername("zwm");
// System.out.println(user.getUsername());
ApplicationContext context=new FileSystemXmlApplicationContext("applicationContext.xml");
User user=(User)context.getBean("user");
System.out.println(user.getUsername());
System.out.println(user.getPassword());  
}
}

你可能感兴趣的:(java,spring,xml,bean)