Spring is a lightweight framework, and you will have a lot of unknown terms after Baidu, but these are not important. It is important to understand what changes Spring can make to your daily development, that is
Spring is used to manage objects for us. The change Spring brings to us is that we don't need to create objects manually anymore, we just need to configure them using configuration or annotations.
03-What are the basic principles and mechanisms of Spring?This is a must-ask question for an interview and a common difficulty. Even some interviewers like to ask, "Tell me about Spring?"
I don't know how to say it, but I think it's OK to say that based on my two-month interview experience:
1. The basic implementation mechanisms of Spring are IOC and AOP
2. We commonly call Spring Container, which holds objects. This container is used to manage our Spring objects
IOC is: Inversion of Control,
The Chinese name is: Control Inversion.
It implements that we create objects, from the normal manual new, to Spring creating objects for us.
So how do we understand the IOC process?
From a usage perspective, the entry used is the ApplicationContext.
1, Entrance:
View source-related comments:
package org.springframework.context; import org.springframework.beans.factory.HierarchicalBeanFactory; import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.core.io.support.ResourcePatternResolver; /** * Central interface to provide configuration for an application. * This is read-only while the application is running, but may be * reloaded if the implementation supports this. * * Translation: The central interface is configured to be provided by the application. This is read-only and the application is running, but may reload if supported now * * <p>An ApplicationContext provides: * <ul> * <li>Bean factory methods for accessing application components. * Inherited from {@link org.springframework.beans.factory.ListableBeanFactory}. * <li>The ability to load file resources in a generic fashion. * Inherited from the {@link org.springframework.core.io.ResourceLoader} interface. * <li>The ability to publish events to registered listeners. * Inherited from the {@link ApplicationEventPublisher} interface. * <li>The ability to resolve messages, supporting internationalization. * Inherited from the {@link MessageSource} interface. * <li>Inheritance from a parent context. Definitions in a descendant context * will always take priority. This means, for example, that a single parent * context can be used by an entire web application, while each servlet has * its own child context that is independent of that of any other servlet. * </ul> * * Translation: * <p>ApplicationContext Provide: * <ul> * <li>Bean factory method for accessing application components. * Inherited from {@link org.springframework.beans.factory.ListableBeanFactory}. * <li>The ability to load file resources in a common way. * Inherited from the {@link org.springframework.core.io.ResourceLoader} interface. * <li>Ability to publish events to registered listeners. * Inherited from the {@link ApplicationEventPublisher} interface. * <li>Ability to parse messages and support internationalization. * Inherited from the {@link MessageSource} interface. * <li>Inherit from parent context. Definition in descendant context * Will always take precedence. This means, for example, that a single parent context can be used by the entire Web application, and each servlet has its own subcontext, which is independent of the subcontext of any other servlet. * < /ul> * * <p>In addition to standard {@link org.springframework.beans.factory.BeanFactory} * lifecycle capabilities, ApplicationContext implementations detect and invoke * {@link ApplicationContextAware} beans as well as {@link ResourceLoaderAware}, * {@link ApplicationEventPublisherAware} and {@link MessageSourceAware} beans. * * Translation: * <p>Except for the standard {@link org.springframework.beans.factory.BeanFactory} * Lifecycle capabilities, ApplicationContext implements detection and invocation * {@link ApplicationContextAware} bean And {@link ResourceLoaderAware}, * {@link ApplicationEventPublisherAware} And {@link MessageSourceAware} bean s. * * @author Rod Johnson * @author Juergen Hoeller * @see ConfigurableApplicationContext * @see org.springframework.beans.factory.BeanFactory * @see org.springframework.core.io.ResourceLoader */ public interface ApplicationContext extends ListableBeanFactory, HierarchicalBeanFactory, MessageSource, ApplicationEventPublisher, ResourcePatternResolver {
2, get the corresponding bean object:
3. How does Spring know what bean s to create?
This is configurable based on the annotations we write or the XML.
What is declared is information about a specific bean object, and what is the definition of a bean
Code Notes:
@Controller @Service @Component
XML file:
<bean id="dataSource"> <property name="username" value="$" /> <property name="url" value="$" /> </bean>
This information is parsed through a BD object (BeanDefinition is actually an interface) and stored in a container.
4, BD (BeanDefinition) objects:
/** * A BeanDefinition describes a bean instance, which has property values, * constructor argument values, and further information supplied by * concrete implementations. * Translate: BeanDefinition describes the property values of a Bean instance, the constructor parameter values, and the specific implementation provided by further information. * * <p>This is just a minimal interface: The main intention is to allow a * {@link BeanFactoryPostProcessor} such as {@link PropertyPlaceholderConfigurer} * to introspect and modify property values and other bean metadata. * * This is the smallest interface: the main purpose is to allow one * {@link BeanFactoryPostProcessor}{@link PropertyPlaceholderConfigurer}etc. * Reflect and modify metadata for attribute values and other beans. * * @author Juergen Hoeller * @author Rob Harrop * @since 19.03.2004 * @see ConfigurableListableBeanFactory#getBeanDefinition * @see org.springframework.beans.factory.support.RootBeanDefinition * @see org.springframework.beans.factory.support.ChildBeanDefinition */ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {