Spring设置注入-set方法

Spring设置注入-set方法

实体类domain

package cn.zhc.domain;

public class EmpInfo {
    private int empid;
    private String empname;
    private String empsex;
    private Address address;

    public int getEmpid() {
        return empid;
    }

    public void setEmpid(int empid) {
        this.empid = empid;
    }

    public String getEmpname() {
        return empname;
    }

    public void setEmpname(String empname) {
        this.empname = empname;
    }

    public String getEmpsex() {
        return empsex;
    }

    public void setEmpsex(String empsex) {
        this.empsex = empsex;
    }

    public Address getAddress() {
        return address;
    }

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

    public EmpInfo(int empid, String empname, String empsex, Address address) {
        this.empid = empid;
        this.empname = empname;
        this.empsex = empsex;
        this.address = address;
    }

    public EmpInfo() {
    }
}
package cn.zhc.domain;

public class Address {
    private String homeAddress;
    private String schoolAddress;

    public String getHomeAddress() {
        return homeAddress;
    }

    public void setHomeAddress(String homeAddress) {
        this.homeAddress = homeAddress;
    }

    public String getSchoolAddress() {
        return schoolAddress;
    }

    public void setSchoolAddress(String schoolAddress) {
        this.schoolAddress = schoolAddress;
    }

    public Address(String homeAddress, String schoolAddress) {
        this.homeAddress = homeAddress;
        this.schoolAddress = schoolAddress;
    }

    public Address() {
    }
}

接口dao

package cn.zhc.dao;

public interface EmpInfoDao {
    public void addEmp();
}

实现类Impl

package cn.zhc.dao.Impl;

import cn.zhc.dao.EmpInfoDao;

public class EmpInfoDaoImpl implements EmpInfoDao {
    @Override
    public void addEmp() {
        System.out.println("迪丽迪丽热巴呀");
    }
}

Service层

package cn.zhc.service;

import cn.zhc.dao.EmpInfoDao;

public class EmpService {
    private EmpInfoDao empInfoDao;

    public EmpInfoDao getEmpInfoDao() {
        return empInfoDao;
    }

    public void setEmpInfoDao(EmpInfoDao empInfoDao) {
        this.empInfoDao = empInfoDao;
    }

    public void addEmp(){
        empInfoDao.addEmp();
    }
}

action层

package cn.zhc.action;

import cn.zhc.service.EmpService;

public class EmpAction {
    private EmpService empService;

    public EmpService getEmpService() {
        return empService;
    }

    public void setEmpService(EmpService empService) {
        this.empService = empService;
    }

    public String execute(){
        empService.addEmp();
        return null;
    }
}

XML文件

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="address" class="cn.zhc.domain.Address">
    <property name="homeAddress" value="淅川"></property>
    <property name="schoolAddress" value="郑州"></property>
</bean>

    <bean id="empInfo" class="cn.zhc.domain.EmpInfo">
        <property name="empid" value="1000"></property>
        <property name="empname" value="az"></property>
        <property name="empsex" value=""></property>
        <property name="address" ref="address"></property>
        <!--ref 引用另外一个bean;前一个address是元素里面的属性,后一个address是引用的注入bean的id-->
    </bean>

    <bean id="empInfoDao" class="cn.zhc.dao.Impl.EmpInfoDaoImpl"></bean>
    <bean id="empService" class="cn.zhc.service.EmpService" autowire="byType"></bean>

    <bean id="empAction" class="cn.zhc.action.EmpAction" autowire="byType"></bean>
</beans>

测试类

package cn.zhc.test;

import cn.zhc.action.EmpAction;
import cn.zhc.domain.EmpInfo;
import cn.zhc.service.EmpService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        //test1();
        //test2();
        //test3();
        test4();
    }
    
    private static void test1() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        EmpInfo empInfo= (EmpInfo) ac.getBean("empInfo");
        System.out.println(empInfo.getAddress().getHomeAddress());
    }

    private static void test2() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        EmpService es = (EmpService) ac.getBean("empService");
        es.addEmp();
    }

    private static void test3() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        EmpService es1 = (EmpService) ac.getBean("empService");
        EmpService es2 = (EmpService) ac.getBean("empService");
        System.out.println(es1==es2);
    }

    private static void test4() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        EmpAction ea = (EmpAction) ac.getBean("empAction");
        ea.execute();
    }
}

运行结果:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值