一、自动装配:

Model类:

People.java:

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

Dog.java:

package com.cy.entity;

public class Dog {
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }

beans.xml  spring加载bean配置文件:

<?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="byName"> <bean id="dog" class="com.cy.entity.Dog">
<property name="name" value="Jack"></property>
</bean> <bean id="dog2" class="com.cy.entity.Dog">
<property name="name" value="Tom"></property>
</bean> <bean id="people1" class="com.cy.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
</bean> </beans>

测试代码:

T.java:

package com.cy.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cy.entity.People; public class T { private ApplicationContext ac; @Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
} @Test
public void test() {
People people = (People) ac.getBean("people1");
System.out.println(people); //People [id=1, name=张三, age=11, dog=Jack]
} }

二、方法注入:

<?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"> <!-- prototype配置dog为多例模式 -->
<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中的getDog为抽象方法,这样在getDog时,由spring来注入具体的dog:

People.java:

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() + "]";
} }

测试代码:

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");
People people2=(People)ac.getBean("people1");
System.out.println(people.getDog()==people2.getDog()); //false people每次装配的dog都不一样 System.out.println(ac.getBean("dog")==ac.getBean("dog")); //false  dog为多例,每次获取都不一样
} }

三、方法替换,不常用,了解下就行了:

People.java:

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><!-- people1中的getDog方法被替换,被people2中的方法替换掉了 -->
</bean> <bean id="people2" class="com.java1234.entity.People2"></bean>
</beans>

测试代码:

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());
} }

峰Spring4学习(4)spring自动装配的更多相关文章

  1. Spring学习(三)-----Spring自动装配Beans

    在Spring框架,可以用 auto-wiring 功能会自动装配Bean.要启用它,只需要在 <bean>定义“autowire”属性. <bean id="custom ...

  2. 【spring 注解驱动开发】spring自动装配

    尚学堂spring 注解驱动开发学习笔记之 - 自动装配 自动装配 1.自动装配-@Autowired&@Qualifier&@Primary 2.自动装配-@Resource& ...

  3. Spring 自动装配 Bean

    Spring3系列8- Spring 自动装配 Bean 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiri ...

  4. spring 自动装配 default-autowire=&quot;byName/byType&quot;

    <PRE class=html name="code">spring 自动装配 default-autowire="byName/byType"   ...

  5. Spring自动装配Bean详解

    1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiring ‘byType 4.      Auto-Wirin ...

  6. Spring自动装配----注解装配----Spring自带的@Autowired注解

    Spring自动装配----注解装配----Spring自带的@Autowired注解 父类 package cn.ychx; public interface Person { public voi ...

  7. Spring系列七:Spring 自动装配

    相思相见知何日?此时此夜难为情. 概述 在Spring框架中,在配置文件中声明bean的依赖关系是一个很好的做法,因为Spring容器能够自动装配协作bean之间的关系.这称为spring自动装配. ...

  8. Spring自动装配(二)

    为什么Spring要支持Autowire(自动装配) 先写几个类,首先定义一个Animal接口表示动物: 1 public interface Animal { 2 3 public void eat ...

  9. Spring自动装配歧义性笔记

    Spring自动装配歧义性笔记 如果系统中存在两个都实现了同一接口的类,Spring在进行@Autowired自动装配的时候,会选择哪一个?如下: // 一下两个类均被标记为bean @Compone ...

随机推荐

  1. XML解析之XPath

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  2. XML解析之JAXP

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  3. C一次将整个文件读入内存

    最近工作,有个需求需要将YUV的整个文件读入内存,然后处理这些YUV数据,一种比较有效的方法如下: #include <stdio.h> #include <stdlib.h> ...

  4. Python int操作

    a = 1 # 1 print(a.bit_length()) #计算一个数字的二进制长度.没啥用

  5. JavaScript权威指南——跳转语句

    前言:JavaScript中有一类语句叫做跳转语句.从名称就可以看出,它使得JavaScript的执行可以从一个位置跳转到另一个位置. return语句让解释器跳出循环体的执行,并提供本次调用的返回值 ...

  6. SWIFT Enumeration(1)

    Swift中定义Enumeration跟其它语言挺类似的,看如下定义一个星期的Enumeration enum Day:Int{ case Monday = 1, Tuesday,Wednesday, ...

  7. HDU 4585

    http://acm.hdu.edu.cn/showproblem.php?pid=4585 从原来的人中找出战斗数值最接近的,输出他们两人的序号 要在logn的复杂度完成查找,我用的是set,当然用 ...

  8. 【论文解读】行人检测:What Can Help Pedestrian Detection?(CVPR'17)

    前言 本篇文章出自CVPR2017,四名作者为Tsinghua University,Peking University, 外加两名来自Megvii(旷视科技)的大佬. 文章中对能够帮助行人检测的ex ...

  9. 读博 在没有导师PUSH的情况下该何去何从?

    读博已有两月之久,与导师也是仅有的一面之缘,短短数分钟谈话大致总结便是看看基础知识,再然后就没有什么了,突然之间有些小懵逼.突然间感慨这就是我的博士生涯的生活,这就没有啦,以后就这么过啦?在读博士之前 ...

  10. Robust Tracking via Weakly Supervised Ranking SVM

    参考文献:Yancheng Bai and Ming Tang. Robust Tracking via Weakly Supervised Ranking SVM Abstract 通常的算法:ut ...