[Spring5]IOC容器_Bean管理XML方式_注入其他类型属性

Posted 唐火

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Spring5]IOC容器_Bean管理XML方式_注入其他类型属性相关的知识,希望对你有一定的参考价值。

xml注入其他属性

bean:

package com.atguigu.spring;

/**
 * 演示使用set方法进行注入属性
 */
public class Book 
    private String bname;
    private String bauthor;
    private String address;

    public Book(String address) 
        this.address = address;
    

    public String getBname() 
        return bname;
    

    public void setBname(String bname) 
        this.bname = bname;
    

    public String getBauthor() 
        return bauthor;
    

    public void setBauthor(String bauthor) 
        this.bauthor = bauthor;
    

    public void testDemo()
    
        System.out.println(bname + "::" + bauthor+"::"+address);
    


1.字面量

(1)null值

    <bean id = "book" class = "com.atguigu.spring.Book">
        <!--使用property完成属性注入
        name:类里面属性名称
        value:向属性注入的值

        -->
        <property name="bname" value="易筋经">

        </property>

        <property name="bauthor" value="达摩老祖">

        </property>
        <!--null值-->
        <property name="address" >
            <null/>
        </property>


    </bean>

(2)属性值包含特殊符号

  <bean id = "book" class = "com.atguigu.spring.Book">
        <!--使用property完成属性注入
        name:类里面属性名称
        value:向属性注入的值

        -->
        <property name="bname" value="易筋经">

        </property>

        <property name="bauthor" value="达摩老祖">

        </property>


    <!--属性值包含特殊符号
    1.把<>进行转义,&lt;&gt;
    2.把带特殊符号内容写到CDATA
    <![CDATA[你要写入的值]]]>
    -->
    <property name="address"  >
        <value><![CDATA[<<南京>>]]></value>

    </property>


    </bean>

测试结果:

注入属性-外部bean

(1)创建两个类service类和dao类

(2)在service调用dao里面的方法

package com.atguigu.service;

import com.atguigu.dao.UserDao;
import com.atguigu.dao.UserDaoImpl;

public class UserService 



    //spring方法
    //创建UserDao类型属性,生成set方法
    private UserDao userDao;

    public void setUserDao(UserDao userDao)
    
        this.userDao = userDao;
    


    public void add01()
    
        System.out.println("service add");
        userDao.update();
    


    public void add()
    
        System.out.println("service add");

        //传统方法
        //创建UserDao对象
        UserDao userDao = new UserDaoImpl();
        userDao.update();
    


package com.atguigu.dao;

public interface UserDao 
    public void update();



package com.atguigu.dao;

public class UserDaoImpl implements UserDao

    @Override
    public void update() 
        System.out.println("dao update");
    


(3)在spring配置文件中进行配置

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


    <!--1.service和dao对象创建-->
    <bean id = "userService" class = "com.atguigu.service.UserService">

        <!--注入userDao对象
        name属性值:类里面属性名称
        ref属性:创建userDao对象bean标签id值
        -->


        <property name="userDao" ref = "userDaoImpl">

        </property>

    </bean>

    <bean id = "userDaoImpl" class = "com.atguigu.dao.UserDaoImpl">

    </bean>




</beans>

测试:

package com.atguigu.test;

import com.atguigu.service.UserService;
import com.atguigu.spring.Orders;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestBean 

    @Test
    public void testBean01()
    
        //1.加载spring配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");

        //2.获取配置创建的对象
        UserService userService = context.getBean("userService", UserService.class);

       userService.add01();

    



测试结果:

注入属性-内部bean

(1)一对多关系:部门和员工

一个部门有多个员工,一个员工属于一个部门

部门是一,员工是多

(2)在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示

package com.atguigu.bean;

public class Dept 

    private String dname;
    public void setDname(String dname)
    
        this.dname = dname;
    

    @Override
    public String toString() 
        return "Dept" +
                "dname='" + dname + '\\'' +
                '';
    


package com.atguigu.bean;

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);
    



(3)在spring配置文件中进行配置

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



    <!--内部bean-->

    <bean id = "emp" class = "com.atguigu.bean.Emp">


        <!--设置两个普通属性-->
        <property name="ename" value="lucy">

        </property>

        <property name="gender" value="">

        </property>

        <!--设置对象类型属性-->
        <property name="dept">
            <bean id = "dept" class = "com.atguigu.bean.Dept">
                <property name="dname" value="保安部">

                </property>
            </bean>

        </property>



    </bean>



</beans>

测试:

 @Test
    public void testBean02()
    
        //1.加载spring配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");

        //2.获取配置创建的对象
        Emp emp = context.getBean("emp", Emp.class);
        emp.add();


    

注入属性-级联赋值

(1)第一种写法:

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


    <bean id = "emp" class = "com.atguigu.bean.Emp">
        <property name="ename" value="lucy">

        </property>
        <property name="gender" value="">


        </property>

        <property name="dept" ref = "dept">


        </property>


    </bean>
    <bean id = "dept" class = "com.atguigu.bean.Dept">
        <property name="dname" value="财务部">

        </property>
    </bean>
</beans>

(2)第二种写法:

该方法需要get()方法

package com.atguigu.bean;

public class Emp 
    private String ename;
    private String gender;

    //生成dept的get方法
    public Dept getDept() 
        return dept;
    

    //员工属于某一个部门,使用对象形式表示
    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 version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id = "emp" class = "com.atguigu.bean.Emp">
        <property name="ename" value="lucy">

        </property>
        <property name="gender" value="">


        </property>

        <property name="dept" ref = "dept">
        </property>
        <!--需要生成dept的get方法-->
        <property name="dept.dname" value="技术部">

        </property>


    </bean>
    <bean id = "dept" class = "com.atguigu.bean.Dept">
        <property name="dname" value="财务部">

        </property>
    </bean>
</beans>

以上是关于[Spring5]IOC容器_Bean管理XML方式_注入其他类型属性的主要内容,如果未能解决你的问题,请参考以下文章

[Spring5]IOC容器_Bean管理XML方式_注入其他类型属性

[Spring5]IOC容器_Bean管理XML方式_外部属性文件

[Spring5]IOC容器_Bean管理XML方式_创建对象_set注入属性and有参构造注入属性

[Spring5]IOC容器_Bean管理XML方式_注入集合类型属性

[Spring5]IOC容器_Bean管理注解方式_完全注解开发

Spring5——IOC操作Bean管理(基于xml文件)