Sample notifications for the aspect used by Spring AOP

Source: Internet
Author: User

1. Interface Class computerinterf. Java

 

package com.stelcomtech;public interface ComputerInterf { public void buyPc(); public void buySome(); public void test();}

2. Computer. Java

package com.stelcomtech;public class Computer implements ComputerInterf{ private String pcName="HPT7600"; private int pcPrice=5000; public String getPcName() {  return pcName; } public void setPcName(String pcName) {  this.pcName = pcName; } public int getPcPrice() {  return pcPrice; } public void setPcPrice(int pcPrice) {  this.pcPrice = pcPrice; }

 

package com.stelcomtech;
Public class computer implements computerinterf {private string pcname = "hpt7600"; private int pcprice = 5000; Public String getpcname () {return pcname;} public void setpcname (string pcname) {This. pcname = pcname;} public int getpcprice () {return pcprice;} public void setpcprice (INT pcprice) {This. pcprice = pcprice;} public void buypc () {system. out. println ("computer name:" + this. getpcname () + "; Price:" + Th Is. getpcprice ();} public void buysome () {system. Out. println ("I just want to buy a monitor! ") ;}Public void test (){}}

 

3. Proxy daili1.java

Package COM. stelcomtech; import Org. aopalliance. intercept. methodinterceptor; import Org. aopalliance. intercept. methodinvocation; public class daili1 implements methodinterceptor {public object invoke (methodinvocation arg0) throws throwable {system. out. println ("This proxy sends the mouse"); object = arg0.proceed (); return object ;}}

 

Package COM. stelcomtech; import Java. lang. reflect. method; import Org. springframework. AOP. methodbeforeadvice; public class daili implements methodbeforeadvice {public void before (method arg0, object [] arg1, object arg2) throws throwable {system. out. println ("welcome ");}}

 

Package COM. stelcomtech; import Java. lang. reflect. method; import Org. springframework. AOP. afterreturningadvice; public class after implements afterreturningadvice {public void afterreturning (Object arg0, method arg1, object [] arg2, object arg3) throws throwable {system. out. println ("Welcome next time! ");}}

 

4. Implement mainaop. Java

Package COM. stelcomtech; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. filesystemxmlapplicationcontext; public class mainaop {public static void main (string [] ARGs) {applicationcontext context = new filesystemxmlapplicationcontext ("src/COM/stelcomtech/applicationcontext. XML "); computerinterf pcinterf = (computerinterf) context. getbean ("proxy"); pcinterf. buypc (); pcinterf. buysome (); // pcinterf. test (); test the interception of the entire class in the configuration file }}

 

5. configuration file applicationcontext. xml

<? 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-2.0.xsd"> <bean id = "daili" class = "com. stelcomtech. daili "abstract =" false "lazy-init =" default "dependency-check =" default "> </bean> <bean id =" computer1 "class =" com. stelc Omtech. computer "abstract =" false "lazy-init =" default "dependency-check =" default "> </bean> <bean id =" after "class =" com. stelcomtech. after "abstract =" false "lazy-init =" default "dependency-check =" default "> </bean> <! -- Proxy Bean --> <bean id = "proxy" class = "org. springframework. AOP. framework. proxyfactorybean "abstract =" false "lazy-init =" default "dependency-check =" default "> <property name =" proxyinterfaces "> <! -- Proxy interface class --> <value> com. stelcomtech. computerinterf </value> </property> <property name = "target"> <! -- The new proxy acts as "notification" --> <ref bean = "computer1"/> </property> <property name = "interceptornames"> <! -- Proxy target class --> <list> <value> buybeforeadvisor </value> <value> buyafteradvisor </value> </List> </property> </bean> <! -- Buy notification --> <bean id = "buybeforeadvisor" class = "org. springframework. AOP. support. regexpmethodpointcutadvisor "abstract =" false "lazy-init =" default "dependency-check =" default "> <property name =" advice "> <ref bean =" daili "/> </property> <property name = "patterns"> <! -- Where proxy is used --> <value>. * Buy. * </value> <! -- <Value>. * COM \. stelcomtech \. computerinterf. * </value> --> </property> </bean> <bean id = "buyafteradvisor" class = "org. springframework. AOP. support. regexpmethodpointcutadvisor "abstract =" false "lazy-init =" default "dependency-check =" default "> <property name =" advice "> <ref bean =" after "/> </property> <property name = "patterns"> <! -- Where proxy is used --> <value>. * Buy. * </value> <! -- <Value>. * COM \. stelcomtech \. computerinterf. * </value> --> </property> </bean> </beans>

. Test Results

Log4j: warn no appenders cocould be found for logger (Org. springframework. Context. Support. filesystemxmlapplicationContext ).
Log4j: Warn please initialize the log4j system properly.
Welcome
Computer Name: hpt7600; Price: 5000
Welcome to the next visit!

 

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.