spring之方法注入,单例和多例,方法替换

Spring bean 作用域默认是 单例 singleton; 可以通过配置 prototype ,实现多例;

People people=(People)ac.getBean("people1");
        People people2=(People)ac.getBean("people1");
        System.out.println(people.getDog()==people2.getDog());

 

打印的输出是true

 

更改beans.xml文件可以是spring每次创建的bean是新的(多例),加上scope="prototype"这句话

 

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

输出为false

 

方法注入 lookup-method,输出为true

 

<?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.java1234.entity.Dog" scope="prototype">
        <property name="name" value="Jack"></property>
    </bean>
    
    
    
    <bean id="people1" class="com.java1234.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>

 

People类代码

package com.java1234.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() + "]";
    }
    

}
 

 

方法替换(用people2中dog的名字Tom替换掉people的狗名字jack)

 

package com.java1234.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() + "]";
    }    

}
 

People2.java

package com.java1234.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;
    }

}

 

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.java1234.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.java1234.entity.People2"></bean>
</beans>

 

Test类

package com.java1234.test;

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

import com.java1234.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());
    }
    

}

//输出结果位Tom

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值