展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

spring5入门(四):基于xml配置管理bean,注入特殊字符、外部bean、内部bean、级联bean

字面量

  • 注入null
# 方式一:可以在实体类中直接设置为空
private String oname="";

# 方式二:在bean.xml中配置如下
<property name="address">
    <null/>
</property>
  • 测试
# 实体类
public class Book {

    private String bname;

    private String bauthor;

    private String address;

    public void setBname(String bname) {
        this.bname = bname;
    }
    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }

    public void setAddress(String address) {
        this.address = address;
    }

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

}

# bean.xml
    <bean id="book" class="com.ychen.spring.model.Book">
        <property name="bname" value="易筋经"></property>
        <property name="bauthor" value="达摩老祖"></property>
        <property name="address">
            <null/>
        </property>
    </bean>

# 测试方法
    @Test
    public void testBook1() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        Book book = context.getBean("book", Book.class);
        System.out.println(book);
        book.testDemo();
    }

# 测试
com.ychen.spring.model.Book@19932c16
易筋经::达摩老祖::null
  • 注入特殊字符,直接写在value属性中会报错
# 方式一:使用转义字符,例如&lt; &gt;就表示 < >
# 方式二:把带特殊符号内容写到 CDATA

  • 测试:把带特殊符号内容写到 CDATA
# 实体
public class Book {

    private String bname;

    private String bauthor;

    private String address;

    public void setBname(String bname) {
        this.bname = bname;
    }
    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }

    public void setAddress(String address) {
        this.address = address;
    }

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

}

# bean.xml
    <bean id="book" class="com.ychen.spring.model.Book">
        <property name="bname" value="易筋经"></property>
        <property name="bauthor" value="达摩老祖"></property>
        <property name="address">
            <value><![CDATA[<<南京>>]]></value>
        </property>
    </bean>

# 测试方法
    @Test
    public void testBook1() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        Book book = context.getBean("book", Book.class);
        System.out.println(book);
        book.testDemo();
    }

# 测试
com.ychen.spring.model.Book@305ffe9e
易筋经::达摩老祖::<<南京>>

Process finished with exit code 0

注入外部bean

  • 原始方式注入外部bean
# 编写持久层接口
public interface UserDao {

    public void update();

}

# 实现持久层接口
public class UserDaoImpl implements UserDao {

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

}

# 业务层注入持久层
public class UserService {

    public void add() {
        System.out.println("service add...............");
        // 原始方式
        UserDao userDao = new UserDaoImpl();
        userDao.update();
    }

}
  • xml配置注入外部bean
# 编写持久层接口
public interface UserDao {

    public void update();

}

# 实现持久层接口
public class UserDaoImpl implements UserDao {

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

}

# 编写业务层
public class UserService {

    private UserDao userDao;

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

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

}

# xml配置中构建对象(使用2个bean标签构建对象),并用ref属性注入外部bean
    <bean id="userService" class="com.ychen.spring.service.UserService">
        <!--注入 userDao 对象
        name 属性:类里面属性名称
        ref 属性:创建 userDao 对象 bean 标签 id 值
        -->
        <property name="userDao" ref="userDaoImpl"></property>
    </bean>
    <bean id="userDaoImpl" class="com.ychen.spring.dao.UserDaoImpl"></bean>

# 测试方法
    @Test
    public void testBean1() {
        //1 加载spring配置文件
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        //2 获取配置创建的对象
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }

# 控制台
service add...............
dao update...........
  • 使用xml配置内部bean注入,即是在1个bean中直接注入另一个bean
# 编写部门实体类
public class Dept {

    private String dname;

    public void setDname(String dname) {
        this.dname = dname;
    }

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

}

# 编写员工实体类
public class Emp {

    private String ename;

    private String gender;

    //员工属于某一个部门,使用对象形式表示
    private Dept dept;

    //生成dept的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;
    }

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

}

# 配置bean.xml
    <bean id="emp" class="com.ychen.spring.bean.Emp">
        <!--设置两个普通属性-->
        <property name="ename" value="lucy"></property>
        <property name="gender" value="女"></property>
        <!--设置对象类型属性-->
        <property name="dept">
            <bean id="dept" class="com.ychen.spring.bean.Dept">
                <property name="dname" value="安保部"></property>
            </bean>
        </property>
    </bean>

# 编写测试方法
    @Test
    public void testBean2() {
        //1 加载spring配置文件
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        //2 获取配置创建的对象
        Emp emp = context.getBean("emp", Emp.class);
        emp.add();
    }

# 测试
lucy::女::Dept{dname='安保部'}

Process finished with exit code 0

级联赋值

  • 方式1:外部bean赋值
# 编写部门实体类
public class Dept {

    private String dname;

    public void setDname(String dname) {
        this.dname = dname;
    }

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

}

# 编写员工实体类
public class Emp {

    private String ename;

    private String gender;

    //员工属于某一个部门,使用对象形式表示
    private Dept dept;

    //生成dept的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;
    }

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

}

# 配置bean.xml
    <bean id="emp" class="com.ychen.spring.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.ychen.spring.bean.Dept">
        <property name="dname" value="财务部"></property>
    </bean>

# 编写测试方法
    @Test
    public void testBean2() {
        //1 加载spring配置文件
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        //2 获取配置创建的对象
        Emp emp = context.getBean("emp", Emp.class);
        emp.add();
    }

# 测试
lucy::女::Dept{dname='财务部'}

Process finished with exit code 0
  • 方式2
# 编写部门实体类
public class Dept {

    private String dname;

    public void setDname(String dname) {
        this.dname = dname;
    }

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

}

# 编写员工实体类,注意需要生成dept的get方法,后面bean.xml中进行级联注入时会用到
public class Emp {

    private String ename;

    private String gender;

    //员工属于某一个部门,使用对象形式表示
    private Dept dept;

    //生成dept的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;
    }

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

}

# 配置bean.xml
    <bean id="emp" class="com.ychen.spring.bean.Emp">
        <!--设置两个普通属性-->
        <property name="ename" value="lucy"></property>
        <property name="gender" value="女"></property>
        <!--级联赋值-->
        <property name="dept" ref="dept"></property>
        <property name="dept.dname" value="技术部"></property>
    </bean>
    <bean id="dept" class="com.ychen.spring.bean.Dept">
<!--        <property name="dname" value="财务部"></property>-->
    </bean>

# 编写测试方法
    @Test
    public void testBean2() {
        //1 加载spring配置文件
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        //2 获取配置创建的对象
        Emp emp = context.getBean("emp", Emp.class);
        emp.add();
    }

# 测试
lucy::女::Dept{dname='技术部'}

Process finished with exit code 0
posted @ 2022-04-17 10:57  DogLeftover  阅读(87)  评论(0编辑  收藏  举报