Spring学习日记02_IOC_属性注入_其他类型属性

ICO操作Bean管理(xml注入其它类型属性)

  1. 字面量
  • null值
<property name="address">
    <null></null>
</property>
  • 属性值包含特殊符号
<!--
  1.把<>进行转义&lt;&gt
  2.把带特殊符号内容写到<![CDATA[...]]>
-->
<property name="address">
  <value><![CDATA[<<南京>>]]></value>
</property>
  1. 注入属性-外部bean
  • 创建两个类:service类和dao类
  • 在service调用dao里面热点方法

service层

public class UserService {
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public void add(){
        System.out.println("service add...");
        userDao.update();
    }
}

Dao层

public interface UserDao {
    public void update();
}
public class UserDapImpl implements UserDao {
    public void update() {
        System.out.println("Dao update...");
    }
}

xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="//www.springframework.org/schema/beans"
       xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="//www.springframework.org/schema/beans //www.springframework.org/schema/beans/spring-beans.xsd">

    <!--1.service和dao对象创建-->
    <bean id="userService" class="Spring.Ioc.Day04.service.UserService">
        <!--注入userDao对象
            name属性:类里面属性名称
            ref属性:创建userDao对象bean标签id值
        -->
        <property name="userDao" ref="userDaoImpl"></property>
    </bean>
    <bean id="userDaoImpl" class="Spring.Ioc.Day04.dao.UserDapImpl"></bean>
</beans>

test测试

public class testBean {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        UserService userService = context.getBean("userService",UserService.class);
        userService.add();
    }
}
  1. 注入属性-内部bean
  • 一对多关系:部门和员工
    • 一个部门有多个员工,一个员工属于一个部门

Dept类

public class Dept {
    private String dname;
    public void setDname(String name) {
        this.dname = name;
    }
    @Override
    public String toString() {
        return "Dept{" +
                "dname='" + dname + '\'' +
                '}';
    }
}

Emp类

public class Emp {
    private String ename;
    private String gender;
    //员工属于一个部门,使用对象形式表示
    private Dept 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;
    }

    public void add(){
        System.out.println(ename+"::"+gender+"::"+dept);
    }
}

xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="//www.springframework.org/schema/beans"
      xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="//www.springframework.org/schema/beans //www.springframework.org/schema/beans/spring-beans.xsd">

   <!--内部bean-->
   <bean id="emp" class="Spring.Ioc.Day04.bean.Emp">
       <!--设置两个普通属性-->
       <property name="ename" value="aaa"></property>
       <property name="gender" value="man"></property>
       <!--设置对象类型属性-->
       <property name="dept">
           <bean id="dept" class="Spring.Ioc.Day04.bean.Dept">
               <property name="dname" value="安保部"></property>
           </bean>
       </property>
   </bean>
</beans>

test类

    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
        Emp emp = context.getBean("emp",Emp.class);
        emp.add();
    }
  1. 注入属性-级联赋值
  • 第一种写法

xml

<bean id="emp" class="Spring.Ioc.Day04.bean.Emp">
        <property name="ename" value="bbb"></property>
        <property name="gender" value="man"></property>
        <!--级联赋值-->
        <property name="dept" ref="dept"></property>
    </bean>
    <bean id="dept" class="Spring.Ioc.Day04.bean.Dept">
        <property name="dname" value="财务部"></property>
    </bean>
  • 第二种写法
  • 需要提供属性的get方法

Emp类

//Dept属性增加get方法
    public Dept getDept() {
        return dept;
    }

xml

    <bean id="emp" class="Spring.Ioc.Day04.bean.Emp">
        <property name="ename" value="bbb"></property>
        <property name="gender" value="man"></property>
        <!--级联赋值-->
        <property name="dept.dname" value="技术部"></property>
    </bean>
Tags: