Spring Study Notes: Spring IoC container, spring source code

Source: Internet
Author: User

As mentioned above, spring uses the configuration file to describe the relationship between bean and bean, and the IOC container will instantiate these beans. The program just used applicationcontext, in fact, it is built on beanfactory, that is, beanfactory is the underlying implementation of Spring IoC containers. it provides the basic functions of IOC containers. applicationcontext provides more functions based on beanfactory, such as internationalization and framework events.

When you hear the IOC container, you may find it strange because you think it is complicated. The IOC container is also a common JavaBean, it is written in common Java code, but it has some special functions. It can store other objects and provides access methods (such as context. getbean ("bean name");) is like list and map. we will use it frequently in the future.IOC containerYou can fully understand this word as beanfactory or applicationcontext.
Beanfactory is just an interface, and its most common implementation class is xmlbeanfactory. Now let's see how to use xmlbeanfactory to rewrite the previous main program.

Package test; </P> <p> Import model. book; </P> <p> Import Org. springframework. beans. factory. beanfactory; <br/> Import Org. springframework. beans. factory. XML. xmlbeanfactory; <br/> Import Org. springframework. core. io. classpathresource; <br/> Import Org. springframework. core. io. resource; </P> <p> public class testxmlbeanfactory {<br/> Public static void main (string [] ARGs) {<br/> resource res = new classpathresource ("bean-test.xml"); <br/> // resource res = new filesystemresource ("C: // project // spring /src // spring-test.xml "); <br/> beanfactory factory = new xmlbeanfactory (RES); <br/> book = (book) factory. getbean ("mybook"); <br/> system. out. println (book. tostring (); <br/>}< br/>

The usage is basically the same as applicationcontext, but you will find that beanfactory is much less than applicationcontext. beanfactory requires a resource to load the configuration file. spring resource indicates a resource unrelated to the source. It has two typical implementation methods: classpathresource loads resources from the class path, and filesystemresource loads resources from a location in the file system.
Now let's take a look at the applicationcontext built on beanfactory. It also has different implementation methods. We used classpathxmlapplicationcontext to instantiate an applicationcontext. Another typical method is filesystemxmlapplicationcontext, it loads the configuration file from a location in the file system. therefore, the previous applicationcontext can be constructed as follows: applicationcontext c = new filesystemxmlapplicationcontext ("C: // somepath // somefile ");
Applicationcontext also has a very useful branch webapplicationcontext, which is specially designed for Web reference. This will be discussed later in spring MVC.
Now let's think: beanfactory, as its name implies, is to create the Bean Factory. How does it create the bean? Let's look at the configuration file of the next Bean:
<Bean id = "mybook" class = "model. book "> <br/> <property name =" ID "value =" 000002 "/> <br/> <property name =" name "value =" thinking in Java "/> <br/> <property name = "author" value = "Bruce Eckel"/> <br/> <property name = "price" value = "2222"/> <br /> </bean>
There are class names, attribute names, and attribute values. You can use reflection to convert this configuration into a Java object. yes, spring does. if you are not familiar with reflection, we recommend that you search for the following and learn about the classes by the way. if you are interested, you can write a beanfactory by yourself. It is not difficult. If you want to see the spring source code, you can do this:
Under Eclipse/myeclipse, press Ctrl and click the class you want to view in the program code. If you haven't attached the source code to the class, the following page is displayed, for example, I want to view webapplicationcontext:


Click attach source..., click exteral file..., select the DIST/Module-sources/spring-web-sources.jar of the decompressed package you downloaded from spring in the file dialog box.

In this way, all the spring web-related source code is added. Don't laugh at me Luo suo. I will add the source code after using eclipse for two years, because no one told me.

You can also use the LOG method to check beanfactory initialization, instantiation, and bean search. You need to configure log4j. download the LIB/log4j package in spring to copy the jar package to the WEB-INF/lib, and create a file log4j In the SRC root directory. properties, the content is as follows:
Log4j. rootlogger = info, RC <br/> log4j. appender. rc = org. apache. log4j. leleappender <br/> log4j. appender. RC. threshold = debug <br/> log4j. appender. RC. layout = org. apache. log4j. patternlayout <br/> log4j. appender. RC. layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS, SSS} %-5 p: % m; % n <br/> log4j. appender. RC. immediateflush = true </P> <p> log4j.logger.org. springframework = debug <br/>
The configuration of log4j is not the scope of this article, but I will explain it a little bit. A root logger is configured in the top six lines, and the message will be output to the console. In this case, log4j.logger.org. springframework = debug means to set Org. set the log output level under the springframework package to debug (lowest level) to output the debug message of spring.
Run the program again. At this time, you will see all beanfactory activities on the console.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.