Spring basics-Bean basics, spring-bean
I. Bean definition, initialization, use, and destruction
Ii. ref specifies three modes of dependency
Iii. Five automatic assembly modes of Bean (autowire)
4. Four Bean dependency check modes: used with atuowire, dependency-check = ""
V. Set injection methods
6. Manage beans
Config. xml file
<! -- Bean configuration document -->
<! -- First, it is defined as XML to store Bean configurations -->
<? Xml version = "1.0" encoding = "UTF-8"?>
<! -- Declares that the http://www.springframework.org/dtd/spring-beans.dtd is used -->
<! DOCTYPE beans PUBLIC "-// SPRING // dtd bean // EN"
Http://www.springframenwork.org/dtd/spring-beans.dtd>
<! -- Start Bean configuration. The root node Beans contains one or more Bean elements -->
<Beans>
<Bean id = "" class = "" init-mehod = "" destory-method = "" autowire = "" dependecy-check = "">
<Property>
<Value> </value>
</Property>
</Bean>
</Beans>
I. Bean definition, initialization, use, and destruction
1. Bean Definition
2. Initialization
Init-method
Implement the InitializingBean interface and add the afterPropertiesSet () method;
3. Bean usage
Three methods are available:
1) Beanwrapper
HelloWorld hellworld = new HelloWorld ();
BeanWrapper bw = new BeanWrapperImpl (helloworld );
Bw. setPropertyValue ("msg", "helloworld ");
Bw. getPropertyValue ("msg ");
2) BeanFactory
InputStream is = new FileInputStream ("config. xml ");
XmlBeanFactory factory = new xmlBeanFactory (is );
HelloWorld helloWorld = (HelloWorld) factory. getBean ("HelloWorld ");
HelloWorld. getMsg ();
3) ApplicationContext
ApplicationContext actx = new FileSystemXmlApplicationContext ("config. xml ");
HelloWorld = (HelloWorld) actx. getBean ("HelloWorld ");
HelloWorld. getMsg ();
4. Bean destruction
1) destory-method
2) Implement the org. springframework. beans. factory. DisposableBean interface and add the destory () method.
Ii. ref specifies three modes of dependency
1. local
2. bean
3. parent
<Property>
<Ref = "local"/> | <ref = "bean"/> | <ref = "parent"/>
<Property>
Iii. Five automatic assembly modes of Bean (autowire)
1. byName
2. byType
3. constructor
4. autodetect: If a constructor exists, the constructor is called. If no, byType is used.
5. no: default, not automatically assembled
4. Four Bean dependency check modes: used with atuowire, dependency-check = ""
1. simple checks the basic data types and strings.
2. Check the dependent objects.
3. all (including simple and object)
4. none
V. Set injection methods
1. list-Lis
<Property name = "">
<List>
<Value> </value>
<Value> </value>
<Value> </value>
</List>
</Property>
2. set-Set
<Property name = "">
<Set>
<Value> </value>
<Value> </value>
<Value> </value>
</Set>
</Property>
3. map-Map
<Property name = "">
<Map>
<Entry key = "">
<Value> </value>
</Entry>
</Map>
</Property>
4. props-Properties
<Property name = "">
<Props>
<Prop key = ""> HelloWorld </prop>
</Props>
</Propertiry>
6. Manage beans
1. BeanWrapper
2. BeanFactory
3. ApplicationContext