Spring learning of Java

1. First know what Spring is.

Spring is a layered lightweight open source framework. It uses inversion control IOC and aspect-oriented AOP as the core. It provides many enterprise-level application technologies such as presentation layer spring MVC, persistence layer Spring JDBC and business layer transaction management.

Spring has always implemented and adhered to: good design is better than specific implementation, and the code should be easy to test. Therefore, Spring provides an IOC container, which transfers the dependencies between objects to spring to control, avoiding hard coding and causing excessive coupling. Provided by Spring With the AOP function, users can replace some of the more difficult problems using object-oriented programming by aspect-oriented.

2. Spring architecture (framework)

 3. The role of each annotation in Spring and how to use it.

@Component-----------------------Generally refers to components. When components are not well classified, we can use this annotation to mark them. (Component-------component; component; part)

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

@Autowired-------------(Autowired)

@Repository------------- is used to mark data access components, namely DAO components (repository-------warehouse; storage room, container. )

@Service---------------------------- is used to mark business layer components (the service layer we usually define uses this)  

@Controller------------------------- is used to mark control layer components (such as action in struts)

When writing code, you can put the above annotations before the corresponding classes, methods, constructors, and entity variables, and the Spring mechanism will scan these tags and assemble them.

Generally, a configuration file is stored in the resources folder, called the applicationContext.xml file.

Among them, < context:component-scan  base-package=”com.eric.spring” > means to scan the class package, and automatically convert the class marked with Spring's annotation into Bean, and complete the Bean injection at the same time.

The injection method is generally to inject the implementation class of DAO into the implementation class of the service, and inject the interface of the service (note that it is not the implementation class of the service) into the action. Then the property does not need getter() and setter() after adding @Autowired method, Spring will also automatically inject it.

The injection of entity variables, methods, and constructors generally uses @Resource or @Autowired. These variables will be injected with the class in the configuration file and converted into beans. The convenience of this is that the setter method can be omitted.

4. Spring core IOC inversion of control.

Spring describes the dependencies between beans and beans through a configuration file, and uses the reflection function of the Java language to instantiate beans and establish dependencies between beans.

5. Let's see how to assemble a Bean.

In the configuration file of the Spring container, define a simple bean 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 can be obtained through the container's getBean("foo"), which can be used to locate and search in the container. The role of the external program and Spring IOC is a bridge. The Class attribute specifies the implementation class corresponding to the Bean.

The id must be unique in the IOC container, and the id must conform to the xml naming specification, that is, it must start with a letter, followed by alphanumeric hyphens, underscores, periods, colons, and other complete terminators.

The name attribute can also be used, but since the name of the name attribute can not comply with the naming convention and there can be multiple beans with duplicate names in the same IOC container, only the last bean will take effect, and the previous bean will be overwritten. So It is recommended to name beans by id as much as possible.

6. Dependency Injection.

6.1 Property injection

Property injection is a very common injection method. The injected bean is required to have a default constructor, and the injected property must have a setter method. Spring will instantiate a bean through the default constructor and reflect it through Inject property values.

If a Bean already has a constructor with parameters, it must also provide a default constructor without parameters. Otherwise, an exception will be thrown during property injection. The following figure is an ioc fragment of property injection:

 6.2 Constructor injection.

First look there are two constructors:

 

How to write the IOC fragment:

 

When the constructor is injected, it is generally necessary to specify the parameters as index, typoe and value to ensure that the specified constructor is injected.

6.3 Circular Dependency Injection

It means that there is a mutual dependency between the injected two beans. This will lead to an infinite loop

6.4 Reference other beans in the IOC container.

As usual, first look at an IOC segment:

 

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

So here's how the ref element applies the bean:

1. Reference the bean of the current container or the parent container through the bean property, this is the most common way.

2. By referencing the bean of the same configuration file through the local attribute, he can use the xml parser to check the validity of the reference, so that the developer can check his own mistakes in the programming stage.

3. The parent property, which is used to refer to the bean of the parent container

Take an example of a parent-child container, where the child container references the parent container:

6.5 Using the p namespace to simplify xml configuration for attribute injection

First look at the property injection IOC fragment not using the p namespace:

With the p namespace, property injection simplifies a lot:

6.6 Scope of beans in Spring

As shown in the figure:

6.7 AnnotationConfigApplicationContext is used to start the Spring container.

6.8 The following table shows the difference between the three configuration beans

 

These configurations have no pros and cons, only suitable usage scenarios

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325391393&siteId=291194637