Java's Spring Learning

Source: Internet
Author: User
Tags aop reflection xml parser

1. First of all know what spring is.

Spring is a lightweight, open-source framework for layering. The inversion controls the IOC, and the aspect becomes AOP as the kernel. offers a wide range of enterprise-class applications such as presentation layer Spring MVC, persistent layer spring JDBC, and business layer transaction management.

Spring has been implemented and adhered to: a good design is better than a specific implementation, and the code should be easy to test. So spring provides the IOC container, which gives the dependency between objects to spring to control, and avoids the hard coding causing over-coupling. With the AOP functionality provided by spring, users can become more difficult to use for object-oriented programming by facing facets instead of some.

2. Spring Architecture (frame structure)

3. The role of each annotation in spring and how it is used.

@Component------------------------A generic component, we can use this annotation for labeling when the component is poorly categorized. (Component-------components; components; parts)

@Resource------------------------(Resources)

@Autowired-----------------------(auto bind)

@Repository-----------------------The callout data access component, the DAO component (Repository-------Warehouse; storage room, container. )

@Service----------------------------for labeling business layer components (this is the service layer we typically define)

@Controller-------------------------for labeling control-layer components, such as action in struts

When writing code, the above annotations can be placed before the corresponding class, method, constructor, and entity variables, and the spring mechanism will scan the tags and assemble them.

Usually in the Resources folder to store a configuration file, called Applicationcontext.xml file.

where <context:component-scan base-package= "com.eric.spring"> represents the scan class package, The class that labels spring's annotations automatically transforms the bean and completes the bean injection.

The injection method typically injects the DAO's implementation class into the service's implementation class, injecting the service's interface (note not the service's implementation class) into the action. Then the Getter () and setter () methods are not required after the attribute is added to the @autowired , spring will also be injected automatically.

Entity variables, methods, and constructors are typically injected with @Resource or @Autowired, and these variables are injected and converted into beans along with the class in the configuration file, so it is convenient to omit the setter method.

4. Spring Core's IOC control inversion.

Spring describes the dependencies between beans and beans through a configuration file that uses the Java language's reflection capabilities to instantiate beans and establish dependencies between beans.

5. Here's a look at how to assemble a bean.

In the Spring container's configuration file, define a simple bean's configuration fragment, as follows:

In general, a spring IOC bean is a bean in the corresponding file, where the ID is the name of the bean, and the corresponding bean is obtained through the container's Getbean ("foo"), which acts as a location and lookup in the container. is a bridge between the external program and the spring IOC. The Class property specifies the classes of the implementation that the bean corresponds to.

The ID must be unique within the IOC container, and the ID must conform to the XML naming convention, that is, you must start with a letter, followed by a full terminator such as the alpha-numeric hyphen underscore period colon.

You can also use the Name property, but because the Name property can not follow the naming convention and the same IOC container can have multiple duplicate names of beans, then only the last bean will take effect and the previous bean will be overwritten. Therefore, it is recommended to name the bean with ID.

6. Dependency Injection. 6.1 Attribute Injection

Attribute injection is a very common method of injection. The bean that is required to be injected has a default constructor, and the injected attribute must have a setter method. Spring instantiates a bean through the default construction method and injects the property value by reflection.

If a bean already has a constructed method with parameters, you must provide a default constructor with no parameters at the same time. Otherwise, an exception is thrown when the attribute is injected. is an IOC fragment of attribute injection:

6.2 constructor Method injection.

First of all, there are two constructors:

How the IOC fragment is written:

When constructing a method, it is common to specify the parameters of index, Typoe, and value to inject the specified construction method.

6.3 Cyclic Dependency Injection

Refers to the existence of dependencies between the injected two beans. So we're stuck in a dead loop.

Other beans are referenced in the 6.4 IOC container.

As usual, look at an IOC segment first:

Here you can see that the properties in the Boss Bean refer to the car bean, through the ref element.

So here's the way that the ref element applies the Bean:

1. This is the most common way to refer to the bean of the current container or parent container through the Bean property.

2. By referencing the bean of the same profile with the local property, he can use an XML parser to verify the legitimacy of the reference so that developers can check their errors during the programming phase.

3. Parent property, which is used to refer to the parent container's bean

To take a parent-child container, the child container refers to an example of the parents container:

6.5 using the P-namespace to simplify the XML configuration of attribute injection

First, take a look at the attributes that are not using the P namespace to inject IOC fragments:

With the P namespace, attribute injection simplifies a lot:

6.6 Scope of beans in spring

:

The 6.7 annotationconfigapplicationcontext is used to start the spring container.

6.8 The table below represents the differences between the three configuration beans

These configurations are not good or bad, only suitable usage scenarios

Java's Spring Learning

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.