创建对象和set注入属性
DI:注入属性
通过set注入属性
xml配置文件
<bean id="book" class="com.su.spring5.Book">
<!--使用property完成属性注入
name:类里面的属性名称
value向属性注入的值
-->
<property name="bname" value="易筋经"></property>
<property name="bauthor" value="达摩老祖"></property>
</bean>
测试类
@Test
public void BookTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
Book book = context.getBean("book", Book.class);
System.out.println(book);
}
通过有参构造注入:
<!-- 有参构造注入-->
<bean id="order" class="com.su.spring5.Orders">
<constructor-arg name="oname" value="电脑"></constructor-arg>
<constructor-arg name="address" value="China"></constructor-arg>
</bean>
p名称空间注入
xmlns:p="http://www.springframework.org/schema/p"
<bean id="book" class="com.su.spring5.Book" p:bname="九阳神功" p:bauthor="无名氏">
<!--<property name="bname" value="易筋经"></property>
<property name="bauthor" value="达摩老祖"></property>-->
</bean>
注入空值和特殊符号
<bean id="book" class="com.su.spring5.Book">
<property name="bname" value="易筋经"></property>
<property name="bauthor" value="达摩老祖"></property>
<property name="bprice">
<null></null> <!--注入空值-->
</property>
</bean>
<bean id="book" class="com.su.spring5.Book">
<!--
1.进行转义
2.把特殊符号内容写到CDATA
-->
<property name="bname" >
<value><![CDATA[<<易筋经>>]]></value>
</property>
<property name="bauthor" value="达摩老祖"></property>
<property name="bprice" value="100"></property>
</bean>
注入外部bean
创建两个类service和dao类
在service调用dao里面的方法
UserDao
public interface UserDao {
public void update();
}
UserImpl
public class UserImpl implements UserDao{
@Override
public void update() {
System.out.println("dao update..........");
}
}
UserService
import com.su.spring5.dao.UserDao;
public class UserService {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
System.out.println("service add......");
userDao.update();
}
}
<bean id="userDaoImpl" class="com.su.spring5.dao.UserImpl"></bean>
<bean id="userService" class="com.su.spring5.service.UserService">
<!--
注入userDao对象
name:类属性
ref:创建userDao对象bean标签id值
-->
<property name="userDao" ref="userDaoImpl">
</property>
</bean>
@Test
public void TestService(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
内部bean和级联赋值
一对多的关系
一个部门有多个员工,一个员工属于一个部门
<bean id="emp" class="com.su.spring5.bean.Emp">
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<property name="dept">
<bean id="dept" class="com.su.spring5.bean.Dept">
<property name="dname" value="安保部"></property>
</bean>
</property>
</bean>
级联赋值
一开始和外部bean注入步骤是一致的。
<bean id="emp" class="com.su.spring5.bean.Emp">
<property name="ename" value="Allen"></property>
<property name="gender" value="男"></property>
<property name="dept" ref="dept"></property>
<property name="dept.dname" value="技术部"></property>
</bean>
<bean id="dept" class="com.su.spring5.bean.Dept"></bean>
注入集合属性
注入数组类型的属性
注入List集合类型
注入Map集合类型
package com.su.spring5.bean;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Student {
private String[] courses;
private List<String> list;
private Map<String,Integer> map;
private Set<String> set;
public Student() {
}
public Student(String[] courses, List<String> list, Map<String, Integer> map, Set<String> set) {
this.courses = courses;
this.list = list;
this.map = map;
this.set = set;
}
public String[] getCourses() {
return courses;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Map<String, Integer> getMap() {
return map;
}
public void setMap(Map<String, Integer> map) {
this.map = map;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
@Override
public String toString() {
return "Student{" +
"courses=" + Arrays.toString(courses) +
", list=" + list +
", map=" + map +
", set=" + set +
'}';
}
}
<bean id="student" class="com.su.spring5.bean.Student">
<!--数组类型的属性注入-->
<property name="courses">
<array>
<value>数学</value>
<value>英语</value>
<value>语文</value>
</array>
</property>
<!--集合类型的属性注入-->
<property name="list">
<list>
<value>Java</value>
<value>C++</value>
<value>Python</value>
</list>
</property>
<property name="map">
<map>
<entry key="html" value="89"></entry>
<entry key="c++" value="92"></entry>
<entry key="js" value="76"></entry>
</map>
</property>
<property name="set">
<set>
<value>mysql</value>
<value>sql server</value>
<value>rdius</value>
</set>
</property>
</bean>