Spring Spring learn the basics of learning the basics

Excerpt: https://www.cnblogs.com/leton/p/11755192.html

 

Spring learning the basics

 

Spring summary

Spring in the distribution frame, contains a total of 20 different modules, it can be divided into 6 different categories of functions.

Spring Overall Chart

In order to reduce the complexity of Java development, Spring has taken the following four key strategies:

  • Lightweight and minimally invasive programming of POJO;
  • Dependency injection oriented interfaces and loose coupling;
  • Section and practices based on declarative programming;
  • Reduce boilerplate code via cut and templates.

Dependency Injection DI

Create a collaborative relationship between the application object's behavior is often referred to as assembly (wiring), this is also dependent on the nature of injection (DI) of.

When describing how to assemble bean, Spring has a great deal of flexibility, it offers three main assembly mechanisms:

  • To be explicitly configured in XML.
  • To be explicitly configured in Java.
  • Implicit bean discovery mechanism and automatic assembly.

Automated assembly bean

Spring from two angles to automate the assembly:

  • Component scans (component scanning): Spring will automatically find the application context bean created.
  • Automatic assembly (autowiring): Spring automatically satisfy the dependencies between the bean.
    Scanning and automatic assembly components together can play a great power, which you can be reduced to a minimum explicit configuration.

@Component notes show that this class is a component class, and inform the Spring bean to be created for the class
@ComponentScan enable annotation component scans
@Autowired notes for automatic assembly

Java code assembly bean

@Bean annotation tells Spring This method returns an object that you want to register for the Spring application context bean.

By assembling XML bean

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="knight" class="com.fiberhome.tongl.knight.BraveKnight"> <constructor-arg ref="quest" /> </bean> <bean id="quest" class="com.fiberhome.tongl.knight.SlayDragonQuest"> <constructor-arg value="#{T(System).out}" /> </bean> </beans>

When you declare DI in XML, there will be a variety of optional configuration scheme and style. Specific to the constructor injection, there are two basic configuration options available:

  • element
  • C- Spring 3.0 namespace using the introduced

Injecting property, there are two basic configuration options available:

  • element
  • p- namespace

Spring configuration uses three kinds of summary

Use automatic configuration mechanism as much as possible. Explicitly configured as little as possible to avoid explicitly configured caused by maintenance costs. When you have to explicitly configure the bean (for example, some of the source code is not up to you to maintain, and when you need to configure the bean to the code time), priority should be selected based on Java configuration, it is more than the XML-based configuration powerful, safe and easy type of reconstruction. Finally, when you only want to use the convenience of XML namespaces, and do not achieve the same in the JavaConfig, you should use XML.

scope of the bean

Spring defines a variety of scopes can be created based on these bean scope, including:

  • Singleton (Singleton): throughout the application, only create an instance of the bean.
  • Prototype (Prototype): each injection or when acquired by the Spring application context, creates a new bean instance.
  • Session (Session): In the Web application, create a bean instance for each session.
  • Request (Rquest): In the Web application, create a bean instance for each request.

Singleton scope is the default, but, as previously described, for the type of variable, this is not appropriate. If you choose a different scope, to use @Scope notes, it can be used with @Component or @Bean.

Injecting external value

In the Spring, the easiest way is to declare an external source processing attribute values ​​and to retrieve the attributes Spring Environment.

@PropertySource reference classpath .properties files.
@Value placeholder property (Configuration PropertySourcesPlaceholderConfigurer bean)

Aspect Oriented Programming (aspect-oriented programming, AOP)

DI contribute to decoupling between the application object, and can achieve AOP crosscutting concerns decoupling between the point and the objects they affect.

Cut point expression

 

Use annotations created section

@AspectJ annotation solution showed that class is not just a POJO, or a section

@EnableAspectJAutoProxy comment enable automatic proxy function

AspectJ provides five annotations to define notification

  • After @After notification method returns or throws an exception in the target method invocation
  • @AfterReturning notification method will be called after the target method returns
  • @AfterThrowing after notification method will throw an exception in the target method invocation
  • @Around notification method will encapsulate the target method
  • @Before notification method will be executed before the target method invocation

Reference

  • "Spring combat (Fourth Edition)"
 
Good text to the top follow me The paper collection
0
0
 
 
 
<< Previous: the Java Concurrency summary

Spring summary

Spring in the distribution frame, contains a total of 20 different modules, it can be divided into 6 different categories of functions.

Spring Overall Chart

In order to reduce the complexity of Java development, Spring has taken the following four key strategies:

  • Lightweight and minimally invasive programming of POJO;
  • Dependency injection oriented interfaces and loose coupling;
  • Section and practices based on declarative programming;
  • Reduce boilerplate code via cut and templates.

Dependency Injection DI

Create a collaborative relationship between the application object's behavior is often referred to as assembly (wiring), this is also dependent on the nature of injection (DI) of.

When describing how to assemble bean, Spring has a great deal of flexibility, it offers three main assembly mechanisms:

  • To be explicitly configured in XML.
  • To be explicitly configured in Java.
  • Implicit bean discovery mechanism and automatic assembly.

Automated assembly bean

Spring from two angles to automate the assembly:

  • Component scans (component scanning): Spring will automatically find the application context bean created.
  • Automatic assembly (autowiring): Spring automatically satisfy the dependencies between the bean.
    Scanning and automatic assembly components together can play a great power, which you can be reduced to a minimum explicit configuration.

@Component notes show that this class is a component class, and inform the Spring bean to be created for the class
@ComponentScan enable annotation component scans
@Autowired notes for automatic assembly

Java code assembly bean

@Bean annotation tells Spring This method returns an object that you want to register for the Spring application context bean.

By assembling XML bean

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="knight" class="com.fiberhome.tongl.knight.BraveKnight"> <constructor-arg ref="quest" /> </bean> <bean id="quest" class="com.fiberhome.tongl.knight.SlayDragonQuest"> <constructor-arg value="#{T(System).out}" /> </bean> </beans>

When you declare DI in XML, there will be a variety of optional configuration scheme and style. Specific to the constructor injection, there are two basic configuration options available:

  • element
  • C- Spring 3.0 namespace using the introduced

Injecting property, there are two basic configuration options available:

  • element
  • p- namespace

Spring configuration uses three kinds of summary

Use automatic configuration mechanism as much as possible. Explicitly configured as little as possible to avoid explicitly configured caused by maintenance costs. When you have to explicitly configure the bean (for example, some of the source code is not up to you to maintain, and when you need to configure the bean to the code time), priority should be selected based on Java configuration, it is more than the XML-based configuration powerful, safe and easy type of reconstruction. Finally, when you only want to use the convenience of XML namespaces, and do not achieve the same in the JavaConfig, you should use XML.

scope of the bean

Spring defines a variety of scopes can be created based on these bean scope, including:

  • Singleton (Singleton): throughout the application, only create an instance of the bean.
  • Prototype (Prototype): each injection or when acquired by the Spring application context, creates a new bean instance.
  • Session (Session): In the Web application, create a bean instance for each session.
  • Request (Rquest): In the Web application, create a bean instance for each request.

Singleton scope is the default, but, as previously described, for the type of variable, this is not appropriate. If you choose a different scope, to use @Scope notes, it can be used with @Component or @Bean.

Injecting external value

In the Spring, the easiest way is to declare an external source processing attribute values ​​and to retrieve the attributes Spring Environment.

@PropertySource reference classpath .properties files.
@Value placeholder property (Configuration PropertySourcesPlaceholderConfigurer bean)

Aspect Oriented Programming (aspect-oriented programming, AOP)

DI contribute to decoupling between the application object, and can achieve AOP crosscutting concerns decoupling between the point and the objects they affect.

Cut point expression

 

Use annotations created section

@AspectJ annotation solution showed that class is not just a POJO, or a section

@EnableAspectJAutoProxy comment enable automatic proxy function

AspectJ provides five annotations to define notification

  • After @After notification method returns or throws an exception in the target method invocation
  • @AfterReturning notification method will be called after the target method returns
  • @AfterThrowing after notification method will throw an exception in the target method invocation
  • @Around notification method will encapsulate the target method
  • @Before notification method will be executed before the target method invocation

Reference

  • "Spring combat (Fourth Edition)"

Guess you like

Origin www.cnblogs.com/xichji/p/11757122.html