Spring Learning notes--spring Hello World Common Java Project Edition analysis class loading and assembly

Source: Internet
Author: User
Tags aop gettext stub

The previous presentation of spring's web version of Hello World, a demo of spring in a common Java project, with a discussion of Singleton and prototype, clearly sees spring's injection strategy for entities.

Since it was a demo, it was added to a simple Beanpostprocessorimpl implementation class after looking at the factory's processor and the Bean's front and back processor.

Project structure:

After building the project, guiding the package, and creating a good directory structure, we write Bo. Dosth class, very simple output string.

Note that this is initialized by initializing the string to "haha"

Package Bo;

public class Dosth {
	private String text = null;
	Public Dosth () {
		this.text = "haha";
	}
	Public String GetText () {return
		text;
	}
	public void SetText (String text) {
		this.text = text;
	}
	
}
See below for a layer of view. Outputview,

Package view;

Import Bo. Dosth;

public class Outputview {
	private String viewtext = null;
	Private Dosth dosth = null;
	
	Public Outputview () {
		this.viewtext = "haha";
	}
	Public String OutPut () {return
		this.doSth.getText ();
	}
	Public Dosth Getdosth () {return
		dosth;
	}
	public void Setdosth (Dosth dosth) {
		this.dosth = dosth;
	}
	Public String Getviewtext () {return
		viewtext;
	}
	public void Setviewtext (String viewtext) {
		this.viewtext = viewtext;
	}
	
}
This class has a private field of its own. Viewtext We also initialize it to "haha".

Here's how to write the configuration file:

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "http://www.springframework.org/ Schema/beans "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:aop=" http://www.springframework.org/ Schema/aop "xmlns:p=" http://www.springframework.org/schema/p "xmlns:tx=" http://www.springframework.org/schema/tx "Xmlns:context=" Http://www.springframework.org/schema/context "xsi:schemalocation=" http:// Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http:// WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http:// Www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http:// Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd " > <context:component-scan base-package= "bo"/> 
	<bean id= "Dosth" class= "bo. Dosth "p:text=" hehe "/>
<bean id= "View" class= "view. Outputview "p:dosth-ref=" Dosth "scope=" prototype "/></beans>"
There are many redundant references in the head of the file, too lazy to erase. For a list of the schema for spring, please go to "http://blog.csdn.net/gklifg/article/details/15337099"

Configuration has three: annotation scan bo packet; Assemble Dosth object; Assemble view object;

The value of the Text property is specified using the P namespace at Dosth assembly, which overrides the initialization operation in the constructor method.

Spring's default object scope (why the scope is called). Very inappropriate AH) is singleton. In the view configuration we have set the scope to prototype. So if we use spring more than once to get the view object, we get multiple independent veiw instances. However, Dosth uses the default singleton, so even if there are multiple view objects, the Dosth attribute in those objects also points to the same dosth instance. The following example shows the structure in a good way.

Then start the Java Generic Project invoke the Spring framework key, create the ApplicationContext interface object, here is the filesystemxmlapplicationcontext, the relative path is very convenient to find the corresponding configuration file. For this path, spring provides several path prefixes to accommodate the loading of different types of resources. View list please: http://blog.csdn.net/gklifg/article/details/15336951

For spring's call there is a more low-level call method, that is, to create the Beanfactory object, but I was just beginning to learn spring on its method is not very understanding, In most cases, applicationcontext as the advanced form of beanfactory can satisfy most of the programming requirements.

Package main;


Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.FileSystemXmlApplicationContext;


Import view. Outputview;


public class Hellospring {public
	static void main (String []args) {
		ApplicationContext ctx = new FILESYSTEMXMLAPPL Icationcontext ("/src/applicationcontext.xml");
		Outputview view = (Outputview) ctx.getbean ("View");
		System.out.println ("View.output:" +view.output ());
		System.out.println ("View.viewtext:" +view.getviewtext ());
		View.setviewtext ("hehehe");
		View.getdosth (). SetText ("hehehe");
		Outputview view2 = (outputview) ctx.getbean ("View");
		System.out.println ("View2.output:" +view2.output ());
		System.out.println ("View2.viewtext:" +view2.getviewtext ());
	}
}
To add the URL of an XML configuration to the Filesystemxmlapplicationcontext constructor

To ApplicationContext request Outputview, the bottom of the object assembly have spring automatically completed, can be used to bring,

When you get the view, you first output the information in the Dosth object, and the view itself.

Modify View self information

Modify DOSTH Information

Retrieves a new view object View2, outputs the information in the Dosth object first, and outputs View2 itself.

The results are as follows:

View.outPut:hehe
View.viewText:haha
View2.outPut:hehehe
View2.viewText:haha

Because the assembly configuration of Dosth covers its construction method, Dosth text in Hehe,view is still the initial haha, the dosth in view is changed to Hehehe, View2 also becomes dosth, This shows that the dosth in the two view points to the same instance, while the text value of View2 itself is still the haha of the initialization, indicating that VIEW2 is a new instance independent of the view.

Follow the process of the spring creation object by joining the factory post processor and Bean before and after the processor:

Package Bo;
Import org.springframework.beans.BeansException;
Import Org.springframework.beans.factory.config.BeanFactoryPostProcessor;
Import Org.springframework.beans.factory.config.BeanPostProcessor;
Import Org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

Import org.springframework.stereotype.Component; @Component ("Beanpostprcessorimpl") public class Beanpostprocessorimpl implements Beanpostprocessor, Beanfactorypostprocessor {@Override public object Postprocessafterinitialization (object bean, String beanname) thro
		WS Beansexception {//TODO auto-generated Method stub System.out.println (beanname+ "bean post Methods");
	return bean;  @Override public Object Postprocessbeforeinitialization (object bean, String beanname) throws Beansexception {//
		TODO auto-generated Method Stub System.out.println (beanname+ "bean pre-Methods");
	return bean; @Override public void Postprocessbeanfactory (Configurablelistablebeanfactory factory) throws Beansexception {//TODO auto-generated Method Stub System.out.println ("List of classes loaded after processor------factory:------");
		For (String s:factory.getbeandefinitionnames ()) {System.out.println (s);
	} System.out.println ("----------------------:------"); }


}

The processor only needs to implement two interfaces, plus the component annotation can be identified by spring, pluggable, very convenient.

Run program again result:

List of classes loaded after------factory Processor:------
Beanpostprcessorimpl
Org.springframework.context.annotation.internalConfigurationAnnotationProcessor
Org.springframework.context.annotation.internalAutowiredAnnotationProcessor
Org.springframework.context.annotation.internalRequiredAnnotationProcessor
Org.springframework.context.annotation.internalCommonAnnotationProcessor
View
Dosth
Org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
----------------------:------

2013-11-11 13:22:35 org.springframework.be....//Log information ...

Dosth Bean Front Method
Dosth Bean Post Method
View Bean Front method
View Bean Post method
View.outPut:hehe
View.viewText:haha
View Bean Front method
View Bean Post method
View2.outPut:hehehe
View2.viewText:haha

The

can be clear to see view, dosth to be loaded, in memory to open up space to inject property information, and Dosth first complete the assembly, then the view. and view in two times Getbean assembly two times, but Dosth because it is a single case mode, only one assembly process.

Related Article

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.