Spring依赖注入的讲解以及代码实现(二)

7 篇文章 0 订阅
6 篇文章 0 订阅

3、通过spring方式来调用sayhello方法。(spring  di,ioc

   首先配置环境,和导入相对应的jar包

(commons-logging-1.1.1.jar、spring-aop-4.3.7.RELEASE.jar、

 spring-aspects-4.3.7.RELEASE.jar、spring-beans-4.3.7.RELEASE.jar、

spring-context-4.3.7.RELEASE.jar、spring-core-4.3.7.RELEASE.jar、spring-expression-4.3.7.RELEASE.jar),

再配置相关的spring.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"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

    <bean id="helloS" class="com.xiao.dao.SayHelloImp"></bean>

</beans>

 最后测试

package com.xiao.test;

import com.xiao.dao.SayHello;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringJarTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("spring01.xml");
        SayHello h = ac.getBean("helloS", SayHello.class);
        h.hello("星星");
    }
}

运行结果


4、自定义2个实体类。然后一个实体类作为另一个实体类的属性。实现里面的某个方法,得到输出属性的值。

  • 实体类里要有无参和有参构造方法,以及set/get方法
  • Address实体类
package com.xiao.bean;

public class Address {

    private String homeAddress;
    private String schoolAddress;

    public Address() {

    }

    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;
    }
}
  • Emp实体类
package com.xiao.bean;

public class Emp {
    private int eid;
    private String ename;
    private String sex;
    private Address address;

    public Emp() {

    }

    public int getEid() {
        return eid;
    }

    public void setEid(int eid) {
        this.eid = eid;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Emp(int eid, String ename, String sex, Address address) {
        this.eid = eid;
        this.ename = ename;
        this.sex = sex;
        this.address = address;
    }

    public Address getAddress() {
        return address;
    }

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

    @Override
    public String toString() {
        return "Emp{" +
                "eid=" + eid +
                ", ename='" + ename + '\'' +
                ", sex='" + sex + '\'' +
                ", address=" + address +
                '}';
    }
}
  • 配置spring.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"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

    <bean id="helloS" class="com.xiao.dao.SayHelloImp"></bean>

	<bean id="address" class="com.xiao.bean.Address">
		<property name="homeAddress" value="九城老街"></property>
		<property name="schoolAddress" value="星光大道"></property>
	</bean>
	<bean id="emp" class="com.xiao.bean.Emp">
		<property name="eid" value="1001"></property>
		<property name="ename" value="大BOSS"></property>
		<property name="sex" value="男"></property>
		<property name="address" ref="address"></property>

	</bean>

</beans>
  • 测试
package com.xiao.test;

import com.xiao.bean.Emp;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringBeanTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("spring01.xml");
        Emp emp = (Emp) ac.getBean("emp");
        System.out.println(emp.getAddress().getHomeAddress());
    }
}
  • 运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值