-
基于xml方式创建对象
在Spring配置文件中,使用bean标签,标签里添加对应的属性,就可以实现对象创建
-
在bean标签中的常用属性
id:对象的唯一标识
class:类全路径
name:可以加特殊符号的对象唯一标识
创建对象时,默认是执行无参构造方法完成对象创建,如果当前对象没有无参构造会报错
Caused by: java.lang.NoSuchMethodException: com.carl.entity.User.<init>()
-
-
基于xml方式注入属性
DI:依赖注入,使用依赖注入用于管理Bean之间的依赖关系,不仅可以注入基本数据类型的属性,还可以注入引用数据类型的属性
-
依赖注入的两种方式(官网),实际上有三种
-
set注入
public class UserService { private UserDao userDao; private String path; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void setPath(String path) { this.path = path; } public void get(){ System.out.println("path:"+path+"::userDao:"+userDao.getValue()); } }
<bean id="userService" class="com.carl.service.UserService"> <property name="userDao" ref="userDao"/> <property name="path" value="readme.txt"/> </bean> <bean id="userDao" class="com.carl.dao.UserDao"> <property name="mapper" value="15"/> </bean>
-
构造器注入
public class User { private String id; private String name; private Integer age; private String sex; public User(String id, String name, Integer age, String sex) { this.id = id; this.name = name; this.age = age; this.sex = sex; } }
<bean id="user" class="com.carl.entity.User"> <constructor-arg index="0" value="1"/> <constructor-arg index="1" value="巴顿"/> <constructor-arg index="2" value="6"/> <constructor-arg index="3" value="母"/> </bean>
-
接口注入
说明:注入空值和特殊字符
<!--注入空值--> <property name="path"> <null/> </property> <!--注入特殊字符: 第一种方式:使用转义字符 第二种方式:使用CDATA--> <property name="path"> <!--注意:使用value标签,包含的所有内容都认为是path的值,回车也算!!!!--> <value><![CDATA[src/com/carl/bean1.xml]]></value> </property>
注入外部bean
<bean id="userService" class="com.carl.service.UserService"> <property name="userDao" ref="userDao"/> <property name="path"> <value><![CDATA[src/com/carl/bean1.xml]]></value> </property> </bean> <bean id="userDao" class="com.carl.dao.UserDao"> <property name="mapper" value="15"/> </bean>
注入内部bean和级联赋值
public class Emp { private String eName; private String gender; //员工属于某一个部门 private Dept dept; private static class Dept { private String dName; public void setdName(String dName) { this.dName = dName; } @Override public String toString() { return "Dept{" + "dName='" + dName + '\'' + '}'; } } //级联赋值使用dept.dName方式必须声明get方法,否则无法使用 public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } public void seteName(String eName) { this.eName = eName; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Emp{" + "eName='" + eName + '\'' + ", gender='" + gender + '\'' + ", dept=" + dept.toString() + '}'; } }
<!--注入内部bean--> <bean id="emp" class="com.carl.entity.Emp"> <property name="eName" value="lucy"/> <property name="gender" value="女"/> <property name="dept"> <bean id="dept" class="com.carl.entity.Emp.Dept"> <property name="dName" value="研发部"/> </bean> </property> <!--也可以使用ref这种方式,两种方式的作用都是一样的,外部bean也可以使用内部bean的方式 ,但是内部bean使用上述的方式更加直观--> <!-- <property name="dept" ref="dept"/>--> </bean> <!--级联赋值--> <bean id="dept" class="com.carl.entity.Emp.Dept"> <property name="dName" value="研发部"/> </bean> <bean id="emp" class="com.carl.entity.Emp"> <property name="eName" value="lucy"/> <property name="gender" value="女"/> <property name="dept" ref="dept"/> <property name="dept.dName" value="产品部"/> </bean>
-
-
注意:这也是为什么使用注解的方式必须所有需要注入的类都必须实现get/set方法的原因
注入集合
-
普通数据类型的集合
public class CollectionEntity { private String[] course; private List<String> list; private Map<String,String> map; private Set<String> set; public void setCourse(String[] course) { this.course = course; } public void setList(List<String> list) { this.list = list; } public void setMap(Map<String, String> map) { this.map = map; } public void setSet(Set<String> set) { this.set = set; } @Override public String toString() { return "CollectionEntity{" + "course=" + Arrays.toString(course) + ", list=" + list + ", map=" + map + ", set=" + set + '}'; } }
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="collectionEntity" class="com.carl.entity.CollectionEntity"> <property name="course"> <array> <value>Java课程</value> <value>Python课程</value> <value>数据库课程</value> <value>Scala课程</value> </array> </property> <property name="list"> <list> <value>唐僧</value> <value>孙悟空</value> <value>猪八戒</value> <value>沙僧</value> <value>白龙马</value> </list> </property> <property name="map"> <map> <entry key="课程1" value="Java"></entry> <entry key="课程2" value="Python"></entry> <entry key="课程3" value="C++"></entry> <entry key="课程4" value="C"></entry> <entry key="课程5" value="php"></entry> </map> </property> <property name="set"> <set> <value>MySQL</value> <value>Oracle</value> </set> </property> </bean> </beans>
-
对象类型的集合
public class CollectionObject { private Course[] course; private List<Course> list; private Map<Course,Course> map; private Set<Course> set; public void setCourse(Course[] course) { this.course = course; } public void setList(List<Course> list) { this.list = list; } public void setMap(Map<Course, Course> map) { this.map = map; } public void setSet(Set<Course> set) { this.set = set; } @Override public String toString() { return "CollectionEntity{" + "course=" + Arrays.toString(course) + ", list=" + list + ", map=" + map + ", set=" + set + '}'; } }
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="course1" class="com.carl.entity.Course"> <property name="cName" value="Spring5"/> </bean> <bean id="course2" class="com.carl.entity.Course"> <property name="cName" value="Mybatis"/> </bean> <bean id="collectionObject" class="com.carl.entity.CollectionObject"> <property name="course"> <array> <ref bean="course1"></ref> <ref bean="course2"></ref> </array> </property> <property name="list"> <list> <ref bean="course1"></ref> <ref bean="course2"></ref> </list> </property> <property name="map"> <map> <entry key-ref="course2" value-ref="course1"></entry> <entry key-ref="course1" value-ref="course2"></entry> </map> </property> <property name="set"> <set> <ref bean="course1"></ref> <ref bean="course2"></ref> </set> </property> </bean> </beans>