• preface

  • Some concepts and ideas of Spring

  • About the IOC

  • About AOP

  • How Spring integrates with the Web

preface

If there are any frameworks that Java programmers will inevitably learn and use, Spring is certainly one of them. This blog will provide a systematic summary of how bloggers use Spring in their daily work.

Some concepts and ideas of Spring

The core of Spring: AOP, IOC.

To put it simply, this means handing over objects to Spring to manage and implementing “template” operations through section-oriented programming, freeing programmers to focus more on business implementation.

Spring is already a one-stop open source framework solution, and the Spring ecosystem is formed.


Java advanced learning Q group: 8515318105; Java advanced learning Q group: 8515318105 You can receive the 2019 Java Architect Advanced Learning Materials and BAT interview questions.

About the IOC

IOC, operationally, is implemented either through XML configuration or through annotations. In practice, annotations are becoming more and more popular.

Depending on the functionality you use, you need to introduce dependencies, as well as XML Schema constraint references.

The name and location of Spring’s core configuration file are not fixed, but in practice, it is usually specified as applicationContext-XXX.xml. This approach has two advantages. First, Spring configuration files can be managed by modules. Second, due to the uniform prefix, it is convenient to load these profiles with re.

Spring

XML configuration for bean creation

This assumes that the class has a no-argument constructor (the principle behind it is instantiated by reflection). This approach is the most commonly used in practice, but there are also static factories and instance factories.

Another point to note is: are beans singletons or multiinstances?

The scope attribute exists in the bean tag to say:

Singleton: singleton, default

Prototype: more cases

Request/session, etc.

Bean property injection :XML

The XML creation of a bean is done through reflection, so how does the property injection of a bean work?

You can provide a constructor with parameters to set when constructing the bean;

You can set it when you provide setter methods; (Most commonly used)

Interface injection, P namespace injection, none of that is really needed…


or


Either give the attribute value directly with value, or reference another bean through ref.

Annotation-based bean creation and injection

Since the implementation of Spring annotations requires AOP support, there are dependency concerns. Second, turn on annotation scanning in XML:


In fact, this configuration will have Spring scan under the specified package, instantiate the annotated bean, and perform property injection. (You can refer to Writing My First Framework: Spring MVC Mini)

Create object with 4 annotations:

@Component/@Controller/@Service/@Repository

On these four annotations, the id of the bean is specified through the value attribute, and singleton OR multiple instances are declared through the @scope coordination. (For now, all four annotations are the same, just to make the annotation classes more clear, and Spring has left the door open for future enhancements.)

How do I inject properties?

@Autowired/@Resource/@Qualifier

It is important to note that @Resource comes under the Javax package, which is provided by J2EE; And @AutoWired is provided by Spring. (You don’t have to provide setter methods)

@resource is injected by name by default. If it cannot be found, it is injected by type.

@autoWired is injected by type by default. You can inject names with @qualifier.

So what do we need to pay attention to?

If @AutoWired does type injection, it’s likely that multiple types will satisfy (polymorphic), so which one to inject? So, if you inject as @autoWired, be aware of this, combined with @qualifier. In real development, obviously, injection should be deterministic, so injection by name should be preferred!

IOC VS DI

IOC, Inversion of control; DI, dependency injection;

Spring can help set properties only if you hand the object to Spring; Therefore, dependency injection cannot exist alone and needs to be done on top of IOC.


Java advanced learning Q group: 8515318105; Java advanced learning Q group: 8515318105 You can receive the 2019 Java Architect Advanced Learning Materials and BAT interview questions.

About AOP

The implementation of AOP, a horizontal extraction mechanism, relies on the dynamic proxy pattern.

A few key concepts about AOP:

JoinPoint: To put it bluntly, is a method that can be enhanced.

PointCut: PointCut, which JoinPoints are intercepted;

Advice:

Aspect: the process of applying an enhancement to a specific method

Spring AOP needs to be implemented with AspectJ, either through XML or through annotations.

For example, using XML, you need to specify which methods of class A are used to enhance which methods of class B. This is where execution expressions are involved.

With annotations, for example, it’s much easier to turn on AOP () in XML and then use something like @before (value=”execution “) on enhancement methods.

In fact, the most common use of AOP in real development is transactions.

Spring transaction management

Spring’s declarative transaction management is mostly annotation-based. First we need to configure a transaction manager, and the transaction manager requires us to inject the DataSource (DBCP, C3P0, etc.), which makes sense since it’s a DB transaction. Keep in mind that Spring has different DAO layer frameworks (Spring JDBC/MyBatis/Hibernate…) Different transaction implementation classes are provided.

Here’s an example:

Spring annotates transaction configuration

For multiple data sources, of course, we need to define multiple transaction managers and also enable transaction annotations. Multiple transaction managers, which can be distinguished by the Qualifier attribute.

After the configuration is complete, use it directly on the class or method of the service layer

@Transactional(value = “gcs”, rollbackFor = Exception.class)

How Spring integrates with the Web

In practice, you configure a listener in web.xml and specify the Spring configuration file.

For every WEB project, there is a ServletContext object, and the listener we configure is listening on it. The listener loads the Spring configuration file, puts the created object into the ServletContext domain (the setAttribute method), Get the object by getAttribute.

Well, at this point, Spring combing is over!


Java advanced learning Q group: 8515318105; Java advanced learning Q group: 8515318105 You can receive the 2019 Java Architect Advanced Learning Materials and BAT interview questions.