第一节: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 详解的更多相关文章

  1. 2、Spring的 IoC详解(第一个Spring程序)

    Spring是为了解决企业应用开发的复杂性而创建的一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架.在这句话中重点有两个,一个是IoC,另一个是AOP.今天我们讲第一个IoC. IoC概念 ...

  2. Spring.Net —IOC详解

    一. Spring.net中IOC介绍 1. 什么是IOC,控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度.其中 ...

  3. Spring之IoC详解(非原创)

    文章大纲 一.Spring介绍二.Spring的IoC实战三.IoC常见注解总结四.项目源码及参考资料下载五.参考文章 一.Spring介绍 1. 什么是Spring   Spring是分层的Java ...

  4. Spring之IOC详解

    学过Spring的小伙伴对于IOC一定不陌生,IOC:控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spr ...

  5. Spring boot 入门二:Spring Boot配置文件详解

    一.自定义属性 当我们创建一个springboot项目的时候,系统默认会为我们在src/main/java/resources目录下创建一个application.properties.同时也支持ym ...

  6. Spring IoC详解

    Spring IoC详解 1. 控制反转 控制反转是一种通过描述(XML或者注解)并通过第三方去产生或获取特定对象的方式.在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入(Depend ...

  7. Spring Boot 配置文件详解

    Spring Boot配置文件详解 Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值.相对于prop ...

  8. Spring DI使用详解

    Spring DI使用详解 一.介绍 DI的定义:依赖注入,为类里面的属性设值.例如,我们之前的setName方法就是在为name属性设值. IOC与DI的关系:IOC进行对象的创建,DI进行值的注入 ...

  9. Spring jar包详解

    Spring jar包详解 org.springframework.aop ——Spring的面向切面编程,提供AOP(面向切面编程)的实现 org.springframework.asm——spri ...

随机推荐

  1. 【bzoj1022】小约翰的游戏John

    Portal -->bzoj1022 Solution ​  这题其实是裸的反Nim,这里主要是为了写反Nim游戏的证明 ​  首先给出反Nim(anti-nim)的定义和结论: [定义]桌子上 ...

  2. Linux之时间相关操作20170607

    一.Linux常用时间相关函数 -asctime,ctime,getttimeofday,gmtime,localtime,mktime,settimeofday,time asctime       ...

  3. JS中如何使用EL表达式中的对象

    JS中如何使用EL表达式中的对象 2017年09月25日 15:33:09 lhpnba 阅读数:4859   1.js中使用el表达式要加双引号或单引号:'${list}' 2.js变量获取el表达 ...

  4. UESTC--1727

    原题链接:http://acm.uestc.edu.cn/problem.php?pid=1727 分析:用 l[i] 记录第 i 层楼有多少物品需要往上继续搬运,如果某层楼没有物品,但是更上面还有, ...

  5. 即时通信系统Openfire分析之一:Openfire与XMPP协议

     引言 目前互联网产品使用的即时通信协议有这几种:即时信息和空间协议(IMPP).空间和即时信息协议(PRIM).针对即时通讯和空间平衡扩充的进程开始协议SIP(SIMPLE)以及XMPP.PRIM与 ...

  6. 分块+lazy 或者 线段树+lazy Codeforces Round #254 (Div. 2) E

    E. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. 使用TortoiseGit时如何实现SSH免密码登录

    1.      Git配置 连接GIT服务器使用的是SSH连接,因此无密码登录,需要使用公钥和私钥. 1)     生成公钥/私钥 在Git Shell中输入ssh-keygen命令,直接回车使用默认 ...

  8. 【CodeForces】827 D. Best Edge Weight 最小生成树+倍增LCA+并查集

    [题目]D. Best Edge Weight [题意]给定n个点m条边的带边权无向连通图,对每条边求最大边权,满足其他边权不变的前提下图的任意最小生成树都经过它.n,m<=2*10^5,1&l ...

  9. Broken Necklace

    Description 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个 例子: 1 2 1 2 r b b r b ...

  10. Java源码-HashMap(jdk1.8)

    一.hash方法 如下是jdk1.8中的源码 static final int hash(Object key) { int h; return (key == null) ? 0 : (h = ke ...