이 콘텐츠는 선택한 언어로 제공되지 않습니다.
5.3. Applying An Aspect
Now that you have a valid distribution containing everything you need, you can configure
jboss-beans.xml
to apply the audit aspect. It is in examples/User_Guide/gettingStarted/commandLineClient/target/client-aop.dir
.
Procedure 5.2. Explanation of the Code to Apply an Aspect
- Before you can apply your aspect to any classes, you need to create an instance of
org.jboss.aop.AspectManager
using a <bean> element. A factory method is used here instead of calling a conventional constructor, since only one instance of the AspectManager in the JVM is necessary at run-time. - Next an instance of our aspect called AuditAspect is created, using the <aop:aspect> element. This looks similar to the <bean> element because it has name and class attributes that are used in the same way. However it also has method and pointcut attributes that you can use to apply or bind an advice within the aspect to constructors and methods within other classes. These attributes bind the audit advice to all public constructors and methods within the
HRManager
class. Only theaudit
method needs to be specified, since it has been overloaded within theAuditAspect
class with different parameters. JBoss AOP knows at run-time which to select, depending on whether a constructor or method invocation is being made.
This additional configuration is all that is needed to apply the audit aspect at run-time, adding auditing behavior to the Human Resources service. You can test this by running the client using the
run.sh
script. A log
directory is created on start-up alongside the lib
directory when the AuditAspect
bean is created by the Microcontainer. Each deployment of the Human Resources service causes a new log file to appear within the log
directory. The log file contains a record of any calls made from the client to the service. It is named something similar to auditLog-28112007-163902
, and contains output similar to Example 5.4, “Example AOP Log Output”.
Example 5.4. Example AOP Log Output
To remove the auditing behavior, comment out the relevant fragments of XML in the deployment descriptor and restart the application.
Warning
The order of deployment matters. Specifically each aspect must be declared before the beans that it applies to, so that the Microcontainer deploys them in that order. This is because the Microcontainer may need to alter the bytecode of the normal bean class to add the cross-cutting logic, before it creates an instance and stores a reference to it in the controller. If a normal bean instance has already been created, this is not possible.