Aspect-Oriented Spring AOP

Source: Internet
Author: User

Spring AOP

The full name of AOP is Aspect Oriented Programming. The purpose of AOP is to separate complex logic and extract commonalities, so that the functions implemented by each part are more specific. For example, in the classic case of Spring AOP implementation, "transaction management", each business class accessing the database may use transaction control to extract its transaction management code from a specific class, put it in a separate place for processing, which greatly simplifies the code of a specific business class.

Aspect (aspect): The "Transaction Management" in the business logic that we are concerned with and can be extracted ".

Joinpoint: The execute () method in the execution process.

Advice (notification): The action performed at a specific connection point. For example, preprocessing before executing the execute () method and post-processing after executing the execute () method.

Pointcut: The action shown in the figure is generated only when the customer calls execute (). You can also set the method for generating the same action, such as Save (), update (), even declare it as "Save. * ", the set of these statements is called the starting point.

Targetobject: an object that contains a connection point, also known as a proxy object. "Business Components" in"

After understanding the concept, let's look at the actual example:

In the Spring IoC section, the SAVE method of dataaccessor. Java is implemented without the separation of transaction management. After we adopt AOP implementation, we expect the implementation code to be changed to the following:

Example 2.1. dataaccessor. Java

public void save(String sql)

{

Statement s = getStatement();

int rows = s.executeUpdate(sql);

}


What a wonderful idea, let's look at the implementation method:

1. The "aspect" In AOP has been clearly defined, that is, "Transaction Management", which is usually implemented by the interceptor. We can customize the interceptor by implementing the General AOP interface methodinterceptor provided by the AOP alliance. In this example, the org. springframework. JDBC. datasource. cetcetransactionmanager transaction management class provided by spring is used.

2. We have also clarified the "target class", that is, dataaccessor. java.

3. Define the start point. In this example, we expect transaction management control when the Save method is executed.

4. Configure in the springconfig. xml file

Example 2.2. springconfig. xml

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName"><value>org.gjt.mm.mysql.Driver</value></property>

<property name="url">

<value>jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=GBK</value>

</property>

<property name="username"><value>root</value></property>

<property name="password"><value></value></property>

</bean>



<bean id="dataAccessor" class="DataAccessor">

<property name="dataSource">

<ref local="dataSource"/>

</property>

</bean>



<bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> (1)

<property name="dataSource">

<ref local="dataSource" />

</property>

</bean>



<bean id="dataAccessorProxy"

class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">(2)

<property name="transactionManager">(3)

<ref bean="transactionManager" />

</property>

<property name="target">(4)

<ref local="dataAccessor" />

</property>

<property name="transactionAttributes">(5)

<props>

<prop key="save">PROPAGATION_REQUIRED</prop>

</props>

</property>

</bean>


(1)

Declare the implementation class of transaction management.

(2)

Inject AOP-related implementation information through the proxy factory class provided by spring.

(3)

Injection transaction implementation class.

(4)

Injection target class.

(4)

Defines the cut point (SAVE ).

After the preceding configuration, modify dataaccessor. java class implementation, because here we use the datasource class provided by spring Org. apache. commons. DBCP. basicdatasource. You can use jdbctemplate to perform database access operations.

Example 2.3. dataaccessor. Java

public class DataAccessor 

{

private DataSource dataSource;



public void setDataSource(DataSource dataSource)

{

this.dataSource = dataSource;

}



public void save()

{

JdbcTemplate template = new JdbcTemplate(this.dataSource);

template.update("insert into game (name) values ('test')");

}

}


The code in SAVE () is exactly what we expect, and there is no link Exception Processing or link transaction management. Imagine if there are 100 such business classes in the project, it can save you a lot of time to repeat this type of code. The most important thing is that we make a business logic to be centrally processed for future maintenance and expansion. This is the benefit of using AOP. When dealing with a complicated business logic in the future, don't forget to think about whether you can solve the problem with the help of the Aspect-oriented thinking.

 

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.