第一节: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. 【BZOJ2151】种树(贪心)

    [BZOJ2151]种树(贪心) 题面 BZOJ 题解 如果没有相邻不能选的限制,那么这就是一道傻逼题. 只需要用一个堆维护一下就好了. 现在加上了相邻点的限制,那么我们就对于当前位置加入一个撤销操作 ...

  2. 洛谷 P2056 [ZJOI2007]捉迷藏 解题报告

    P2056 [ZJOI2007]捉迷藏 题目描述 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩捉迷藏游戏.他们的家很大且构造很奇特,由\ ...

  3. 通过系统自带的MSI安装包来提权账号

    Windows environments provide a group policy setting which allows a regular user to install a Microso ...

  4. 解题:CTSC 2017 吉夫特

    题面 首先有个结论:$C_n^m$为奇数当且仅当$m$是$n$的一个子集 于是从后往前推,记录每个数出现的位置,然后对每个位置枚举子集统计在它后面的贡献即可 #include<cstdio> ...

  5. 【bzoj2588】Count on a tree

    Portal -->bzoj2588 Solution 不行我一定要来挂这道题qwq很气愤qwq(其实还不是因为自己蠢..) 额首先说一下正解 如果这个问题放在序列上面的话..直接离散化一下然后 ...

  6. array_udiff、array_udiff_assoc、array_udiff_uassoc 使用方法

      <?php // array_udiff 用自定义函数比较数组的差值(array_diff 使用内置函数) // 使用该函数我们通过进行更复杂的比较 class Rectangle { pu ...

  7. 三元组ADT (数据结构C语言版) C++实现

    很久没用C语言,都忘了C语言中没有引用参数,下面的代码中用到了C语言没有的引用参数. 首先是一些表示状态的全局变量 common.h #define TRUE 1 #define FALSE 0 #d ...

  8. command not found: django-admin.py

    http://www.cnblogs.com/Xjng/p/3559984.html django-admin.py startproject projectname  其中projectname 为 ...

  9. git grep mysql 操作历史

    history |grep mysql-----git history匹配出mysql操作的命令 !626 到mysql命令安装处链接mysql /usr/local/mysql/bin/mysql ...

  10. No qualifying bean of type [java.lang.String] found for dependency: expected

    出现这个问题的原因是因为多写了一个注解但是没有实体类.如: 或者其他注解下没有内容.