Use code to learn spring: IOC and AOP

Source: Internet
Author: User
Download spring from http://www.springframework.org
2. Use eclipse to create a Java Project
3. Establish our business method Interface
Public interface businessobject {
Public void dosomething ();
Public void doanotherthing ();
}
Import org. Apache. commons. Logging. log;
Import org. Apache. commons. Logging. logfactory; public interface businessobject {
Public void dosomething ();
Public void doanotherthing ();
}
Import org. Apache. commons. Logging. log;
Import org. Apache. commons. Logging. logfactory;

4. Implement the business method. Note that setwords uses dependency injection. The so-called dependency injection is to put the strings in the configuration file into our program automatically when the program is running. If this is not the case, we can only fix these things in the code, which violates the object-oriented Dependency inversion principle. There is also a method to satisfy the Dependency inversion, that is, dependency query, this is the so-called factory mode, that is, to request an abstract thing in the code, and then get it according to the configuration, but this method injects more environment dependencies into dependencies, in addition, the Code is redundant, and the JNDI query of EJB belongs to this type. In addition, our spring configuration file is bean-centered, which is a class we write. It describes its name, location, covered content, and relationship in XML.
Public class businessobjectimpl implements businessobject {
Private string words;
Public void setwords (string words ){
This. Words = words;
}
Public void dosomething (){
Log = logfactory. getlog (this. getclass ());
Log.info (words );
}
Public void doanotherthing (){
Log = logfactory. getlog (this. getclass ());
Log.info ("Another thing ");
}

} Public class businessobjectimpl implements businessobject {
Private string words;
Public void setwords (string words ){
This. Words = words;
}
Public void dosomething (){
Log = logfactory. getlog (this. getclass ());
Log.info (words );
}
Public void doanotherthing (){
Log = logfactory. getlog (this. getclass ());
Log.info ("Another thing ");
}

}

5 create a running method class, read the definition of the Bo class from the configuration file spring-beans.xml, and instantiate an object
Import org. springframework. Beans. Factory. xml. xmlbeanfactory;
Import org. springframework. Core. Io. classpathresource;

Public class main {
Public static void main (string [] ARGs ){
Xmlbeanfactory xbf = new xmlbeanfactory (New classpathresource ("spring-beans.xml "));
Businessobject BO = (businessobject) xbf. getbean ("Bo ");
Bo. dosomething ();
Bo. doanotherthing ();
}
} Import org. springframework. Beans. Factory. xml. xmlbeanfactory;
Import org. springframework. Core. Io. classpathresource;

Public class main {
Public static void main (string [] ARGs ){
Xmlbeanfactory xbf = new xmlbeanfactory (New classpathresource ("spring-beans.xml "));
Businessobject BO = (businessobject) xbf. getbean ("Bo ");
Bo. dosomething ();
Bo. doanotherthing ();
}
}

6. Setting up an interceptor class invoke is a required method of methodinterceptor, which indicates the action during interception. You can carefully understand the meaning of the Code.
Import org. aopalliance. Intercept. methodinterceptor;
Import org. aopalliance. Intercept. methodinvocation;
Import org. Apache. commons. Logging. log;
Import org. Apache. commons. Logging. logfactory;

Public class myinterceptor implements methodinterceptor {
Private string before, after;
Public void setafter (string after ){
This. After = after;
}
Public void setbefore (string before ){
This. Before = before;
}
Public object invoke (methodinvocation Invocation) throws throwable {
Log = logfactory. getlog (this. getclass ());
Log.info (Before );
Object rval = invocation. Proceed ();
Log.info (after );
Return rval;
}
} Import org. aopalliance. Intercept. methodinterceptor;
Import org. aopalliance. Intercept. methodinvocation;
Import org. Apache. commons. Logging. log;
Import org. Apache. commons. Logging. logfactory;

Public class myinterceptor implements methodinterceptor {
Private string before, after;
Public void setafter (string after ){
This. After = after;
}
Public void setbefore (string before ){
This. Before = before;
}
Public object invoke (methodinvocation Invocation) throws throwable {
Log = logfactory. getlog (this. getclass ());
Log.info (Before );
Object rval = invocation. Proceed ();
Log.info (after );
Return rval;
}
}

7. Establish the relationship between classes in the configuration file organization. AOP has two important concepts: entry point and enhancement. The two concepts are combined to add execution when a method is executed, the entry point indicates the append, the enhancement indicates the append, And the mypointcut in the configuration file indicates the entry point. myinterceptor indicates the enhancement content, and myadvisor indicates the Enhancement Tool, that is, the combination of the two. In the Bo bean, we will attach this booster to the bean Bo.

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd">
<Beans>
<Bean id = "businessobjectimpl" class = "businessobjectimpl">
<Property name = "Words">
<Value> executing business method </value>
</Property>
</Bean>
<Bean id = "myinterceptor" class = "myinterceptor">
<Property name = "before">
<Value> before executing a business method </value>
</Property>
<Property name = "after">
<Value> after the business method is executed </value>
</Property>
</Bean>
<Bean id = "mypointcut" class = "org. springframework. AOP. Support. jdkregexpmethodpointcut">
<Property name = "patterns">
<List>
<Value> businessobject. dosomething </value>
</List>
</Property>
</Bean>
<Bean id = "myadvisor" class = "org. springframework. AOP. Support. defaultpointcutadvisor">
<Property name = "pointcut" ref = "mypointcut"/>
<Property name = "advice" ref = "myinterceptor"/>
</Bean>
<Bean id = "Bo" class = "org. springframework. AOP. Framework. proxyfactorybean">
<Property name = "target">
<Ref local = "businessobjectimpl"/>
</Property>
<Property name = "proxyinterfaces">
<Value> businessobject </value>
</Property>
<Property name = "interceptornames">
<List>
<Value> myinterceptor </value>
<Value> myadvisor </value>
</List>
</Property>
</Bean>
</Beans> <? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd">
<Beans>
<Bean id = "businessobjectimpl" class = "businessobjectimpl">
<Property name = "Words">
<Value> executing business method </value>
</Property>
</Bean>
<Bean id = "myinterceptor" class = "myinterceptor">
<Property name = "before">
<Value> before executing a business method </value>
</Property>
<Property name = "after">
<Value> after the business method is executed </value>
</Property>
</Bean>
<Bean id = "mypointcut" class = "org. springframework. AOP. Support. jdkregexpmethodpointcut">
<Property name = "patterns">
<List>
<Value> businessobject. dosomething </value>
</List>
</Property>
</Bean>
<Bean id = "myadvisor" class = "org. springframework. AOP. Support. defaultpointcutadvisor">
<Property name = "pointcut" ref = "mypointcut"/>
<Property name = "advice" ref = "myinterceptor"/>
</Bean>
<Bean id = "Bo" class = "org. springframework. AOP. Framework. proxyfactorybean">
<Property name = "target">
<Ref local = "businessobjectimpl"/>
</Property>
<Property name = "proxyinterfaces">
<Value> businessobject </value>
</Property>
<Property name = "interceptornames">
<List>
<Value> myinterceptor </value>
<Value> myadvisor </value>
</List>
</Property>
</Bean>
</Beans>

8. Run the main class, observe the output result on the console, review the code, and reflect on why this result occurs.

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.