(二)Spring 之IOC 详解
第一节:spring ioc 简介
IOC(控制反转:Inversion of Control),又称作依赖注入dependency injection( DI ),是一种重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring 框架的核心。
第二节:spring ioc 实例讲解
package com.wishwzp.service;
public interface Tester {
public void test();
}
package com.wishwzp.service;
public class LiSi implements Tester{
public void test(){
System.out.println("李四-测试程序");
}
}
package com.wishwzp.service;
public class ZhangSan implements Tester{
public void test(){
System.out.println("张三-测试程序");
}
}
package com.wishwzp.service;
public class JavaWork {
private Tester tester;
public void setTester(Tester tester) {
this.tester = tester;
}
public void doTest(){
// ZhangSan zhangsan = new ZhangSan();
// zhangsan.test();
tester.test();
}
}
package com.wishwzp.test; import com.wishwzp.service.JavaWork;
import com.wishwzp.service.LiSi; public class Test { /**
* 主管执行命令
* @param args
*/
public static void main(String[] args) {
JavaWork javawork = new JavaWork();
javawork.setTester(new LiSi());
javawork.doTest();
}
}

package com.wishwzp.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.service.JavaWork;
import com.wishwzp.service.Tester; public class Test2 { public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml"); // Tester t = null;
// t=(Tester) ac.getBean("zhangsan");
// t.test(); JavaWork javaWork=(JavaWork)ac.getBean("javaWork");
javaWork.doTest(); }
}

第三节:装配一个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"> <bean id="zhangsan" class="com.wishwzp.service.ZhangSan"></bean> <bean id="lisi" class="com.wishwzp.service.LiSi"></bean> <bean id="javaWork" class="com.wishwzp.service.JavaWork">
<property name="tester" ref="zhangsan"></property>
</bean> </beans>
第四节:依赖注入
1,属性注入;
2,构造函数注入;(通过类型;通过索引;联合使用)
3,工厂方法注入;(非静态工厂,静态工厂)
4,泛型依赖注入;(Spring4 整合Hibernate4 的时候顺带说)
package com.wishwzp.entity;
public class People {
private int id;
private String name;
private int age;
public People() {
super();
// TODO Auto-generated constructor stub
}
public People(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
package com.wishwzp.factory;
import com.wishwzp.entity.People;
public class PeopleFactory {
public People createPeople(){
People p=new People();
p.setId(5);
p.setName("小七");
p.setAge(77);
return p;
}
}
package com.wishwzp.factory;
import com.wishwzp.entity.People;
public class PeopleFactory2 {
public static People createPeople(){
People p=new People();
p.setId(8);
p.setName("小八");
p.setAge(88);
return p;
}
}
<?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="people" class="com.wishwzp.entity.People"></bean>
<!-- 属性注入 -->
<bean id="people2" class="com.wishwzp.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
</bean>
<!-- 构造方法注入----通过类型-->
<bean id="people3" class="com.wishwzp.entity.People">
<constructor-arg type="int" value="2"></constructor-arg>
<constructor-arg type="String" value="李四"></constructor-arg>
<constructor-arg type="int" value="22"></constructor-arg>
</bean>
<!-- 构造方法注入---通过索引-->
<bean id="people4" class="com.wishwzp.entity.People">
<constructor-arg index="0" value="3"></constructor-arg>
<constructor-arg index="1" value="王五"></constructor-arg>
<constructor-arg index="2" value="55"></constructor-arg>
</bean>
<!-- 构造方法注入---联合使用-->
<bean id="people5" class="com.wishwzp.entity.People">
<constructor-arg index="0" type="int" value="4"></constructor-arg>
<constructor-arg index="1" type="String" value="招六"></constructor-arg>
<constructor-arg index="2" type="int" value="66"></constructor-arg>
</bean>
<!-- 工厂方法注入 -->
<bean id="peopleFactory" class="com.wishwzp.factory.PeopleFactory"></bean>
<!-- 非静态工厂 -->
<bean id="people7" factory-bean="peopleFactory" factory-method="createPeople"></bean>
<!-- 静态工厂 -->
<bean id="people8" class="com.wishwzp.factory.PeopleFactory2" factory-method="createPeople"></bean> </beans>
package com.wishwzp.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.entity.People; public class Test2 { public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
People people=(People)ac.getBean("people");
System.out.println(people); // 属性注入
People people2=(People)ac.getBean("people2");
System.out.println(people2); // 构造方法注入
People people3=(People)ac.getBean("people3");
System.out.println(people3); People people4=(People)ac.getBean("people4");
System.out.println(people4); People people5=(People)ac.getBean("people5");
System.out.println(people5); // 工厂方法注入
People people7=(People)ac.getBean("people7");
System.out.println(people7); People people8=(People)ac.getBean("people8");
System.out.println(people8);
}
}

第五节:注入参数
1,基本类型值;
2,注入bean;
3,内部bean;
4,null 值;
5,级联属性;
6,集合类型属性;
1,基本类型值;
package com.wishwzp.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.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);
} }
T.java
<?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.wishwzp.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
</bean> </beans>
beans.xml
package com.wishwzp.entity;
public class People {
private int id;
private String name;
private int age;
public People() {
super();
// TODO Auto-generated constructor stub
}
public People(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
People.java
运行结果显示:

2,注入bean;
package com.wishwzp.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.entity.People; public class T { private ApplicationContext ac; @Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
} // 注入bean
@Test
public void test2() {
People people=(People)ac.getBean("people2");
System.out.println(people);
} }
T.java
<?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="dog1" class="com.wishwzp.entity.Dog">
<property name="name" value="Jack"></property>
</bean> <bean id="people2" class="com.wishwzp.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三2"></property>
<property name="age" value="11"></property>
<property name="dog" ref="dog1"></property>
</bean>
</beans>
beans.xml
package com.wishwzp.entity;
public class Dog {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Dog.java
package com.wishwzp.entity;
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(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
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() + "]";
}
}
People.java
运行结果显示:

3,内部bean;
package com.wishwzp.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.entity.People; public class T { private ApplicationContext ac; @Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
} // 内部bean
@Test
public void test3() {
People people=(People)ac.getBean("people3");
System.out.println(people);
} }
T.java
<?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="people3" class="com.wishwzp.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<property name="dog">
<bean class="com.wishwzp.entity.Dog">
<property name="name" value="Tom"></property>
</bean>
</property>
</bean>
</beans>
beans.xml
package com.wishwzp.entity;
public class Dog {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Dog.java
package com.wishwzp.entity;
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(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
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() + "]";
}
}
People.java
运行结果显示:

4,null 值;
package com.wishwzp.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.entity.People; public class T { private ApplicationContext ac; @Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
} // 注入null
@Test
public void test4() {
People people=(People)ac.getBean("people4");
System.out.println(people);
} }
T.java
<?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="people4" class="com.wishwzp.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<property name="dog">
<null></null>
</property>
</bean>
</beans>
beans.xml
package com.wishwzp.entity;
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(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
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 + "]";
}
}
People.java
package com.wishwzp.entity;
public class Dog {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Dog.java
运行结果显示:

5,级联属性;
package com.wishwzp.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.entity.People; public class T { private ApplicationContext ac; @Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
} // 级联属性
@Test
public void test5() {
People people=(People)ac.getBean("people5");
System.out.println(people);
} }
T.java
<?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 -->
<bean id="dog1" class="com.wishwzp.entity.Dog">
<!-- <property name="name" value="Jack2"></property> -->
</bean>
<!-- 引用bean -->
<bean id="people5" class="com.wishwzp.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<!-- 使用property的ref属性建立bean之间的引用关系 -->
<property name="dog" ref="dog1"></property>
<!-- 为级联属性赋值,首先必须引用级联属性的类 -->
<property name="dog.name" value="Jack2"></property>
</bean>
</beans>
beans.xml
package com.wishwzp.entity;
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(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
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() + "]";
}
}
People.java
package com.wishwzp.entity;
public class Dog {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Dog.java
运行结果显示:

6,集合类型属性;
package com.wishwzp.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.entity.People; public class T { private ApplicationContext ac; @Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
} // 注入集合
@Test
public void test6() {
People people=(People)ac.getBean("people6");
System.out.println(people);
} }
T.java
<?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="dog1" class="com.wishwzp.entity.Dog">
<property name="name" value="Jack"></property>
</bean> <bean id="people6" class="com.wishwzp.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<property name="dog" ref="dog1"></property>
<property name="hobbies">
<list>
<value>唱歌</value>
<value>跳舞</value>
</list>
</property>
<property name="loves">
<set>
<value>唱歌2</value>
<value>跳舞2</value>
</set>
</property>
<property name="works">
<map>
<entry>
<key><value>上午</value></key>
<value>写代码</value>
</entry>
<entry>
<key><value>下午</value></key>
<value>测试代码</value>
</entry>
</map>
</property>
<property name="addresses">
<props>
<prop key="address1">aaaaa</prop>
<prop key="address2">bbbbb</prop>
</props>
</property>
</bean>
</beans>
beans.xml
package com.wishwzp.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;
private List<String> hobbies=new ArrayList<String>();
private Set<String> loves=new HashSet<String>();
private Map<String,String> works=new HashMap<String,String>();
private Properties addresses=new Properties(); public People() {
super();
// TODO Auto-generated constructor stub
} public People(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
} 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;
} public List<String> getHobbies() {
return hobbies;
} public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
} public Set<String> getLoves() {
return loves;
} public void setLoves(Set<String> loves) {
this.loves = loves;
} public Map<String, String> getWorks() {
return works;
} public void setWorks(Map<String, String> works) {
this.works = works;
} public Properties getAddresses() {
return addresses;
} public void setAddresses(Properties addresses) {
this.addresses = addresses;
} @Override
public String toString() {
return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + ", hobbies=" + hobbies
+ ", loves=" + loves + ", works=" + works + ", addresses=" + addresses + "]";
} }
People.java
package com.wishwzp.entity;
public class Dog {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Dog.java
运行结果显示:
四月 24, 2017 9:21:01 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7c53a9eb: startup date [Mon Apr 24 21:21:01 CST 2017]; root of context hierarchy
四月 24, 2017 9:21:01 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
People [id=1, name=张三, age=11, dog=Jack, hobbies=[唱歌, 跳舞], loves=[唱歌2, 跳舞2], works={上午=写代码, 下午=测试代码}, addresses={address2=bbbbb, address1=aaaaa}]
第六节:Spring 自动装配
通过配置default-autowire 属性,Spring IOC 容器可以自动为程序注入bean;默认是no,不启用自动装配;
default-autowire 的类型有byName,byType,constructor;
byName:通过名称进行自动匹配;
byType:根据类型进行自动匹配;
constructor:和byType 类似,只不过它是根据构造方法注入而言的,根据类型,自动注入;
建议:自动装配机制慎用,它屏蔽了装配细节,容易产生潜在的错误;
1.byName:通过名称进行自动匹配;
package com.wishwzp.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.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);
}
}
T.java
<?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.wishwzp.entity.Dog">
<property name="name" value="Jack"></property>
</bean> <bean id="dog2" class="com.wishwzp.entity.Dog">
<property name="name" value="Tom"></property>
</bean> <bean id="people1" class="com.wishwzp.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
</bean> </beans>
beans.xml
package com.wishwzp.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() + "]";
}
}
People.java
package com.wishwzp.entity;
public class Dog {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Dog.java
运行结果显示:

2.byType:根据类型进行自动匹配;
package com.wishwzp.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.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);
}
}
T.java
<?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="byType"> <!--
<bean id="dog" class="com.wishwzp.entity.Dog">
<property name="name" value="Jack"></property>
</bean>
--> <bean id="dog2" class="com.wishwzp.entity.Dog">
<property name="name" value="Tom"></property>
</bean> <bean id="people1" class="com.wishwzp.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
</bean> </beans>
beans.xml
package com.wishwzp.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() + "]";
}
}
People.java
package com.wishwzp.entity;
public class Dog {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Dog.java
运行结果显示:

3.constructor:和byType 类似,只不过它是根据构造方法注入而言的,根据类型,自动注入;
package com.wishwzp.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.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);
}
}
T.java
<?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="dog" class="com.wishwzp.entity.Dog">
<property name="name" value="Jack"></property>
</bean>
--> <bean id="dog2" class="com.wishwzp.entity.Dog">
<property name="name" value="Tom"></property>
</bean> <bean id="people1" class="com.wishwzp.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
</bean> </beans>
beans.xml
package com.wishwzp.entity;
public class People {
private int id;
private String name;
private int age;
private Dog dog;
public People() {
super();
}
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() + "]";
}
}
People.java
package com.wishwzp.entity;
public class Dog {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Dog.java
运行结果显示:

第七节:方法注入
Spring bean 作用域默认是单例singleton; 可以通过配置prototype ,实现多例;
方法注入lookup-method
1.我们默认使用scope="singleton",也就是默认不写。我们发现每次获取都是一样的值,都是相等的。
T.java:
package com.wishwzp.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.entity.Dog; public class T { private ApplicationContext ac; @Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
} @Test
public void test1() {
Dog dog1 = (Dog) ac.getBean("dog");
Dog dog2 = (Dog) ac.getBean("dog");
System.out.println(dog1==dog2);
} }
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.wishwzp.entity.Dog"><!-- 不写的话,默认是使用scope="singleton" -->
<property name="name" value="Jack"></property>
</bean> </beans>
Dog.java:
package com.wishwzp.entity;
public class Dog {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
运行结果显示:
true
2.我们使用scope="prototype"。我们发现每次获取都是不一样的值,都是不相等的。
T.java:
package com.wishwzp.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.entity.Dog; public class T { private ApplicationContext ac; @Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
} @Test
public void test1() {
Dog dog1 = (Dog) ac.getBean("dog");
Dog dog2 = (Dog) ac.getBean("dog");
System.out.println(dog1==dog2);
} }
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.wishwzp.entity.Dog" scope="prototype">
<property name="name" value="Jack"></property>
</bean> </beans>
Dog.java:
package com.wishwzp.entity;
public class Dog {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
运行结果显示:
false
3.当我们使用注入bean的时候,我们发现虽然用的是scope="prototype",但是我们发现每次获取都是一样的值,都是相等的。
T.java:
package com.wishwzp.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.entity.People; public class T { private ApplicationContext ac; @Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
} @Test
public void test1() {
People people1 = (People) ac.getBean("people1");
People people2 = (People) ac.getBean("people1");
System.out.println(people1.getDog()==people2.getDog());
} }
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.wishwzp.entity.Dog" scope="prototype">
<property name="name" value="Jack"></property>
</bean> <bean id="people1" class="com.wishwzp.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<property name="dog" ref="dog"></property>
</bean> </beans>
People.java:
package com.wishwzp.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.wishwzp.entity;
public class Dog {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
运行结果显示:
true
3.当我们使用注入bean的时候,我们发现虽然用的是scope="prototype",但是我们发现每次获取都是一样的值,都是相等的。(这个时候我们需要改变每次获取的都是不一样的值,都是不相等的)
这里我们需要将People.java设置成public abstract class People {}类,并且将getDog设置成public abstract Dog getDog();,然后修改beans.xml中<lookup-method name="getDog" bean="dog"/>,这里的name="getDog"就是public abstract Dog getDog();的getDog(),bean="dog"就是beans.xml配置文件中上面一个<bean id="dog" >的。
T.java:
package com.wishwzp.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.entity.People; public class T { private ApplicationContext ac; @Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
} @Test
public void test1() {
People people1 = (People) ac.getBean("people1");
People people2 = (People) ac.getBean("people1");
System.out.println(people1.getDog()==people2.getDog());
} }
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.wishwzp.entity.Dog" scope="prototype">
<property name="name" value="Jack"></property>
</bean> <bean id="people1" class="com.wishwzp.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<!--
<property name="dog" ref="dog"></property>
-->
<lookup-method name="getDog" bean="dog"/>
</bean> </beans>
People.java:
package com.wishwzp.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() + "]";
}
}
Dog.java:
package com.wishwzp.entity;
public class Dog {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
运行结果显示:
false
第八节:方法替换
T.java:
package com.wishwzp.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.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());
} }
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.wishwzp.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.wishwzp.entity.People2"></bean>
</beans>
People2.java:
package com.wishwzp.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;
}
}
People.java:
package com.wishwzp.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() + "]";
}
}
Dog.java:
package com.wishwzp.entity;
public class Dog {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
运行结果显示:
Tom
第九节:bean 之间的关系
1,继承;
2,依赖;
3,引用;
1,继承;
T.java:
package com.wishwzp.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.entity.People; public class T { private ApplicationContext ac; @Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
} @Test
public void test1() {
People zhangsan=(People)ac.getBean("zhangsan");
System.out.println(zhangsan); People lisi=(People)ac.getBean("lisi");
System.out.println(lisi);
} }
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="abstractPeople" class="com.wishwzp.entity.People" abstract="true"><!-- 抽象的类 -->
<property name="className" value="高三5班"></property>
<property name="age" value="19"></property>
</bean> <bean id="zhangsan" parent="abstractPeople"><!-- 继承了抽象类abstractPeople -->
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean> <bean id="lisi" parent="abstractPeople"><!-- 继承了抽象类abstractPeople,并重写覆盖了age属性 -->
<property name="id" value="2"></property>
<property name="name" value="李四"></property>
<property name="age" value="20"></property>
</bean>
</beans>
People.java:
package com.wishwzp.entity;
public class People {
private int id;
private String name;
private int age;
private String className;
public People() {
System.out.println("初始化People");
}
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 String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + ", age=" + age + ", className=" + className + "]";
}
}
运行结果显示:
初始化People
初始化People
People [id=1, name=张三, age=19, className=高三5班]
People [id=2, name=李四, age=20, className=高三5班]
2,依赖;
T.java:
package com.wishwzp.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.entity.People; public class T { private ApplicationContext ac; @Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
} @Test
public void test1() {
People zhangsan=(People)ac.getBean("zhangsan");
System.out.println(zhangsan); People lisi=(People)ac.getBean("lisi");
System.out.println(lisi);
} }
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="abstractPeople" class="com.wishwzp.entity.People" abstract="true"><!-- 抽象的类 -->
<property name="className" value="高三5班"></property>
<property name="age" value="19"></property>
</bean> <bean id="zhangsan" parent="abstractPeople"><!-- 继承了抽象类abstractPeople -->
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean> <bean id="lisi" parent="abstractPeople"><!-- 继承了抽象类abstractPeople,并重写覆盖了age属性 -->
<property name="id" value="2"></property>
<property name="name" value="李四"></property>
<property name="age" value="20"></property>
</bean> <bean id="authority" class="com.wishwzp.service.Authority"></bean>
</beans>
People.java:
package com.wishwzp.entity;
public class People {
private int id;
private String name;
private int age;
private String className;
public People() {
System.out.println("初始化People");
}
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 String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + ", age=" + age + ", className=" + className + "]";
}
}
Authority.java:
package com.wishwzp.service;
public class Authority {
public Authority() {
System.out.println("获取权限");
}
}
运行结果显示:
初始化People
初始化People
获取权限
People [id=1, name=张三, age=19, className=高三5班]
People [id=2, name=李四, age=20, className=高三5班]
----------------------
但是根据我们的需求,必须要在初始化people之前获取权限才可以。这时候我们就需要依赖关系了,people需要在初始化之前依赖authority。
所以我们修改一下beans.xml文件。
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="abstractPeople" class="com.wishwzp.entity.People" abstract="true"><!-- 抽象的类 -->
<property name="className" value="高三5班"></property>
<property name="age" value="19"></property>
</bean> <bean id="zhangsan" parent="abstractPeople" depends-on="authority"><!-- 继承了抽象类abstractPeople --><!-- 添加了依赖关系depends-on="authority" -->
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean> <bean id="lisi" parent="abstractPeople"><!-- 继承了抽象类abstractPeople,并重写覆盖了age属性 -->
<property name="id" value="2"></property>
<property name="name" value="李四"></property>
<property name="age" value="20"></property>
</bean> <bean id="authority" class="com.wishwzp.service.Authority"></bean>
</beans>
运行结果显示:
获取权限
初始化People
初始化People
People [id=1, name=张三, age=19, className=高三5班]
People [id=2, name=李四, age=20, className=高三5班]
3,引用;
T.java:
package com.wishwzp.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.entity.People; public class T { private ApplicationContext ac; @Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
} @Test
public void test1() {
People zhangsan=(People)ac.getBean("zhangsan");
System.out.println(zhangsan); People lisi=(People)ac.getBean("lisi");
System.out.println(lisi);
} }
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.wishwzp.entity.Dog">
<property name="name" value="jack"></property>
</bean> <bean id="abstractPeople" class="com.wishwzp.entity.People" abstract="true"><!-- 抽象的类 -->
<property name="className" value="高三5班"></property>
<property name="age" value="19"></property>
</bean> <bean id="zhangsan" parent="abstractPeople" depends-on="authority"><!-- 继承了抽象类abstractPeople --><!-- 添加了依赖关系depends-on="authority" -->
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean> <bean id="lisi" parent="abstractPeople"><!-- 继承了抽象类abstractPeople,并重写覆盖了age属性 -->
<property name="id" value="2"></property>
<property name="name" value="李四"></property>
<property name="age" value="20"></property>
<!-- 这里引用了dog -->
<property name="dog" ref="dog"></property>
</bean> <bean id="authority" class="com.wishwzp.service.Authority"></bean>
</beans>
People.java:
package com.wishwzp.entity;
public class People {
private int id;
private String name;
private int age;
private String className;
private Dog dog;
public People() {
System.out.println("初始化People");
}
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 String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
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 + ", className=" + className + ", dog=" + dog
+ "]";
}
}
Authority.java:
package com.wishwzp.service;
public class Authority {
public Authority() {
System.out.println("获取权限");
}
}
Dog.java:
package com.wishwzp.entity;
public class Dog {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
运行结果显示:
获取权限
初始化People
初始化People
People [id=1, name=张三, age=19, className=高三5班, dog=null]
People [id=2, name=李四, age=20, className=高三5班, dog=com.wishwzp.entity.Dog@78e67e0a]
第十节:bean 作用范围
1,singleton Spring ioc 容器中仅有一个Bean 实例,Bean 以单例的方式存在;
2,prototype 每次从容器中调用Bean 时,都返回一个新的实例;
3,request 每次HTTP 请求都会创建一个新的Bean;
4,session 同一个HTTP Session 共享一个Bean;
5,global session 同一个全局Session 共享一个Bean,一般用于Portlet 应用环境;
6,application 同一个Application 共享一个Bean;
这里重点我说一下1.singleton和2.prototype两个。。。。
T.java:
package com.wishwzp.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.entity.Dog; public class T { private ApplicationContext ac; @Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
} @Test
public void test1() {
Dog dog=(Dog)ac.getBean("dog");
Dog dog2=(Dog)ac.getBean("dog");
System.out.println(dog==dog2);
} }
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.wishwzp.entity.Dog" scope="singleton"><!-- scope属性不写的话默认是singleton -->
<property name="name" value="jack"></property>
</bean> </beans>
Dog.java:
package com.wishwzp.entity;
public class Dog {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
运行结果显示:
true
-----------我们改一下beans.xml文件,将scope改成prototype
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.wishwzp.entity.Dog" scope="prototype"><!-- scope属性不写的话默认是singleton -->
<property name="name" value="jack"></property>
</bean> </beans>
运行结果显示:
false
-------------------------------------------------------------------------------------------------------------------------------
(二)Spring 之IOC 详解的更多相关文章
- 2、Spring的 IoC详解(第一个Spring程序)
Spring是为了解决企业应用开发的复杂性而创建的一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架.在这句话中重点有两个,一个是IoC,另一个是AOP.今天我们讲第一个IoC. IoC概念 ...
- Spring.Net —IOC详解
一. Spring.net中IOC介绍 1. 什么是IOC,控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度.其中 ...
- Spring之IoC详解(非原创)
文章大纲 一.Spring介绍二.Spring的IoC实战三.IoC常见注解总结四.项目源码及参考资料下载五.参考文章 一.Spring介绍 1. 什么是Spring Spring是分层的Java ...
- Spring之IOC详解
学过Spring的小伙伴对于IOC一定不陌生,IOC:控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spr ...
- Spring boot 入门二:Spring Boot配置文件详解
一.自定义属性 当我们创建一个springboot项目的时候,系统默认会为我们在src/main/java/resources目录下创建一个application.properties.同时也支持ym ...
- Spring IoC详解
Spring IoC详解 1. 控制反转 控制反转是一种通过描述(XML或者注解)并通过第三方去产生或获取特定对象的方式.在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入(Depend ...
- Spring Boot 配置文件详解
Spring Boot配置文件详解 Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值.相对于prop ...
- Spring DI使用详解
Spring DI使用详解 一.介绍 DI的定义:依赖注入,为类里面的属性设值.例如,我们之前的setName方法就是在为name属性设值. IOC与DI的关系:IOC进行对象的创建,DI进行值的注入 ...
- Spring jar包详解
Spring jar包详解 org.springframework.aop ——Spring的面向切面编程,提供AOP(面向切面编程)的实现 org.springframework.asm——spri ...
随机推荐
- 【BZOJ1143】祭祀(网络流)
[BZOJ1143]祭祀(网络流) 题面 BZOJ 洛谷 Description 在遥远的东方,有一个神秘的民族,自称Y族.他们世代居住在水面上,奉龙王为神.每逢重大庆典, Y族都 会在水面上举办盛大 ...
- 【BZOJ4540】【HNOI2016】序列(莫队)
[BZOJ4540][HNOI2016]序列(莫队) 题面 BZOJ 洛谷 Description 给定长度为n的序列:a1,a2,-,an,记为a[1:n].类似地,a[l:r](1≤l≤r≤N)是 ...
- Round 403 div. 2
B 可以二分相遇的坐标:也可以二分时间,判断是否存在两个人的区间没有交. An easy way to intersect a number of segments [l1, r1], ..., [l ...
- 使用Hystrix进行微服务降级管理
前言:目前我们的项目是微服务架构,基于dubbo框架,服务之间的调用是通过rpc调用的.刚开始没有任何问题,项目运行健康.良好.可是过了一段时间,线上总有人反应查询订单失败,等过了一段时间才能查到.这 ...
- Python3 字典 get() 方法
Python3 字典 描述 Python 字典 get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...
- 如何将下载的web工程导入到eclipse中使用
如果你是喜欢编程的,在你的开发工具中一定有许多项目,就像小编一样(PS:小编只想默默地装一X): 我们选中其中的一个项目,然后[Ctrl + C]复制,再[Ctrl + V]粘贴到桌面: 那么 ...
- windows下MySQL 5.7+ 解压缩版安装配置方法--转载
方法来自伟大的互联网. 1.去官网下载https://dev.mysql.com/downloads/mysql/.zip格式的MySQL Server的压缩包,根据需要选择x86或x64版.注意:下 ...
- CSS中em,rem的区别
首先这两个单位一般用在移动端 不太清楚得求证 再记录 1.em w3cschool中给出css中尺寸单位如下: 单位 描述 % 百分比 in 英寸 cm 厘米 mm 毫米 em 1em 等于当前的字 ...
- 51nod 1449 砝码称重 (进制思想)
1449 砝码称重 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 现在有好多种砝码,他们的重量是 w0,w1,w ...
- HDU 5700 优先队列(或者multiset) 或 线段树
题目大意:有n个区间,求k个区间,使得这k个区间相交的区间内数字之和最大.数列的数字均>=0 优先队列思路: 按照左端点sort,然后枚举左端点,假设他被覆盖过k次,然后用优先队列来维护最右端即 ...