spring --wiring binding
Containing your beans
??? Tere i sno single Spring container.Spring actually comes with two distince types of containers:Bean factories and Application
contexts.Beyong there two basic types of contains .Srping come with sereral implementss of BeanFacotory and ApplicationContext.
? 1 introducing the BeanFactory
? There are several implementations of BeanFactory in Spring .But the most userful one is XmlBeanFactory,whick loads
its bean based on the definitions contained in an xml file.
? Creat a XmlBeanFactory? BeanFactory factory=new XMLBeanFactory(new FileInputStream("beans.xml"));
?But at that time ,the BeanFactory does not initialize the bean ,it is loaded lazliy.
? Get the Bean :MyBean myBean=(MyBean)factory.getBean("myBean");
?When getBean() is called ,the factory will instantiate the bean and being setting the bean using dependency injection.
? 2 Working with an application context
?an ApplicationContextis prefered over a BeanFactory in nearly all application ,the only time you might consider using a BeanFactory
are in circumtance where resource s are scarce.
?? Amony the many implements of ApplicationContext are three that are commonly used:
????? ClassPathXmlApplicationContext,FileSystemXmpApplicationContext,XmlWebApplicationContext.
?? ApplicationContext context=new ClassPathXmlApplicationContext("foo.xml");
?wiring the beans
? <beans>
??? <bean id="foo" class="com.springinaction.Foo" />
?</beans>
?Prototyping vs.singleton
?By default ,all Spring beans are singletons.When the container dispenses a bean it will always give the exact same instance of the????
In this case ,you would want to define a prototype bean .Defining a prototype means that instead of defining a single bean.
???? <bean id="foo" class="com.springinaction.Foo"? single/>on="false" />
Initialization and destruction
?? <bean id="foo" class="com.springinaction.Foo" init-method="setup" destory-method="teardown">
Injectiong dependencies via setter method
?? <bean id="foo" class="com.srpinginaction.Foo" >
??????? <property name="name">Foo McFoo</value>
?? </bean>
?Referencing other beans
?? <bean id="foo" class="com.springinaction.Foo">
???? <property name="bar" >
??????? <ref bean="bar" />
???? </property>
??? </bean>
??? <bean id="bar" colass="com.srpinginaction.Bar" />
?Inner beans
?? <bean id="courseService"
???????????? class="com.CourseWericeImpl">
???? <property nanme="studentService">
???????? <bean
????????????? class="com....." />
????? </property>
?? </bean>
? Wiring collections
??? 1Wiring lists and arrays? java.util.List
????? <property name="barList">
?????????? <list>
??????????????? <value>bar1</value>
??????????????? <ref bean="bar2"/>
?????????? </lsit>
????? </property>
?? 2 Wiring set? java.tuil.Set
??? <property name="barSet">
????????? <set>
???????????????? <value>bar1</value>
???????????????? <ref bean="bar2" />
????????? </set>
?? </property>
?? 3 Wiring maps java.util.Map
?? <property name="barMap">
??????? <ebtry key="key1">
??????? <value>bar1</value>
?? </property>
? 4 Wiring propertyies
??? <property name="barProps">
???????? <props>
??????????? <prop key="key1">bar1</prop>
??????????? <prop key="key2">bar2</prop>
???????? </props>
??? </property>
? 5 Setting null values
?? <property name="foo"><null/><property>
?? injecting dependencies via constructor
?? <id="foo" class="com.springinaction.Foo">
??? <constructor-arg>
??????? <value>42<value>( <ref bean="bar">)
???? </constructor-arg>
??? <bean id="foo" class="com.springinaction.Foo">
?????? <constructor-arg>
??????????? <value>http://www.manning.com</value>
?????? </constructor-arg>
? <constructor-arg>
??????????? <value>http://www.manning.com</value>
?????? </constructor-arg>
?? </bean>
posted on 2006-09-04 11:10 康文 閱讀(311) 評(píng)論(0) 編輯 收藏 所屬分類(lèi): java