Spring Combat (1)

Source: Internet
Author: User

This blog is a reference to the Spring combat fourth version, the important points of knowledge to summarize.

    • How does spring simplify Java development?

A: (1) lightweight and minimal intrusive programming based on Pojo, (2) loose coupling via Dependency injection and interface-oriented, (3) declarative programming based on facets and conventions, and (4) reduction of boilerplate code through facets and templates;

    • Configure the Spring container three main assembly mechanisms?

A: (1) display configuration in XML, (2) display configuration in Java, (3) implicit bean discovery Mechanism and automatic assembly;

    • Spring to automate assembly from two angles?

A: (1) Component scan: Spring automatically discovers the bean created in the context, (2) automatic assembly: Spring automatically satisfies the dependency between the beans;

    • How does spring's dependency injection di function be implemented?

A: Any meaningful application will consist of two or more classes that work together to accomplish specific business logic. Traditionally, each object is responsible for managing references to objects that are collaborating with itself, which can result in highly coupled and difficult testing. For example, the following code:

As you can see, Damselrescuingknight creates a rescuedamselquest in its constructor. This makes the damselrescuingknight tightly coupled to the rescuedamselquest, thus greatly limiting the Knight's ability to explore. With Di, the object's dependencies are set by the system to coordinate the third-party components of each object when creating the object. Objects do not have to create or manage their dependencies on their own.

As we can see, unlike the previous damselrescuingknight,braveknight, they did not create an adventure task on their own, but instead passed the task as a parameter when exploring.

    • See how Spring Works?

A: Spring loads the bean definitions and assembles them by applying context (application context). The spring context is solely responsible for object creation and assembly.

    • AOP application?

As you can see, Minstrel is a simple class with only two methods. The appropriate function is called before or after the Knight Expedition. How do we combine Bard and Knight adventures? If called in the following way,

So it's reasonable for every knight to go on an expedition and order the Bard to record him? So we use AOP, you can declare that the Bard must go to praise the Knight Expedition, and the knight does not need to control the Bard. Declares the minstrel a slice.

    • Spring helps us to manage beans in two parts, one for registering beans and one for assembling beans.

A: There are three ways to complete these two actions, one is to use automatic configuration, one is to use Javaconfig, and the other is to use the XML configuration method. In the auto-configuration mode, use @component to tell Spring that I am a bean, you want to manage me, and then use the @autowired annotations to assemble the bean (the so-called assembly is the direct collaboration of the management object). Then in Javaconfig, the @Configuration is actually telling the spring,spring how to configure the container (how to register the bean and how to handle the relationship between the beans (assembly)). That's a long time to understand, @Bean mean, when I'm going to get this bean, you spring is going to help me get to this bean in this way. The same goes for the way XML is used. None of the <bean> tags is telling spring how to get the bean, and the various <ref> is the manual configuration bean relationship. For example, there are three different ways of code:

    • Configure the profile bean?

A: The solution provided by spring for environment-related beans is not very different from the solution at the time of the build. Of course, in this project it is necessary to determine the common bean and not to create the bean according to the environment. But spring does not make such decisions at the time of construction, but rather waits for a run to decide. The result is that the same deployment unit is able to use the said environment, and there is no need to reconstruct it. Spring introduces the Bean Profile feature. You can use @profile to specify that the bean belongs to the profile.

When spring determines that the profile is active, it needs to rely on two separate properties: Spring.profiles.active and Spring.profiles.default. If the spring.profiles.active attribute is set, then his value will be used to determine if the profile is activated, and if spring.profiles.active is not set, spring will find Spring.profiles.defaul T value. If both are not set, then no profile is activated. You can set the default profile in Web. Xml.

    • Restrict automatically assembled beans: @qualifier Annotations are one of the main ways to use qualifiers.
    • The scope of the bean?

A: The bean is created as a singleton by default. That is, no matter how many times a bean is injected into another bean, each injection is an instance. Spring defines a variety of scopes that can be used to create beans, including: Singleton (Singleton): Only one instance of the bean is created throughout the application, and the prototype (Prototype): Each time it is injected or acquired through the application context, Creates a new Bean instance; session: In a web app, create a bean instance for each session; Request: In a web app, create a bean instance for each request.

    • Run-time value injection?

A: Spring provides two ways to evaluate at run time: (1) attribute placeholder (property placeholder), (2) Spring expression language (spel).

To configure in XML:

Use Spel to express your language:

Spring Combat (1)

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.