function extension of container (ii) Function extension

Source: Internet
Author: User

Before entering the function preparebeanfactory, Spring has completed the parsing of the configuration, and the extension of the ApplicationContext function expands accordingly.

	protected void Preparebeanfactory (Configurablelistablebeanfactory beanfactory) {//Tell ' the internal bean factory to U
		Se the context ' s class loader etc.
		Set Beanfactory ClassLoader as the current context of the ClassLoader Beanfactory.setbeanclassloader (getClassLoader ());
		Set Beanfactory expression Language processor, Spring3 add support for expression language,//default to invoke related property values in the form of #{bean.xxx}
		Beanfactory.setbeanexpressionresolver (New Standardbeanexpressionresolver ()); Added a default propertyeditor for Beanfactory, which is a tool for setting up management of bean properties such as Beanfactory.addpropertyeditorregistrar (new

		Resourceeditorregistrar (this, getenvironment ()));
		Configure The Bean Factory with context callbacks.
		/* * Add beanpostprocessor, */beanfactory.addbeanpostprocessor (new Applicationcontextawareprocessor (this));
		Several interface Beanfactory.ignoredependencyinterface (Resourceloaderaware.class) ignoring automatic assembly are set up;
		Beanfactory.ignoredependencyinterface (Applicationeventpublisheraware.class);
		Beanfactory.ignoredependencyinterface (Messagesourceaware.class); Beanfactory.iGnoredependencyinterface (Applicationcontextaware.class);

		Beanfactory.ignoredependencyinterface (Environmentaware.class);
		Beanfactory interface not registered as resolvable type in a plain factory.
		Messagesource registered (and found for autowiring) as a bean.
		Several special rules for automatic assembly are set Beanfactory.registerresolvabledependency (Beanfactory.class, beanfactory);
		Beanfactory.registerresolvabledependency (Resourceloader.class, this);
		Beanfactory.registerresolvabledependency (Applicationeventpublisher.class, this);

		Beanfactory.registerresolvabledependency (Applicationcontext.class, this);
		Detect a loadtimeweaver and prepare for weaving, if found. Increased support for AspectJ if (Beanfactory.containsbean (Load_time_weaver_bean_name)) {Beanfactory.addbeanpostprocessor (new
			Loadtimeweaverawareprocessor (beanfactory));
			Set a temporary ClassLoader for type matching.
		Beanfactory.settempclassloader (New Contexttypematchclassloader (Beanfactory.getbeanclassloader ())); }//RegisteR default Environment beans. Add the default system environment BEAN if (!beanfactory.containslocalbean (Environment_bean_name)) {Beanfactory.registersingleton (
		Environment_bean_name, Getenvironment ()); } if (!beanfactory.containslocalbean (System_properties_bean_name)) {Beanfactory.registersingleton (SYSTEM_
		Properties_bean_name, Getenvironment (). Getsystemproperties ()); } if (!beanfactory.containslocalbean (System_environment_bean_name)) {Beanfactory.registersingleton (SYSTEM_
		Environment_bean_name, Getenvironment (). Getsystemenvironment ()); }
	}

These steps look a little blurry, and then we'll analyze each step in detail

1. Add Spel language Support

Spring expression language is all called "Spring expression Language", abbreviated as "Spel", similar to the OGNL language used in struts 2x, Spel is a separate module that relies only on the core module and does not depend on other modules and can be used alone.

Spel use #{...} As delimiters, all characters in the large frame number will be considered spel, using the following format:

	<util:properties id= "db" location= "classpath:db.properties" >
	</util:properties>
	<bean id= " DBCP "class=" Org.apache.commons.dbcp.BasicDataSource ">
	  <property name=" username "value=" #{db.user} "> </property>
	  <property name= "password" value= "#{db.pwd}" ></property>
	  <property name= " Driverclassname "value=" #{db.driver} "></property>
	  <property name=" url "value=" #{db.url} "></ Property>
	</bean>

The above is just the simplest way to use, Spel is very powerful, the use of good can greatly improve the development efficiency.

In the source code beanfactory.setbeanexpressionresolver (new Standardbeanexpressionresolver ()), register the language parser, you can parse the Spel, So where did you call this parser after that?

It was said in Beanfactory that spring will have a step in the property padding when the bean is initialized, In this step, spring calls the Abstractautowirecapabelbeanfactory class's applypropertyvalues to make the property worth parsing. At the same time this step is generally through the Evaluatebeandefinitionstring method in the Abstractbeanfactory Spel parsing:

	Protected Object evaluatebeandefinitionstring (String value, Beandefinition beandefinition) {
		if ( This.beanexpressionresolver = = null) {
			return value;
		}
		Scope scope = (beandefinition! = null? Getregisteredscope (Beandefinition.getscope ()): null);
		return This.beanExpressionResolver.evaluate (value, new Beanexpressioncontext (this, scope));
	}

When this method is called, it is determined if there is a language parser, and if it is present, the language parser is invoked, and the parsing process is in spring's expression package.

2. Add Applicationcontextawareprocessor processor

Beanfactory.addbeanpostprocessor (New Applicationcontextawareprocessor (this)); In fact, the main purpose is to register a beanpostprocessor, And the real logical implementation is still in the applicationcontextawareprocessor.

Applicationcontextawareprocessor implements the Beanpostprocessor interface, and we look at what we said earlier, before and after the Bean instantiation, which is the init-method of the spring activation bean. The Beanprocessor Postprocessbeforeinitialization method and the Postprocessafterinitialization method are called. Similarly, there are two methods for applicationcontextawareprocessor.

For the Postprocessafterinitialization method, there is not much processing in applicationcontextawareprocessor.

	public object Postprocessafterinitialization (object bean, String beanname) {
		return bean;
	}

Focus on the Postprocessbeforeinitialization method:

	public Object Postprocessbeforeinitialization (Final object bean, String beanname) throws Beansexception {
		AccessControlContext acc = null;

		if (system.getsecuritymanager () = null &&
				(bean instanceof Environmentaware | | Bean instanceof Embeddedvalueresolveraware | |
						Bean instanceof Resourceloaderaware | | Bean instanceof Applicationeventpublisheraware | |
						Bean instanceof Messagesourceaware | | Bean instanceof Applicationcontextaware) {
			acc = this.applicationContext.getBeanFactory (). Getaccesscontrolcontext ();
		}

		if (ACC! = null) {
			accesscontroller.doprivileged (new privilegedaction<object> () {public
				Object run () { C11/>invokeawareinterfaces (bean);
					return null;
				}
			}, ACC);
		}
		else {
			invokeawareinterfaces (bean);
		}

		return bean;
	}

private void Invokeawareinterfaces (Object bean) {if (bean instanceof Aware) {if (Bean instanceof Environmentaware)
			{(environmentaware) bean). Setenvironment (This.applicationContext.getEnvironment ());
						} if (bean instanceof Embeddedvalueresolveraware) {((Embeddedvalueresolveraware) bean). Setembeddedvalueresolver (
			New Embeddedvalueresolver (This.applicationContext.getBeanFactory ())); } if (bean instanceof Resourceloaderaware) {((Resourceloaderaware) bean). Setresourceloader (This.applicationcontext
			); } if (bean instanceof Applicationeventpublisheraware) {((Applicationeventpublisheraware) bean). setapplicationevent
			Publisher (This.applicationcontext);
			} if (bean instanceof Messagesourceaware) {((Messagesourceaware) bean). Setmessagesource (This.applicationcontext); } if (bean instanceof Applicationcontextaware) {((Applicationcontextaware) bean). Setapplicationcontext (This.app
			Licationcontext); }
		}
	}

The invokeawareinterfaces is called in the Postprocessbeforeinitialization method. From the Invokeawareinterfaces method, we can see that the bean that implements these aware interfaces can get some corresponding resources after being initialized.

3. Setting Ignore dependencies

After spring registers applicationcontextawareprocessor, the aware class that is called in the middle of the Invokeawareinterfaces method is no longer a normal bean. such as Resourceloaderaware,applicationeventpublisheraware, it is of course necessary to ignore them when spring does the dependency injection of the bean. And Ignoredependencyinterface's role is exactly the same.

		Several interface
		Beanfactory.ignoredependencyinterface (Resourceloaderaware.class) ignoring automatic assembly are set up;
		Beanfactory.ignoredependencyinterface (applicationeventpublisheraware.class);
		Beanfactory.ignoredependencyinterface (messagesourceaware.class);
		Beanfactory.ignoredependencyinterface (applicationcontextaware.class);
		Beanfactory.ignoredependencyinterface (Environmentaware.class);

4. Registration Dependency

There is a function of ignoring dependencies in spring, as well as the ability to register dependencies.

		Several special rules for automatic assembly are set
		beanfactory.registerresolvabledependency (Beanfactory.class, beanfactory);
		Beanfactory.registerresolvabledependency (Resourceloader.class, this);
		Beanfactory.registerresolvabledependency (Applicationeventpublisher.class, this);
		Beanfactory.registerresolvabledependency (Applicationcontext.class, this);

Once a dependency resolution has been registered, for example, when the Beanfactory.class parsing is registered, when the Bean's attribute is injected, the instance of the beanfactory is injected when the attribute is detected as a beanfactory type.

Some friends may be confused, why just realized the interface and ignore the dependency injection, just ignore the interface of the dependency injection, the following increases the registration dependency function.

Because the implemented interface is also dependent on spring when the bean is loaded, because it is a singleton pattern, these interfaces are already dependent on the other modules in spring, and there is no need to repeat dependencies.

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.