spring4学习IOC之自动装配(五)

一、依赖注入之自动装配
这里写图片描述

1.编写People类和Dog类

package com.newbeedaly.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class People {

    private int id;
    private String name;
    private int age;
    private Dog dog;



    public People() {
        super();
        // TODO Auto-generated constructor stub
    }


    public People(Dog dog) {
        super();
        System.out.println("constructor");
        this.dog = dog;
    }


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    @Override
    public String toString() {
        return "People [id=" + id + ", name=" + name + ", age=" + age
                + ", dog=" + dog.getName() + "]";
    }








}
package com.newbeedaly.entity;

public class Dog {

    private String name;

    public String getName() {
        return name;
    }

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


}

2.配置Beans.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
        http://www.springframework.org/schema/beans/spring-beans.xsd"
        default-autowire="constructor">
        <!-- 自动装配在此配置,一般不推荐使用 -->

    <bean id="dog2" class="com.newbeedaly.entity.Dog">
        <property name="name" value="Jack"></property>
    </bean>



    <bean id="people1" class="com.newbeedaly.entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>

    </bean>

</beans>

3.编写测试类

package com.newbeedaly.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.newbeedaly.entity.People;

public class T {

    private ApplicationContext ac;

    @Before
    public void setUp() throws Exception {
        ac=new ClassPathXmlApplicationContext("beans.xml");
    }

    @Test
    public void test1() {
        People people=(People)ac.getBean("people1");
        System.out.println(people);
    }


}

二、依赖注入之方法注入

这里写图片描述

1.编写Dog类和People类

package com.newbeedaly.entity;

public class Dog {

    private String name;

    public String getName() {
        return name;
    }

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


}
package com.newbeedaly.entity;


public abstract class People {

    private int id;
    private String name;
    private int age;
    private Dog dog;


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public abstract Dog getDog();

    public void setDog(Dog dog) {
        this.dog = dog;
    }
    @Override
    public String toString() {
        return "People [id=" + id + ", name=" + name + ", age=" + age
                + ", dog=" + dog.getName() + "]";
    }








}

2.配置beans.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
        http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="dog" class="com.newbeedaly.entity.Dog" scope="prototype">
        <property name="name" value="Jack"></property>
    </bean>



    <bean id="people1" class="com.newbeedaly.entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
        <lookup-method name="getDog" bean="dog"/>
    </bean>

</beans>

3.编写测试类

package com.newbeedaly.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.newbeedaly.entity.People;

public class T {

    private ApplicationContext ac;

    @Before
    public void setUp() throws Exception {
        ac=new ClassPathXmlApplicationContext("beans.xml");
    }

    @Test
    public void test1() {
        People people=(People)ac.getBean("people1");
        People people2=(People)ac.getBean("people1");
        System.out.println(people.getDog()==people2.getDog());

        System.out.println(ac.getBean("dog")==ac.getBean("dog"));
    }


}

三、依赖注入之方法替换(几乎不用)
1.配置beans.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
        http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="people1" class="com.newbeedaly.entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
        <replaced-method name="getDog" replacer="people2"></replaced-method>
    </bean>

    <bean id="people2" class="com.newbeedaly.entity.People2"></bean>
</beans>

2.编写对应的类

package com.newbeedaly.entity;


public class People {

    private int id;
    private String name;
    private int age;
    private Dog dog;


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public Dog getDog() {
        Dog dog=new Dog();
        dog.setName("Jack");
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    @Override
    public String toString() {
        return "People [id=" + id + ", name=" + name + ", age=" + age
                + ", dog=" + dog.getName() + "]";
    }




}
package com.newbeedaly.entity;

import java.lang.reflect.Method;

import org.springframework.beans.factory.support.MethodReplacer;


public class People2 implements MethodReplacer {

    @Override
    public Object reimplement(Object arg0, Method arg1, Object[] arg2)
            throws Throwable {
        Dog dog=new Dog();
        dog.setName("Tom");
        return dog;
    }


}
package com.newbeedaly.entity;

public class Dog {

    private String name;

    public String getName() {
        return name;
    }

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


}

3.编写测试类

package com.newbeedaly.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.newbeedaly.entity.People;

public class T {

    private ApplicationContext ac;

    @Before
    public void setUp() throws Exception {
        ac=new ClassPathXmlApplicationContext("beans.xml");
    }

    @Test
    public void test1() {
        People people=(People)ac.getBean("people1");
        System.out.println(people.getDog().getName());
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值