定义User类
public class User {
private String name;// 姓名
private Integer age;
public Integer getAge() {
return age;
}
public void setMsg(Integer age) {
this.age= age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "名字为:" + name+";年龄:"+age;
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="user" class="com.javaketang.test.User">
<property name="name" value="Alan"></property>
<property name="age" value="20"></property>
</bean>
</beans>
编写测试类
public static void main(String[] args) {
ApplicationContext ap = new ClassPathXmlApplicationContext("application-config.xml");
User u=ap.getBean("user",User.class);
System.out.println(u);
}
输出结果为 名字为:Alan;年龄:20
setter注入的强大之处在于还能引用其他类型的bean对象。
增加一个Dept类
public class Dept{
private String department;// 部门
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department= department;
}
@Override
public String toString() {
return "部门:" + department;
}
修改之前的User类
public class User {
private String name;// 姓名
private Integer age;
private Dept dept;
public void setDept(Dept dept){
this.dept=dept;
}
public Dept getDept(){
return dept;
}
public Integer getAge() {
return age;
}
public void setMsg(Integer age) {
this.age= age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "名字为:" + name+";年龄:"+age+";"+dept;
}
在配置文件上添加一个属性
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dept" class="com.javaketang.test.Dept">
<property name="department" value="营销部"></property>
</bean>
<bean id="user" class="com.javaketang.test.User">
<property name="name" value="Alan"></property>
<property name="age" value="20"></property>
<property name="dept" ref="dept"></property>
</bean>
</beans>
运行测试类
将会输出 名字为:Alan;年龄:20;部门:营销部
注入boolean值
public class User {
private String name;// 姓名
private Integer age;
private Dept dept;
private boolean close;
public void set(boolean close){
this.close=close;
}
public boolean isClose(){
return close;
}
public void setDept(Dept dept){
this.dept=dept;
}
public Dept getDept(){
return dept;
}
public Integer getAge() {
return age;
}
public void setMsg(Integer age) {
this.age= age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "名字为:" + name+";年龄:"+age+";"+dept+";标记:"+close;
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dept" class="com.javaketang.test.Dept">
<property name="department" value="营销部"></property>
</bean>
<bean id="user" class="com.javaketang.test.User">
<property name="name" value="Alan"></property>
<property name="age" value="20"></property>
<property name="dept" ref="dept"></property>
<-! 在Spring中boolean值得配置,支持这几种:true/false、1/0、on/off、yes/no -->
<property name="close" value="true"></property>
</bean>
</beans>
执行测试代码,将输出 名字为:Alan;年龄:20;部门:营销部;标记:true
(中智软件科技学校)