之前的 *** 作,都是注入的引用Bean,除了对象可以的引用可以注入,普通数据类型,集合等都可以在容器内注入
注入数据的三种类型
普通数据类型引用数据类型集合数据类型
现在来试试第一种,修改UserDaoImpl文件
package com.itheima.dao.impl; import com.itheima.dao.UserDao; import com.itheima.domain.User; import java.util.List; import java.util.Map; import java.util.Properties; public class UserDaoImpl implements UserDao { private String name; private int age; public void setName(String name) { this.name = name; }
普通数据类型值用value来赋值,再运行UserController,结果如下:
二》现在来讲解集合数据类型(引用数据类型之前已经讲过了,最先开始的把UserDao引用到UserService就是)
修改UserDaoimpl文件
package com.itheima.dao.impl; import com.itheima.dao.UserDao; import com.itheima.domain.User; import java.util.List; import java.util.Map; import java.util.Properties; public class UserDaoImpl implements UserDao { private ListstrList; private Map userMap; private Properties properties; public void setStrList(List strList) { this.strList = strList; } public void setUserMap(Map userMap) { this.userMap = userMap; } public void setProperties(Properties properties) { this.properties = properties; } private String name; private int age; public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void save() { System.out.println(name+"===="+age); System.out.println(strList); System.out.println(userMap); System.out.println(properties); System.out.println("save running...."); } }
创建个文件包domain.User,写代码
package com.itheima.domain; public class User { private String addr; private String name; public void setAddr(String addr) { this.addr = addr; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "addr='" + addr + ''' + ", name='" + name + ''' + '}'; } }
再来配置xml文件
aaa ccc bbb
在运行UserController文件,结果如下:
再来配置xml文件,为map赋值
aaa ccc bbb ppp1 ppp2
在运行UserController文件,结果如下:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)