Locate the „web.xml“ file under /src/main/webapp/WEB-INF and add the following :
For loading the Spring context upon startup,
1 | < listener > |
2 | < listener-class > |
3 | org.springframework.web.context.ContextLoaderListener |
4 |
listener-class > |
5 |
listener > |
Now lets create the applicationContext.xml file that will drive Spring container. Create the file under /src/main/webapp/WEB-INF directory. An example „applicationContext.xml“ is presented below
01 | < beans xmlns = "http://www.springframework.org/schema/beans" |
02 | xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:p = "http://www.springframework.org/schema/p" |
03 | xmlns:aop = "http://www.springframework.org/schema/aop" xmlns:context = "http://www.springframework.org/schema/context" |
04 | xmlns:jee = "http://www.springframework.org/schema/jee" xmlns:tx = "http://www.springframework.org/schema/tx" |
05 | xmlns:task = "http://www.springframework.org/schema/task" |
06 | xsi:schemaLocation=" |
07 | http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd |
08 | http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
09 | http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd |
10 | http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-3.0.xsd |
11 | http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd |
12 | http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.0.xsd"> |
13 |
14 | < context:component-scan base-package = "com.javacodegeeks.aspectjspring" /> |
15 | |
16 | < bean class = "com.javacodegeeks.aspectjspring.aspects.GreetingAspect" factory-method = "aspectOf" > |
17 | < property name = "message" value = "Hello from Greeting Aspect" /> |
18 |
bean > |
19 |
20 |
beans > |
- Change the base-package attribute of the context:component-scan element to whatever is the base package of your project so as to be scanned for Spring components
- We have to define our aspects in “applicationContext.xml” only if we want to inject dependences to them
- AspectJ denotes the term of “aspect association”. It defines how to manage aspect state. The following state associations are supported:
- Per JVM – one shared aspect instance is constructed and used (default)
- Per object – aspect has its own state per every advised object
- Per control flow – aspect has its own state per particular control flow
All AspectJ aspect classes have “hasAspect()” and “aspectOf()” static methods. These methods are implicitly generated by AspectJ compiler/load time weaver. So, for the default aspect state there is a single aspect instance that can be retrieved using the “aspectOf()” method
01 | package com.javacodegeeks.aspectjspring.services; |
02 |
03 | import org.springframework.stereotype.Service; |
04 |
05 | @Service ( "greetingService" ) |
06 | public class GreetingService { |
07 |
08 | public String sayHello() { |
09 | return "Hello from Greeting Service" ; |
10 | } |
11 | |
12 | } |
No comments:
Post a Comment