Recently, there is no substantive work, just a little time, want to learn other people's code. Also saw a bit of source code, is a bit of reading experience, so determined to see the spring of this large-scale project source code, learn its design ideas.
Hand code is not easy, reproduced please specify: Xingoo
In this blog post you can learn:
1 Spring jar package and source code usage
2 Simple Spring Run example
3 Using the breakpoint debugger, how to quickly read the program "shortcut keys, etc."
The source of the reading is older, is 3.0.5 version, because there is just the version of the source code, and usually is also used in this version of spring, so the analysis is also for this version of the article.
The following contribution to the jar package and source code, because the Baidu Cloud upload compression package always decompression failed, put on the csdn above.
How to use the jar package and source package
First, in the project right-click Property, add the necessary jar package.
Select the necessary jar package, the source jar package given above, and import all the jar packages in the spring3.0.5.
Where Lib is the Spring jar package, with which to import which, do not know, all imported on the line.
Several jar packages outside, for logs and for MySQL drivers. Commons-logging jar Package is a must, other casual bar.
If you are unsure, you can import all the jar packages and Lib inside Lib.
After importing the jar package, open the jar package, select source attachment to edit, and link to the source jar.
Select the appropriate source source package
After importing all, the following
Spring Sample Example
The first is a necessary Pojo class that is used to inject the value of the property.
1 package Com.test.bean; 2 3 public class Person {4 5 private string name, 6 private int age, 7 8 public string GetName () {9 return name;10 }11 public void SetName (String name) { this.name = name;13 }14 Public int Getage () { age;16}17 public void Setage (int.) {This.age = age;19 }20 public void info () { System.out.println ("Name:" +getname () + "Age:" +getage ()); }23}
The main function, which reads the resource file, gets the bean, calls the info method
1 package testspring; 2 3 import org.springframework.context.ApplicationContext; 4 Import Org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 Import Com.test.bean.Person; 7 8 public class Test {9 public static void Main (string[] args) { appli Cationcontext CTX = new Classpathxmlapplicationcontext ("Bean.xml");//Read the contents of the Bean.xml in person p = Ctx.getbean (" Person ", person.class);//Create a Reference object for the Bean P.info (); }14}
Bean.xml the resource file used to configure the bean
1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <beans xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" 3 xmlns= "Http://www.springframework.org/schema/beans" 4 xsi:schemalocation= "/http Www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd " > 6 <bean id= "person" class= "Com.test.bean.Person" > 7 <property name= "name" value= "Xingoo"/> 8 <property name= "age" value= "/> 9 </bean>10 </beans>
Run results
Read the source code
First of all, with the front jar package and source package, you can through this simple program, do not debug, read the source code.
Simple Debugging Shortcuts:
1 F5: Next, you can go to the next function stack
2 F6: The next step of the current function does not go into the other functions.
3 F8: Next breakpoint.
4 You can also press ctrl+shift+i to view the content by selecting a variable or expression. or add a monitor to view it.
5 You can see the Javadoc of a function method by using the shortcut key F2, which means
6 shortcut keys F3 or Ctrl + mouse click to enter a function
7 Ctrl+shift+g See who is using the current method
8 F4 View the inheritance of a class, you can display the parent class inherited by the class and the interface.
With the method of debugging, next, is how to read the source code!
1 references to the book, "Spring Technology Insider"
This book is basically about the way spring is implemented, and the complex relationships between classes. Can help you quickly clarify the relationship between complex classes.
2 drawing a class diagram using STARUML
If, for example, you have managed to clear out a part of the relationship, you will soon forget the relationship, so you can simply draw a complex relationship with this tool.
In this way, the next time you look at it will be clear, this is the classpathxmlapplicationcontext I just painted today's class diagram relationship:
Spring Source code Analysis--How to read the source code