Spring bean is no different from normal JavaBean. Spring is a container, just to manage (or map) JavaBean through bean. xml.
1. Define spring Bean
1 public class hellobean {
2 Private string helloworld;
3
4 Public String gethelloworld (){
5 return helloworld;
6}
7
8 Public void sethelloworld (string helloworld ){
9 This. helloworld = helloworld;
10}
11
12 public void sayhello (){
13 system. Out. println (helloworld );
14}
15}
2. Define beans. xml
1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <! Doctype beans public "-// spring/DTD bean/EN"
3 "http://www.springframework.org/dtd/spring-beans.dtd">
4
5 <beans>
6 <bean id = "hellobean" class = "com. Spring. IOC. Demo. hellobean">
7 <property name = "helloworld">
8 <value> hello! World! </Value>
9 </property>
10 </bean>
11 </beans>
3. Bean call
Bean calling is the most commonly used new object, while spring maps (reflection, or IOC) bean calling through XML. Spring has multiple ing methods, as shown below:
1 import org. springframework. Beans. Factory. beanfactory;
2 Import org. springframework. Beans. Factory. xml. xmlbeanfactory;
3 Import org. springframework. Context. applicationcontext;
4 Import org. springframework. Context. Support. classpathxmlapplicationcontext;
5 import org. springframework. Context. Support. filesystemxmlapplicationcontext;
6 Import org. springframework. Core. Io. classpathresource;
7 Import org. springframework. Core. Io. filesystemresource;
8 Import org. springframework. Core. Io. resource;
9
10 public class hellobeantest {
11 public static void main (string [] ARGs ){
12 /**
13 * when using common JavaBean
14 **/
15 hellobean = new hellobean ();
16 hellobean. sethelloworld ("Hello world! ");
17 system. Out. println ("new object direct call ");
18 hellobean. sayhello ();
19
20 /**
21 * access the JavaBean Component through spring
22*1. beanfactory Mode
23 **/
24 // 1.1 When classpathresource is used, Beans. XML can read XML files from classpath.
25 Resource resource = new classpathresource ("Beans. xml ");
26 beanfactory factory = new xmlbeanfactory (Resource );
27 hellobean = (hellobean) Factory. getbean ("hellobean ");
28 system. Out. println ("classpathresource + xmlbeanfactory call ");
29 hellobean. sayhello ();
30
31 // 1.2 When filesystemresources is used, Beans. XML can specify the path of the XML definition file to read the definition file.
32 Resource = new filesystemresource ("Beans. xml ");
33 factory = new xmlbeanfactory (Resource );
34 hellobean = (hellobean) Factory. getbean ("hellobean ");
35 system. Out. println ("filesystemresource + xmlbeanfactory call ");
36 hellobean. sayhello ();
37
38 /**
39*2. applicationcontext
40 * using beanfactory is sufficient for simple applications and provides convenience in object management.
41 * to obtain some special and advanced container functions, you can use applicationcontext.
42 * applicationcontext provides some advanced functions, such:
43*2. Provide text message parsing Methods
44*3. Support for international (i18n) messages
45*4. events can be published. Beans interested in events can receive these events.
46 *
47 * spring creators suggest replacing beanfactory with applicationcontext.
48 **/
49 // 2.1 filesystemxmlapplicationcontext can specify the path of the XML definition file to read the definition file
50 applicationcontext context = new filesystemxmlapplicationcontext ("Beans. xml ");
51 hellobean = (hellobean) Context. getbean ("hellobean ");
52 system. Out. println ("filesystemxmlapplicationcontext call ");
53 hellobean. sayhello ();
54 // 2.2 classpathxmlapplicationcontext can read XML files from classpath
55 context = new classpathxmlapplicationcontext ("Beans. xml ");
56 hellobean = (hellobean) Context. getbean ("hellobean ");
57 system. Out. println ("classpathxmlapplicationcontext call ");
58 hellobean. sayhello ();
59 // 2.3 xmlwebapplicationcontext reads the definition file from the file architecture of the Web application at a specified location
60 // omitted
61}
62}