- 浏览: 1573654 次
- 来自: 上海
文章分类
- 全部博客 (374)
- Java (101)
- Struts (54)
- Oracle (30)
- JavaScript (16)
- Spring (27)
- Hibernate (16)
- MyEclipse (3)
- JSF (1)
- FreeMarker (2)
- SiteMesh (2)
- JfreeChart (2)
- Ibatis (2)
- JSP (12)
- MyBatis (4)
- SWFupload (1)
- EJB (4)
- Jboss (4)
- WebService (2)
- Linux (16)
- Android (2)
- XML (6)
- Java 网络编程 (13)
- AXIS2 (1)
- FTP (1)
- Jswing (1)
- Socket (3)
- 杂文选集 (6)
- solr (2)
- PS (1)
- Tomcat (7)
- JDBC (9)
- Highcharts (1)
- maven (1)
- Nodejs (0)
- navicat (2)
- Exception (5)
- eclipse (3)
- jQuery (1)
- springMVC (4)
- MySQL (11)
- SVN (1)
- Sql Server (1)
- zookeeper (1)
- JVM (1)
- Groovy (2)
- Git (1)
- Nginx (1)
- DynamicReport (1)
- IDEA (2)
- JasperReports (1)
- Postgresql (2)
- Mac (1)
- gradle (1)
- 数据结构算法 (1)
最新评论
-
hpu145:
引用引用
java 千分位的添加和去除 -
被遗忘的下路:
少了个junit-4.8.2的包
SSH2整合完整案例(四十三) -
白天看黑夜:
java过滤emoji字符处理,希望能帮到你http://ww ...
emoji 表情图片解决方法 -
caipeiming:
这个挺好JavaScript实现input输入框控件只允许输入 ...
js 控制文本框只能输入中文、英文、数字等 -
双子树:
东西太好啦受教啊
Struts2 JSP中将list,set ,Map传递到Action然后<s:iterator>遍历(三十五)
协调作用域不同步的bean
问题是;当一个singleton的bean依赖一个prototype的bean的时候,会产生不同步的现象
解决问题的方法
1:放弃部分依赖,当singleton的bean每次需要prototype的bean 的时候,主动向容器中访问新的bean. 这样会造成 与springAPI 严重耦合
2.利用方法注入 (我们用这种)
配置lookup-method 让spring帮我们实现
深入理解依赖配置关系
前面都是bean之间的依赖,下面讲的是,bean依赖bean之间的属性,方法返回值,field值
注入其他bean的属性值
将其他bean的属性值定义成一个bean
注入其他bean的Field值
注入其他bean的方法返回值
问题是;当一个singleton的bean依赖一个prototype的bean的时候,会产生不同步的现象
解决问题的方法
1:放弃部分依赖,当singleton的bean每次需要prototype的bean 的时候,主动向容器中访问新的bean. 这样会造成 与springAPI 严重耦合
2.利用方法注入 (我们用这种)
//singleton bean package cn.sh.springmvc.model; import cn.sh.springmvc.model.interfaces.Axe; import cn.sh.springmvc.model.interfaces.Person; /** * 不同作用于的 依赖与协同 * 采用方法注入新的Bean ,解决:singleton Bean中使用个prototypeBean 的问题 * @author Bin * */ public abstract class Japanese implements Person { //定义一个方法 让spring 跟我们实现,这样就能保证每次都是新的对象 public abstract Axe getAxe(); @Override public void useAxe() { System.out.println("正在使用"+getAxe()+"劈柴"); System.out.println(getAxe().chop()); } } //prototype bean package cn.sh.springmvc.model; import cn.sh.springmvc.model.interfaces.Axe; public class StoneAxe implements Axe { private String name; public StoneAxe() { System.out.println("石头初始化StoneAxe"); } @Override public String chop() { // TODO Auto-generated method stub return name+"石斧看柴慢"; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
配置lookup-method 让spring帮我们实现
<!-- 不同作用于的协同工作 --> <bean id="stoneAxe1" class="cn.sh.springmvc.model.StoneAxe" scope="prototype"> <property name="name" value="磨过"/> </bean> <bean id="japanese" class="cn.sh.springmvc.model.Japanese" scope="singleton"> <lookup-method name="getAxe" bean="stoneAxe1"/> </bean>
//测试 不同作用域的Bean相互依赖后出现的工作不协调问题 @Test public void test17() { AbstractApplicationContext act=new ClassPathXmlApplicationContext("classpath*:applicationContent.xml"); Japanese p=act.getBean("japanese",Japanese.class); p.useAxe(); p.useAxe(); System.out.println(p.getAxe()==p.getAxe()); }
深入理解依赖配置关系
前面都是bean之间的依赖,下面讲的是,bean依赖bean之间的属性,方法返回值,field值
在spring配置文件中使用xml元素进行配置,实际上是让spring执行相应的java代码 例如: 1.使用<bean>元素,就是让spring执行无参数构造函数 2.使用<property> 就是让spring执行setter方法 但是java程序还有可能还有其他语句,调用getting,调用普通方法,访问类或者对象的file,spring也为这种语句提供利配置的语法 3.调用getter方法:使用 PropertyPathFactoryBean 4.访问类或对象的Field值,使用FieldRetrievingFactoryBean 5.调用普通方法:使用MethodInvokingFactoryBean 由此可见,spring可以然我们不写java代码就可以实现java编程,只要使用合适XML 语法进行配置,spring就可通过反射执行任意的底层java代码.
注入其他bean的属性值
<!-- 注入其他Bean的属性 --> <bean id="chinese1" class="cn.sh.springmvc.model.Chinese"> <property name="axe" ref="stoneAxe"/> </bean> <!-- Bean 之间的属性依赖 --> <bean id="chinese2" class="cn.sh.springmvc.model.Chinese"> <property name="axe"> <!--注意这里,并不是bean的ID,而是属性的表达式 --> <bean id="chinese1.axe" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/> </property> </bean>
将其他bean的属性值定义成一个bean
<!-- 指定一个Bean实例的属性定义为一个 Bean --> <bean id="pro_stoneAxe" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"> <property name="targetBeanName" value="chinese1"/> <property name="propertyPath" value="axe"/> </bean> <bean id="stone_name" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"> <property name="targetBeanName" value="chinese1"/> <property name="propertyPath" value="axe.name"/> </bean> <bean id="chinese_list" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"> <property name="targetBeanName" value="chinese"/> <property name="propertyPath" value="schools"/> </bean> <bean id="chinese_list_ele" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"> <property name="targetBeanName" value="chinese"/> <property name="propertyPath" value="schools[0]"/> </bean> <bean id="chinese_map_ele" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"> <property name="targetBeanName" value="chinese"/> <property name="propertyPath" value="entrySet[0]"/> </bean> <!-- 注意有些集合不是 有序的,如果集合没有采用泛型定义,注意返回的类型 --> <bean id="chinese_set_ele_str" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"> <property name="targetBeanName" value="chinese"/> <property name="propertyPath" value="axes[0]"/> </bean> <bean id="chinese_set_ele_obj" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"> <property name="targetBeanName" value="chinese"/> <property name="propertyPath" value="axes[1]"/> </bean> <!-- 采用嵌套bean --> <bean id="new_stone_name" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"> <property name="targetObject"> <bean class="cn.sh.springmvc.model.SteelAxe"> <property name="name" value="打磨过的"/> </bean> </property> <property name="propertyPath" value="name"/> </bean>
//测试 Bean实例属性之间的依赖,以及将一个实例Bean的属性定义成一个Bean //注意 org.springframework.beans.factory.config.PropertyPathFactoryBean //如果将对象的属性定义成Bean 的时候,要指定targetBeanName 和 propertyPath 两个属性 @Test public void test18() { AbstractApplicationContext act=new ClassPathXmlApplicationContext("classpath*:applicationContent.xml"); //属性之间的传递 Chinese c2=act.getBean("chinese2",Chinese.class); System.out.println(c2.getAxe().chop()); //属性的对象 Axe a=act.getBean("pro_stoneAxe",Axe.class); System.out.println(a.chop()); //属性对象的属性 String name=act.getBean("stone_name",String.class); System.out.println(name); //对象的 list List<String> schools=act.getBean("chinese_list",List.class); System.out.println(schools.get(0)); //对象的 list 中的元素 String schools_Name=act.getBean("chinese_list_ele",String.class); System.out.println(schools_Name); //对象的 map 中的元素 /*Set<Map.Entry<String,Object>> set=act.getBean("chinese_map_ele",Set.class); for(Iterator<Map.Entry<String,Object>> it=set.iterator();it.hasNext();){ Map.Entry<String,Object> entry=it.next(); System.out.println(entry.getKey()); System.out.println(entry.getValue()); }*/ Map.Entry<String,Object> entry=act.getBean("chinese_map_ele",Map.Entry.class); System.out.println(entry.getKey()); System.out.println(entry.getValue()); String set1=act.getBean("chinese_set_ele_str",String.class); System.out.println(set1); SteelAxe obj=act.getBean("chinese_set_ele_obj",SteelAxe.class); System.out.println(obj.chop()); //使用内嵌Bean的属性 String stone_Name=act.getBean("new_stone_name",String.class); System.out.println(stone_Name); }
注入其他bean的Field值
<!-- 注入其他Bean的Field的值 --> <!-- 将 java.sql.Connection.TRANSACTION_SERIALIZABLE属性值给 user的age属性--> <bean id="user1" class="cn.sh.springmvc.model.User"> <property name="age"> <bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/> </property> </bean> <!--将 java.sql.Connection.TRANSACTION_SERIALIZABLE 定义成一个bean --> <bean id="theAge" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> <property name="targetClass" value="java.sql.Connection"/> <property name="targetField" value="TRANSACTION_SERIALIZABLE"/> </bean> <!-- 如果 字段为静态 的,还可以简写 --> <bean id="theAge1" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> <property name="staticField" value="java.sql.Connection.TRANSACTION_SERIALIZABLE"/> </bean>
//测试 注入其他Bean的Field字段值 //FieldRetrievingFactoryBean targetClass targetField staticField @Test public void test19() { AbstractApplicationContext act=new ClassPathXmlApplicationContext("classpath*:applicationContent.xml"); User u= act.getBean("user1",User.class); System.out.println(u.getAge()); Integer level=act.getBean("theAge",Integer.class); System.out.println(level); Integer level1=act.getBean("theAge1",Integer.class); System.out.println(level1); }
注入其他bean的方法返回值
<!-- 注入 其他Bean的方法返回值 --> <bean id="valueGenerate" class="cn.sh.springmvc.model.ValueGenerator"/> <bean id="user2" class="cn.sh.springmvc.model.User"> <property name="age"> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="valueGenerate"/> <property name="targetMethod" value="getValue"/> </bean> </property> </bean> <!-- 如果是静态方法 --> <bean id="user3" class="cn.sh.springmvc.model.User"> <property name="age"> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <!-- 如果是静态方法.可以不用 targetObject --> <property name="targetClass" value="cn.sh.springmvc.model.ValueGenerator"/> <property name="targetMethod" value="getStaticVal"/> </bean> </property> </bean> <!-- 如果方法方法存在重载 指定参数--> <bean id="sysProps" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetClass" value="java.lang.System"/> <property name="targetMethod" value="getProperties"/> </bean> <bean id="java_version" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetClass" value="java.lang.System"/> <property name="targetMethod" value="getProperty"/> <property name="arguments"> <list> <value>java.version</value> </list> </property> </bean> <bean id="mth_stoneAxe_sft" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetClass" value="cn.sh.springmvc_java.factory.AxeFactory"/> <property name="targetMethod" value="getAxe"/> <property name="arguments"> <list><value>stone</value></list> </property> </bean> <!-- 如果是静态的method 可以省略 targetClass --> <bean id="mth_steelAxe_sft" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="staticMethod" value="cn.sh.springmvc_java.factory.AxeFactory.getAxe"/> <property name="arguments"> <list><value>steel</value></list> </property> </bean> <!-- 使用嵌套bean --> <bean id="mth_people_sft" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject"> <bean class="cn.sh.springmvc_java.factory.PeopleFactory"/> </property> <property name="targetMethod" value="getPeople"/> <property name="arguments"> <list><value>chin</value></list> </property> </bean>
//测试 注入其他Bean的方法返回值 //MethodInvokingFactoryBean targetClass targetMethod @Test public void test20() { AbstractApplicationContext act=new ClassPathXmlApplicationContext("classpath*:applicationContent.xml"); User u= act.getBean("user2",User.class); System.out.println(u.getAge()); User u1=act.getBean("user3",User.class); System.out.println(u1.getAge()); Properties syspro=act.getBean("sysProps",Properties.class); System.out.println(syspro.getProperty("java.version")); System.out.println(act.getBean("java_version")); System.out.println(System.getProperty("java.version")); Axe saxe=act.getBean("mth_stoneAxe_sft",Axe.class); System.out.println(saxe.chop()); Axe saxe1=act.getBean("mth_steelAxe_sft",Axe.class); System.out.println(saxe1.chop()); People p=act.getBean("mth_people_sft",People.class); System.out.println(p.sayHello("admin")); }
发表评论
-
spring-session 中的坑
2017-07-06 15:34 9944spring-session 配置 依赖 gradle ... -
Spring AspectJ Aop Annotation
2017-03-29 17:08 670import org.aspectj.lang.Proce ... -
spring 第13天 使用@scheduled注解执行定时任务
2015-01-06 23:11 54021我们使用spring的注解 @Scheduled 执行定时任务 ... -
Spring 第12天,事务传播属性和 隔离级别
2014-09-28 00:36 7751 事务的传播属性(Propagation) 1) REQ ... -
spring 第11天 quartz任务调度
2014-08-24 13:59 1101Quartz是一个强大的企业级任务调度框架,Spring中继承 ... -
spring 第10 天 AOP 面向切面
2014-08-21 00:08 1718AOP(Aspect Orient Programming ... -
spring 第9天 Resurce 资源访问
2014-08-17 22:20 1825Spring Resource接口 spring提供的Reso ... -
spring 第8天 Spring 注解
2014-08-17 15:33 1435spring注解 @Component:标注一个普通的sp ... -
spring 第7天 Bean,BeanFactory处理器,配置器
2014-08-16 21:46 1187spring 两种后处理器 第一种,Bean 后处理器 对容器 ... -
spring 第6天SpEL,P命名空间,Util Schema
2014-08-13 22:52 1267使用p名称空间配置属性 ... -
spring 第4天bean继承,init,destory-method
2014-08-10 17:54 1601深入理解Spring容器中的b ... -
spring 第3天使用java类和XML配置bean
2014-08-09 16:51 1463下面采用java类来配置bean,前面都采用xml进行配置be ... -
spring 第2天,bean作用域,自动注入集合
2014-08-06 22:16 1886sping容器中的Bean <!---beans的全 ... -
spring 第1天 IOC,DI,国际化,容器事件
2014-08-04 21:27 13581.构造注入(就是使用 构 ... -
SpringMVC 注解 和非注解
2014-01-26 10:29 17977首先看看非注解的项目结构 在web.xml文件 配置spr ... -
详解spring 每个jar的作用
2013-11-19 23:54 3871spring.jar 是包含有完整 ... -
Spring配置Hibernate事务
2013-11-10 13:45 1182为了保证数据的一致性,在编程的时候往往需要引入事务这个概念。事 ... -
Spring 中引用Properties文件
2013-08-29 14:39 10611其中部分配置信息(邮件发送相关): #邮件发送的相关配置 ... -
Spring IOC控制反转 依赖注入DI
2012-12-15 09:37 2296目录 1.使用IOC控制反转 中的DI依赖注入 手工注入 ... -
Spring IOC控制反转 依赖注入DI
2012-12-14 16:23 8目录 1.使用IOC控制反转 中的DI依赖注入 (两种配置方式 ...
相关推荐
Spring 的bean的作用域总结,详细的总结了 Spring 的bean的作用域
Spring Bean 的作用域之间有什么区别:Bean的作用域: 可以通过scope 属性来指定bean的作用域 ①singleton: 默认值。当IOC容器
讲解了spring的6种作用域:singleton(单例)、non-singleton(也称 prototype),Spring2.0以后,增加了session、request、global session三种专用于Web应用程序上下文的Bean
spring bean 的作用域(scope), SPringle bean的作用域
主要介绍了Spring实战之Bean的作用域singleton和prototype用法,结合实例形式分析了Bean的作用域singleton和prototype相关使用方法及操作注意事项,需要的朋友可以参考下
详解Spring中bean的作用域,一级对action的管理方式,方便对spring有一个深入的了解
NULL 博文链接:https://huangminwen.iteye.com/blog/1486717
主要介绍了Spring实战之Bean的作用域request用法,结合实例形式分析了spring中Bean的request作用域相关使用技巧与操作注意事项,需要的朋友可以参考下
Bean元素有一个scope属性,用于定义Bean的作用域,该属性有如下五个值: 1>singleton: 单例模式,在整个spring IOC容器中,单例模式作用域的Bean都将只生成一个实例。一般Spring容器默认Bean的作用域为singleton ...
bean加入spring容器管理的方式,bean加入applicationcontext容器的方式
通过@Bean 和spring的factoryBean注入的bean. 以及对应@service注解注入的类 通过@Autowired 注入对象的时间是现根据类型在根据beanName获取的案例集合
如果bean的作用域的属性被声明为 singleton, 那么Spring Ioc容器只会创建一个共享的bean实例。对于所有的bean请求,只要id与该bean定义的相匹配,那么Spring在每次需要时都返回同一个bean实例。 Singleton是单例...
Spring容器中Bean的作用域编程开发技术共3页.pdf.zip
主要给大家介绍了关于spring boot中几种注入方法的一些个人看法,文中通过示例代码介绍的非常详细,对大家学习或者使用spring boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
spring动态向容器中添加bean和删除指定bean,不需要重启应用
* * * * Bean的作用域 作用域的种类 Spring 4.3中为Bean的实例定义了7种作用域,如下表所示: 注意:在上表7种作用域中,singleton和prototype是最常用的两种作用域。 在Spring配置文件中,可以使用<bean>元素的...
使用了ApplicationContextAware接口,获取spring管理的bean; 多项目整合夸spring容器获取bean的实现方式。
主要给大家介绍了Spring中Bean的生命周期和作用域及实现方式的相关资料,文中介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
本篇文章主要介绍了Spring 中如何控制2个bean中的初始化顺序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
01.Spring Bean的作用域代码