峰Spring4学习spring自动装配
Posted 有点懒惰的大青年
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了峰Spring4学习spring自动装配相关的知识,希望对你有一定的参考价值。
一、自动装配:
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学习spring自动装配的主要内容,如果未能解决你的问题,请参考以下文章
Spring4学习笔记 - 配置Bean - 自动装配 关系 作用域 引用外部属性文件