Bean作用域的配置以及 Spring各种注入方式实例 list set map props
1.Bean有两种作用域属性,singleton 和 prototype ,默认为前者。对于singleton,当请求的 Bean 相同时,则不再重新生成新的实例化对象,通常应用程序中的组多组件都只需要一个实例就足够了。而 prototype ,用于每次返回 Bean 的一个新的实例,例如需要获取系统实时时间。
<bean id="mydate" class="com.lihui.MyDate" scope="prototype"></bean>
2.各种注入方式
所有的注入方式在Spring.xml 文件中配置如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 7 8 <!-- 注入基本类型 --> 9 <bean id="person" class="com.lihui.Person"> 10 <property name="name" value="张三"></property> 11 <property name="password" value="hello"></property> 12 <property name="chock" value="true"></property> 13 </bean> 14 15 <!-- 注入list类型和数组类型--> 16 <bean id="beansDemoClass" class="com.lihui.BeansDemoClass"> 17 <property name="fruit"> 18 <list> 19 <value>荔枝</value> 20 <value>桔子</value> 21 <value>樱桃</value> 22 </list> 23 </property> 24 <property name="num"> 25 <list> 26 <value>20</value> 27 <value>45</value> 28 <value>12</value> 29 </list> 30 </property> 31 </bean> 32 33 <!-- 注入引用类型 --> 34 <bean id="mydate" class="com.lihui.MyDate" scope="prototype"> 35 <property name="date" ref="refdate"></property> 36 </bean> 37 <bean id="refdate" class="java.util.Date" scope="prototype"></bean> 38 39 <!-- 注入 set map props 类型 --> 40 <bean id="gatherDemp" class="com.lihui.SetMapProps"> 41 <property name="scoreMap"> 42 <map> 43 <!-- map 必须是 key-value 对应的 --> 44 <entry key="Maths"> 45 <value>98</value> 46 </entry> 47 <entry key="English"> 48 <value>95</value> 49 </entry> 50 </map> 51 </property> 52 <property name="properties"> 53 <!-- 定义 properties 属性 --> 54 <props> 55 <!-- props 必须是 key-value 对应的 --> 56 <prop key="path">C:/MyDocument/MyMusic</prop> 57 <prop key="filename">lihui.txt</prop> 58 </props> 59 </property> 60 <property name="settest"> 61 <set> 62 <!-- set元素,使用value、bean、ref 等指定系列值 --> 63 <value>set值</value> 64 </set> 65 </property> 66 </bean> 67 </beans>
(1)基本注入方式
person.java
1 public class Person { 2 public String name; 3 public String password; 4 public boolean islogin; 5 6 public void setName(String name){ 7 this.name = name; 8 } 9 public void setPassword(String password){ 10 this.password = password; 11 } 12 public void setChock(boolean islogin){ 13 this.islogin = islogin; 14 } 15 }
调用方法:
1 public static void main(String[] args) { 2 XmlBeanFactory bFactory = new XmlBeanFactory(new ClassPathResource( 3 "Spring.xml")); 4 Person person = (Person) bFactory.getBean("person"); 5 System.out.println("姓名:" + person.name + " 密码:" + person.password 6 + " 状态:" + person.islogin); 7 }
(2)注入引用类型
MyDate.java
1 public class MyDate { 2 private Date date; 3 public void setDate(Date date){ 4 this.date = date; 5 } 6 public void getDate(){ 7 System.out.println(date); 8 } 9 }
应用举例:
1 public static void main(String[] args) { 2 XmlBeanFactory bFactory = new XmlBeanFactory(new ClassPathResource("Spring.xml")); 3 MyDate myDate = (MyDate)bFactory.getBean("mydate"); 4 myDate.getDate(); 5 try { 6 Thread.sleep(1000); 7 } catch (InterruptedException e) { 8 // TODO Auto-generated catch block 9 e.printStackTrace(); 10 } 11 ((MyDate)bFactory.getBean("mydate")).getDate(); 12 }
(3)注入list类型和数组类型
BeansDemoClass.java
1 public class BeansDemoClass { 2 public List<String> fruit; 3 public int[] num; 4 public void setFruit(List<String> fruit){ 5 this.fruit = fruit; 6 for(String f : fruit){ 7 System.out.println(f); 8 } 9 } 10 public void setNum(int[] num){ 11 this.num = num; 12 for(int n : num){ 13 System.out.println(n); 14 } 15 } 16 }
应用举例:
1 public static void main(String[] args) { 2 XmlBeanFactory bFactory = new XmlBeanFactory(new ClassPathResource("Spring.xml")); 3 BeansDemoClass bDemoClass = (BeansDemoClass)bFactory.getBean("beansDemoClass"); 4 System.out.println(bDemoClass.fruit + " " + bDemoClass.num); 5 }
(4)注入set、map 及 props 类型
SetMapProps.java
1 public class SetMapProps { 2 public Map scoreMap = new HashMap<String, String>(); 3 public Properties properties = new Properties(); 4 public Set settest = new HashSet<String>(); 5 public void setScoreMap(Map scoreMap){ 6 this.scoreMap = scoreMap; 7 System.out.println(this.scoreMap.get("English")); 8 } 9 public void setProperties(Properties properties){ 10 this.properties = properties; 11 System.out.println(this.properties.get("path")); 12 System.out.println(this.properties.get("filename")); 13 } 14 public void setSettest(Set settest){ 15 this.settest = settest; 16 System.out.println(settest.toString()); 17 } 18 }
应用:
1 public static void main(String[] args) { 2 XmlBeanFactory bFactory = new XmlBeanFactory(new ClassPathResource("Spring.xml")); 3 SetMapProps beans = (SetMapProps) bFactory.getBean("gatherDemp"); 4 5 }
相关推荐
这篇文章将教你快速地上手使用 Spring 框架. 如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦! 首先, 我需要在你心里建立起 Spring...
前几天搞 Spring + Hibernate + Struts 事务配置 ,网上找了好多资料,不过好无语,大多都是 Ctrl + V,浪费俺的宝贵时间 现在我总结配出一套,给大家参考参考,可能有不足,请大家多多交流。 附:内有弹出...
①组件实例的作用域: 是孤立的,简单的来说,组件和组件之间,即使有同名属性,值也不共享。 <add></add> <del></del> [removed] var vm = new Vue({ el: '#app', components: { add: { template: <button>...
组件实例的作用域是孤立的。这意味着不能 (也不应该) 在子组件的模板内直接引用父组件的数据。要让子组件使用父组件的数据,需要通过子组件的 props 选项 使用Prop传递数据包括静态和动态两种形式,下面先介绍...
通过ConfigHandler的解析,我们最终得到一个ConfigInfo实例,Factory就是根据这个实例中所包含的配置信息,利用反射技术对所需对象生成并组装的。SayHelloFactory的代码如下: using System; using System.IO; using...
-- p命名空间注入属性依然要设置set方法 --> <bean id="user" class="cn.sxt.vo.User" p:name="风清扬" p:age="230"/> <!--c命名空间注入要求有对应参数的构造方法 --> <bean id="u1" class=...
PCL+VS2015+pcl.props配置属性文件,注意是64位
pcl1.8.0.VS2015配置文件props
在配置完LobHandler后, 还需要将其注入到sessionFactory的Bean中,下面是调用后的sessionFactory Bean的配置: 代码 6 将lobHandler注入到sessionFactory中的配置 1. 2. … 3. <bean id="sessionFactory" 4. ...
spring+xmemcached aop切面 需要xmemcached-1.2.5+spring-2.5.6 <bean name="factoryMemcachedClient" class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean" destroy-method="shutdown"> $...
ssh 框架整合的实例。 applicationContext.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop=...
前端开源库-map-props映射属性,映射反应属性的高阶组件
弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...
Vue props属性配置学习
application.xml配置 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx=...
本篇文章主要介绍了spring-boot读取props和yml配置文件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要帮助大家简单的理解vue中Props属性,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
opencv3.0的配置文件,opencv300.props
spring3.2+strut2+hibernate4 注解方式。 spring.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...