Spring-IOC注入实现-03

spring-IOC注入实现


  1. 创建web项目

  2. 添加spring包(5个)

    • commons-logging-1.2.jar
    • spring-beans-4.3.17.RELEASE.jar
    • spring-context-4.3.17.RELEASE.jar
    • spring-core-4.3.17.RELEASE.jar
    • spring-expression-4.3.17.RELEASE.jar

1、Setter注入

  1. 在src->com.jt.demo1下创建User类。

    添加成员:name,sex,age及setter/getter方法

package com.jt.demo1;

public class User {
    private String name;//用户姓名
    private Integer age;//年龄
    private String sex;//性别

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}
  1. 在src下创建配置文件applicationContext.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean name="user1" class="com.jt.demo1.User">
		<property name="name">
			<value>无语</value>
		</property>
		<property name="age">
			<value>30</value>
		</property>
		<property name="sex">
			<value>女</value>
		</property>
	</bean>

</beans>
  1. 在src->com.jt.demo1下创建TestUser类。
package com.jt.demo1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class TestUser {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        User user=(User)context.getBean("user1");
        System.out.println(user.getName());
        System.out.println(user.getAge());
        System.out.println(user.getSex());
    }
}

运行~

2、构造器注入

  1. 在src->com.jt.demo2下创建User类,并在类中添加构造器。
package com.jt.demo2;

public class User {
    private String name;//用户姓名
    private Integer age;//年龄
    private String sex;//性别

    public User(String name, Integer age, String sex){
        this.name=name;
        this.age=age;
        this.sex=sex;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

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

  1. 在src->applicationContext.xml中添加
<bean name="user2" class="com.jt.demo2.User">
    <constructor-arg  name="name">
        <value>啊这</value>
    </constructor-arg>
    <constructor-arg name="age">
        <value>23</value>
    </constructor-arg>
    <constructor-arg name="sex">
        <value>男</value>
    </constructor-arg>
</bean>
  1. 在src->com.jt.demo2下创建TestUser类。
package com.jt.demo2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestUser {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        User user=(User)context.getBean("user2");
        System.out.println(user.getName());
        System.out.println(user.getAge());
        System.out.println(user.getSex());
    }
}

运行~

3、引用其他Bean实例对象

step1、创建java类

在demo3下创建User类

package com.jt.demo3;

public class User {
    private String name;//用户姓名
    private Integer age;//年龄
    private String sex;//性别

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
    //输出JavaBean的属性值方法
    public void printInfo(){
        System.out.println("用户姓名——"+name);//输出用户的姓名
        System.out.println("用户年龄——"+age);//输出用户的年龄
        System.out.println("用户性别——"+sex);//输出用户的性别
    }
}

在demo3下创建Manager类

  • 添加User类的成员和setter(和getter)方法
package com.jt.demo3;

public class Manager {
    private User user;//注入User对象

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
step2、配置xml

在applicationContext.xml中添加

<bean id="user3" class="com.jt.demo3.User">
    <property name="name">
        <value>张三</value>
    </property>
    <property name="age">
        <value>11</value>
    </property>
    <property name="sex">
        <value>男</value>
    </property>
</bean>

<bean name="manager" class="com.jt.demo3.Manager">
    <property name="user">
        <ref bean="user3"/>
    </property>
</bean>

ref标签引用其他javaBean的实例对象

step3、测试

在demo3下创建TestUser类

package com.jt.demo3;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestUser {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        Manager manager=(Manager)ctx.getBean("manager");
        manager.getUser().printInfo();//获取user3对象,并调用其方法
    }
}

运行~

posted @ 2020-11-30 22:46  jt_coder  阅读(57)  评论(0)    收藏  举报