-
Bean属性继承
查看全部 -
Bean的初始化和销毁
查看全部 -
Bean懒加载
查看全部 -
Bean作用域 Web环境作用域:request session application
查看全部 -
测试集中情况: 第一种:被引用bean1为单例模式,bean也为单例模式
<bean id = "bean1" class="com.example.spring.ioc.class7.Bean1" scope="singleton" /> <bean id = "bean" class="com.example.spring.ioc.class7.Bean" scope="singleton"> <property ref="bean1" name="bean1"/> </bean>
第二种:被引用类bean1为多例模式,bean为单例模式 <bean id = "bean1" class="com.example.spring.ioc.class7.Bean1" scope="prototype" /> <bean id = "bean" class="com.example.spring.ioc.class7.Bean" scope="singleton"> <property ref="bean1" name="bean1"/> </bean>
第三种:被引用类bean1为单例,bean为多例模式 <bean id = "bean1" class="com.example.spring.ioc.class7.Bean1" scope="singleton" /> <bean id = "bean" class="com.example.spring.ioc.class7.Bean" scope="prototype"> <property ref="bean1" name="bean1"/> </bean>
第四种:被引用类bean1为多例模式,bean为多例模式 <bean id = "bean1" class="com.example.spring.ioc.class7.Bean1" scope="prototype" /> <bean id = "bean" class="com.example.spring.ioc.class7.Bean" scope="prototype"> <property ref="bean1" name="bean1"/> </bean>
bean是单例模式singleton,bean1是多例模式prototype,bean依赖bean1.我们希望每次调用Bean的某个方法时, 该方法拿到的Bean1都是一个新的实例,做法如下:
package com.example.spring.ioc.class7; public abstract class Bean { protected abstract Bean1 createBean1(); public void printBean1(){ System.out.println(createBean1()); }}
<?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 = "bean1" class="com.example.spring.ioc.class7.Bean1" scope="prototype" /> <bean id = "bean" class="com.example.spring.ioc.class7.Bean" scope="singleton"> <lookup-method bean="bean1" name="createBean1"/> </bean> </beans>
package com.example.spring.ioc.class7; import org.junit.jupiter.api.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanTest { @Test public void test(){ ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); System.out.println("--------------------------这是第一个Spring Context 上下文----------------------------"); Bean bean = ac.getBean("bean",Bean.class); bean.printBean1(); bean.printBean1(); bean.printBean1(); System.out.println("--------------------------这是第二个Spring Context 上下文----------------------------"); ApplicationContext ac1 = new ClassPathXmlApplicationContext("spring.xml"); Bean bean_1 = ac1.getBean("bean",Bean.class); bean_1.printBean1(); bean_1.printBean1(); bean_1.printBean1(); } }
实体改造前: package com.example.spring.ioc.class7; public class Bean { public void printBean1(){ System.out.println("bean1 = " + bean1); } private Bean1 bean1; public Bean1 getBean1() { return bean1; } public void setBean1(Bean1 bean1) { this.bean1 = bean1; } } <?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 = "bean1" class="com.example.spring.ioc.class7.Bean1" scope="prototype" /> <bean id = "bean" class="com.example.spring.ioc.class7.Bean" scope="singleton"> <property name="bean1" ref="bean1"/> </bean> </beans> package com.example.spring.ioc.class7; import org.junit.jupiter.api.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanTest { @Test public void test(){ ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); System.out.println("--------------------------这是第一个Spring Context 上下文----------------------------"); Bean bean = ac.getBean("bean",Bean.class); bean.printBean1(); bean.printBean1(); bean.printBean1(); System.out.println("--------------------------这是第二个Spring Context 上下文----------------------------"); ApplicationContext ac1 = new ClassPathXmlApplicationContext("spring.xml"); Bean bean_1 = ac1.getBean("bean",Bean.class); bean_1.printBean1(); bean_1.printBean1(); bean_1.printBean1(); } }
查看全部 -
Singleton作用于如下如:bean作为属性会被注入到anotherBean1 anotherBean2 anotherBean3中,这里注意,bean作为单例模式的话,只会有一个bean实例,所以注入到以上实体中的bean都是同一bean
在一个Spring上下文环境下,单例模式会生成一个实例,如果在多个上下文环境下则会一个Spring上下文环境单例模式会生成一个实例,多个Spring上下文每个Spring上下文都会生成一个单例模式!!!
package com.example.spring.ioc.class7; public class Bean { private Bean1 bean1; public Bean1 getBean1() { return bean1; } public void setBean1(Bean1 bean1) { this.bean1 = bean1; } @Override public String toString() { return "Bean{" + "bean1=" + bean1 + '}'; } }
package com.example.spring.ioc.class7; public class Bean1 { }
<?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 = "bean1" class="com.example.spring.ioc.class7.Bean1" scope="singleton" /> <bean id = "bean" class="com.example.spring.ioc.class7.Bean"> <property name="bean1" ref="bean1"/> </bean> </beans>
package com.example.spring.ioc.class7; import org.junit.jupiter.api.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanTest { @Test public void test(){ ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); Bean bean = ac.getBean("bean",Bean.class); Bean1 bean1 = ac.getBean("bean1",Bean1.class); System.out.println("Bean = " + bean); System.out.println("Bean1 = " + bean1); System.out.println(bean.toString()); System.out.println("--------------------------这是第二个Spring Context 上下文----------------------------"); ApplicationContext ac1 = new ClassPathXmlApplicationContext("spring.xml"); Bean bean_1 = ac1.getBean("bean",Bean.class); Bean1 bean1_1 = ac1.getBean("bean1",Bean1.class); System.out.println("bean = " + bean_1); System.out.println("bean1 = " + bean1_1); System.out.println(bean.toString()); } }
查看全部 -
这是注入方式展示:PojoBean{ name='gavin', bean=com.example.spring.ioc.Bean@4c39bec8, num=0, bean1=com.example.spring.ioc.Bean@4c39bec8, listStr=[str1, str2], listBean=[com.example.spring.ioc.Bean@4c39bec8], setStr=[setStr1, setStr2], setBean=[com.example.spring.ioc.Bean@4c39bec8], map={key1=com.example.spring.ioc.Bean@4c39bec8}, mapBean={com.example.spring.ioc.Bean@4c39bec8=com.example.spring.ioc.Bean@4c39bec8}, pro={proKey=proValue} }
package com.example.spring.ioc; import org.junit.jupiter.api.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanTests { @Test public void Test(){ //通过构造方法实例化bean ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); System.out.println("这是注入方式展示:" + ac.getBean("pojoBean").toString()); } }
package com.example.spring.ioc; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class PojoBean { private String name; private Bean bean; private Integer num; private Bean bean1; private List<String> listStr; private List<Bean> listBean; private Set<String> setStr; private Set<Bean> setBean; private Map<String,Bean> map; private Map<Bean,Bean> mapBean; private Properties pro; public PojoBean(String name, Bean bean) { this.name = name; this.bean = bean; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Bean getBean() { return bean; } public void setBean(Bean bean) { this.bean = bean; } public Integer getNum() { return num; } public void setNum(Integer num) { this.num = num; } public Bean getBean1() { return bean1; } public void setBean1(Bean bean1) { this.bean1 = bean1; } public List<String> getListStr() { return listStr; } public void setListStr(List<String> listStr) { this.listStr = listStr; } public List<Bean> getListBean() { return listBean; } public void setListBean(List<Bean> listBean) { this.listBean = listBean; } public Set<String> getSetStr() { return setStr; } public void setSetStr(Set<String> setStr) { this.setStr = setStr; } public Set<Bean> getSetBean() { return setBean; } public void setSetBean(Set<Bean> setBean) { this.setBean = setBean; } public Map<String, Bean> getMap() { return map; } public void setMap(Map<String, Bean> map) { this.map = map; } public Map<Bean, Bean> getMapBean() { return mapBean; } public void setMapBean(Map<Bean, Bean> mapBean) { this.mapBean = mapBean; } public Properties getPro() { return pro; } public void setPro(Properties pro) { this.pro = pro; } @Override public String toString() { return "PojoBean{" + "name='" + name + '\'' + ", bean=" + bean + ", num=" + num + ", bean1=" + bean1 + ", listStr=" + listStr + ", listBean=" + listBean + ", setStr=" + setStr + ", setBean=" + setBean + ", map=" + map + ", mapBean=" + mapBean + ", pro=" + pro + '}'; } }
<?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 = "bean" class = "com.example.spring.ioc.Bean" name = "bean_1,bean_2"/> <!--别名--> <alias name="bean" alias="bean1" /> <!--通过静态方法实例化bean--> <bean id = "bean2" class = "com.example.spring.ioc.Bean2Factory" factory-method="getBean" /> <!--通过实例方法实例化bean--> <bean id = "bean3Factory" class="com.example.spring.ioc.Bean3Factory" /> <bean id = "bean3" class="com.example.spring.ioc.Bean3" factory-bean="bean3Factory" factory-method="getBean" /> <!--注入bean的方式--> <bean id = "pojoBean" class="com.example.spring.ioc.PojoBean"> <constructor-arg index="0" value="gavin" type="java.lang.String" name="name"/> <constructor-arg index="1" ref="bean" type="com.example.spring.ioc.Bean" name="bean"/> <property name="bean1" ref="bean" /> <property name="num" value="0" /> <property name="listBean" > <list> <ref bean="bean"/> </list> </property> <property name="listStr"> <list> <value>str1</value> <value>str2</value> </list> </property> <property name="setBean"> <set> <ref bean="bean"/> </set> </property> <property name="setStr"> <set> <value>setStr1</value> <value>setStr2</value> </set> </property> <property name="map"> <map> <entry key="key1" value-ref="bean" /> </map> </property> <property name="mapBean"> <map> <entry key-ref="bean" value-ref="bean"/> </map> </property> <property name="pro"> <props> <prop key="proKey" >proValue</prop> </props> </property> </bean> </beans>
查看全部 -
Spring实例化Bean
通过构造方法实例化bean
通过静态方法实例化bean
通过实例方法实例化bean
Bean的别名
package com.example.spring.ioc; public class Bean { public Bean() { System.out.println("我是Bean的构造器!"); } }
package com.example.spring.ioc; public class Bean2 { public Bean2() { System.out.println("我是Bean2的构造器!"); } }
package com.example.spring.ioc; public class Bean2Factory { public static Bean2 getBean(){ return new Bean2(); } }
package com.example.spring.ioc; public class Bean3 { public Bean3() { System.out.println("我是Bean3的构造器!"); } }
package com.example.spring.ioc; public class Bean3Factory { public Bean3 getBean(){ return new Bean3(); } }
<?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 = "bean" class = "com.example.spring.ioc.Bean" name = "bean_1,bean_2"/> <!--别名--> <alias name="bean" alias="bean1" /> <!--通过静态方法实例化bean--> <bean id = "bean2" class = "com.example.spring.ioc.Bean2Factory" factory-method="getBean" /> <!--通过实例方法实例化bean--> <bean id = "bean3Factory" class="com.example.spring.ioc.Bean3Factory" /> <bean id = "bean3" class="com.example.spring.ioc.Bean3" factory-bean="bean3Factory" factory-method="getBean" /> </beans>
package com.example.spring.ioc; import org.junit.jupiter.api.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanTests { @Test public void Test(){ //通过构造方法实例化bean ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); Bean bean = ac.getBean("bean",Bean.class); System.out.println("这是bean : " + bean); System.out.println("通过构造器实例化bean完成!"); //通过静态方法实例化bean Bean2 bean2 = ac.getBean("bean2",Bean2.class); System.out.println("这是Bean2 : " + bean2); System.out.println("通过静态方法实例化bean2完成!"); //通过实例化方法实例化bean Bean3 bean3 = ac.getBean("bean3",Bean3.class); System.out.println("这是bean3 : " + bean3); System.out.println("通过实例方法实例化bean完成!"); //bean别名 Bean bean1 = ac.getBean("bean1",Bean.class); System.out.println("这是别名bean1 : " + bean1); Bean bean_1 = ac.getBean("bean_1",Bean.class); System.out.println("这是别名bean_1 : " + bean_1); Bean bean_2 = ac.getBean("bean_2",Bean.class); System.out.println("这是别名bean_2 : " + bean_2); // System.out.println("这是注入方式展示:" + ac.getBean("pojoBean").toString()); } }
查看全部 -
IOC(Inversion of Control)控制反转、依赖注入
控制什么?
控制对象的创建及销毁(生命周期)
反转什么?
将对象的控制权交给IOC容器
查看全部 -
Spring IOC介绍
查看全部 -
ioc为控制反转,依赖注入。 控制什么 控制对象,对象的创建和销毁。依赖注入什么 通过ioc控制对象。查看全部
-
假设不把控制权交给ioc,那换一辆车就得重写方法
查看全部 -
如果某个Bean在程序整个运行周期都可能不会被使用,那么可以考虑懒加载
优点:尽可能的节省了资源
缺点:可能会导致某个操作响应时间增加
查看全部 -
Bean1和Bean2的作用域
查看全部 -
注解注入bean
查看全部
举报
0/150
提交
取消