spring questions word document

Upload: swamymergu

Post on 07-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Spring Questions Word Document

    1/31

    Using Springs AOP Features with Java EE

    April 18, 2011Deepak VohraThe popular Spring framework complements and facilitates development on the Java EE platform byproviding a modular, object-based programming model. Whereas Java EE is OOP (object-orientedprogramming) based, Spring is AOP (aspect-oriented programming) based; in fact the AOP framework is

    one of the main components of Spring. AOP complements OOP by providing an aspect as the unit ofmodularity in addition to the traditional OOP class. As explained in the Spring doc, Aspects enablemodularization of concerns such as transaction management that cut across multiple types and objects.A Spring bean may be a JavaBean, a POJO, or any other object such as an EJB. Spring implements theInversion of Control (IoC) principle via dependency injection. The Spring configuration file is used tospecify the configuration of the Spring beans with the Spring container and the BeanFactory is used tomanage the Spring beans. The BeanFactory configures, instantiates, and assembles the dependenciesamong the objects.

    In this article we shall explore the main AOP-related features in Spring: method interception and theSpring AOP framework.

    SetupFirst you will need to download and install the following:

    y Oracle Enterprise Pack for Eclipse (OEPE)y Oracle WebLogic Servery aspectj-1.6.10.jar , cglib-2.2.jar, and asm-3.3.jarAdd the following JAR files to the CLASSPATH variable inC:\Oracle\Middleware\user_projects\domains\base_domain\bin\startWebLogic batch script. (The SpringJAR files can be added after the Spring Framework Library has been added to the Spring Web project asdiscussed in a later section.)

    C:\Spring\lib\aspectjweaver.jar

    C:\Spring\lib\aspectjtools.jar

    C:\Spring\lib\aspectjrt.jar

    C:\Spring\lib\org.aspectj.matcher.jar

    C:\Spring\asm-3.3.jar

    C:\Spring\cglib-2.2.jar

    C:\Spring\aspectj-1.6.10.jar

    C:\Users\foo\workspace\libraries\Spring Framework 2.5.6\spring-framework-2.5.6\dist\spring.jar

    C:\Users\foo\workspace\libraries\Spring Framework 2.5.6\spring-framework-2.5.6\dist\lib\jakarta-commons\commons-logging.jar

  • 8/6/2019 Spring Questions Word Document

    2/31

  • 8/6/2019 Spring Questions Word Document

    3/31

    Right-click on the project node and select Properties. Select the Java Build Path node - the spring.jarshould be in the Java Build Path. Now we're ready to explore these features.

    Method InterceptionIn this section we shall discuss the method interceptor mechanism provided by Spring.

    Creating a Spring Bean ClassFirst, we need to create a Spring bean, which is an object managed, instantiated, and configured bythe framework. We shall create a JavaBean object for the Spring bean; this object shall implement amagazine catalog with properties representing the catalogs journal name, publisher, edition, title,author. The bean class shall also provide accessor methods for the bean properties.

    We need to create an interface, CatalogInterface, for the bean class.

    1. Select the project node and select File>New.2. In the New wizard select Java>Interface and click on Next.3. Specify a package name and a class name and click on Finish. A spring.catalog.CatalogInterface

    interface gets added the src folder. The interface CatalogInterface.java is listed below.

    package spring.catalog;public interface CatalogInterface {

    public void setJournal(String journal);public String getJournal();public void setPublisher(String publisher);public String getPublisher();public void setEdition(String edition);public String getEdition();public void setTitle(String title);public String getTitle();public void setAuthor(String author);public String getAuthor();

    }Next, add the bean class Catalog.java, which implements the interface.

    1. Select Java>Class in New and click on Next.2. In New Java Class, specify the package name and the class name and select the

    interface spring.catalog.CatalogInterface. Click onFinish. The spring.catalog.Catalog.java classgets added to the Web project.

    In the Spring bean class, provide implementation for the accessor methods for the bean properties.Also, add a test method getTestMessage() that returns a String message. The Catalog.java class is listedbelow.

    package spring.catalog;

    public class Catalog implements CatalogInterface {

    public String journal;public String publisher;public String edition;public String title;public String author;

  • 8/6/2019 Spring Questions Word Document

    4/31

    public Catalog(){}

    public Catalog(String journal, String publisher, String edition,String title, String author) {

    this.journal = journal;this.publisher = publisher;

    this.edition = edition;this.title = title;this.author = author;

    }

    public String getAuthor() {// TODO Auto-generated method stubreturn author;

    }

    public String getEdition() {// TODO Auto-generated method stubreturn edition;

    }

    public String getJournal() {// TODO Auto-generated method stubreturn journal;

    }

    public String getPublisher() {// TODO Auto-generated method stubreturn publisher;

    }

    public String getTitle() {// TODO Auto-generated method stub

    return title;}

    public void setAuthor(String author) {// TODO Auto-generated method stubthis.author = author;

    }

    public void setEdition(String edition) {// TODO Auto-generated method stubthis.edition = edition;

    }

    public void setJournal(String journal) {// TODO Auto-generated method stubthis.journal = journal;

    }

    public void setPublisher(String publisher) {// TODO Auto-generated method stubthis.publisher = publisher;

    }

  • 8/6/2019 Spring Questions Word Document

    5/31

    public void setTitle(String title) {// TODO Auto-generated method stubthis.title = title;

    }

    public String getTestMessage() {return "Spring Bean Test";

    }

    }Creating a Bean Definition FileIn this section we shall create a Spring bean definition file, beanDefinition.xml, to configure the Springbean, the Catalog JavaBean, with the Spring framework. The root element of the beanDefinition.xmlis beans and each of the bean sub-elements represents a Spring bean. We shall register three beanswith the bean configuration file: Catalog JavaBean, Interceptor bean, and Proxy bean.

    To create a bean configuration file:

    1. Select the project node in the Project Explorer and select File>New.2. In New select Spring>Spring Bean Configuration and click on Next.3. In New Spring Bean Definition File select the project folder and specify File name

    beanDefinition.xml; click on Next.4. In Select XSD namespaces select spring-beans-2.5.xsd and click on Next.5. Click on Finish. A Spring bean definition file beanDefinition.xml gets created.For each of the beans to be registered with the Spring framework we need to add a element.Add a < bracket within the element and in the pop-up select .

    Figure 2

  • 8/6/2019 Spring Questions Word Document

    6/31

    A element gets added. Next, add an id attribute to the element. Select theDesign tab, right-click on bean,and select Add Attribute>id.

    Figure 3An id attribute gets added to the bean element. Specify the id attribute value as spring-bean.Similarly, add a class attribute to the bean element to specify the JavaBean classspring.catalog.Catalog. Also add bean elements for an interceptor bean and a proxy bean. Thefollowing table lists details about the beans you should add.

    bean id class

    CatalogJavaBean

    spring-bean spring.catalog.Catalog

    Interceptor

    beaninterceptor spring.catalog.CatalogInterceptor

    Proxy bean proxyBean org.springframework.aop.framework.ProxyFactoryBean

    The bean definition file with the beans added is shown in the Design view.

  • 8/6/2019 Spring Questions Word Document

    7/31

    Figure 4Next, we shall add the following properties to the bean with id proxyBean.

    Property Value Description

    proxyInterfaces spring.catalog.CatalogInterfaceSet of interfaces being

    proxied

    interceptorNames interceptor

    List of beans used as

    interceptor beans for the

    proxied bean

    target spring.catalogId of the bean to be

    proxied

    proxyTargetClass trueCreates class based

    proxies

    To add a property:

    Right-click on the bean with id proxyBean and select Add child>property.

  • 8/6/2019 Spring Questions Word Document

    8/31

    Figure 5To add a value right-click on property and select Add child>list. To add a element tothe element right-click onlist and select Add child>value. For the interceptorName property addlist value interceptor.

    The target property requires a ref sub-element. Right-click on property and select Add child>ref. Tothe ref element we need to add the local attribute. Right-click on refand select Add Attribute>local.Similarly add the proxyTargetClass property and set its value to true. The proxyBean with properties isshown below.

  • 8/6/2019 Spring Questions Word Document

    9/31

    Fig 6To the bean for JavaBean Catalog add constructor-arg sub-elements for arguments to the constructorfor the JavaBean. The constructor has parameters for journal, publisher, edition, title and author.

    >

    The beans property values may also be specified as property/value.

    Oracle Magazine

    Oracle Publishing

    November-December 2010

    Agile Enterprise Architecture

    Bob Rhubart

    The bean definition file is listed below.

  • 8/6/2019 Spring Questions Word Document

    10/31

    spring.catalog.CatalogInterface

    interceptor

    true

    Creating a Method InterceptorThe org.aopalliance.intercept.MethodInterceptor interface is used to intercept calls on an interfaceprior to the target. The interceptor bean class should implement the interface and implement theinvoke(MethodInvocation) method of the interface to modify the behavior of a method.

    Create an interceptor Java class CatalogInterceptor that implements the MethodInterceptor interfacein the spring.catalog package. CatalogInterceptor.java get added to the Spring project. In the invoke()method output the name of the intercepted method and the name of the class containing theintercepted method. The CatalogInterceptor method is listed below.

    package spring.catalog;

  • 8/6/2019 Spring Questions Word Document

    11/31

    import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;

    public class CatalogInterceptor implements MethodInterceptor {

    @Overridepublic Object invoke(MethodInvocation inv) throws Throwable {

    System.out.println("Intercepted method - "+ inv.getMethod().getDeclaringClass()

    + " - " + inv.getMethod().getName());

    return null;}}Creating a Spring Client

    In this section we shall create a Java client for the Spring bean and invoke the bean methods. We shallinstantiate the Spring bean that represents the JavaBean spring.catalog.Catalog and invoke its method.Subsequently, we shall instantiate the proxy bean proxyBean, which is configured with an interceptor.We shall invoke the methods of the target class to demonstrate that the method interceptor interceptsthe method calls and outputs the method name and the name of the containing class.

    First, create a Spring client class SpringClient in the same package as the JavaBean and the interceptorclass. Add external JARs asm-3.3.jar and cglib-2.2.jar to the Java Build Path.

    We shall create two versions of the SpringClient.java class. In the first version we shall instantiate theJavaBean Catalog and invoke its methods to demonstrate that the interceptor is not invoked.Instantiate the Catalog bean using the elements or the property/value inbeanDefinition.xml as discussed earlier. Add a method loadContext() to instantiate the Springcontainer, which is essentially creating an ApplicationContext.

    private void loadContext() {String filename = "beanDefinition.xml";context = new FileSystemXmlApplicationContext(filename);

    }In the main method create an instance of the SpringClient class and invoke the loadContext method.

    SpringClient client = new SpringClient();client.loadContext();Instantiate the JavaBean with id spring.catalog using the getBean method.

    Catalog catalog = (Catalog) context.getBean("spring.catalog");Output the bean properties values.

    System.out.println(catalog.journal);System.out.println(catalog.publisher);

  • 8/6/2019 Spring Questions Word Document

    12/31

    System.out.println(catalog.edition);System.out.println(catalog.title);System.out.println(catalog.author);Invoke the getTestMessage() and output the message returned.

    String test = catalog.getTestMessage();System.out.println(test);

    The SpringClient.java class is shown below.

    package spring.catalog;

    import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;

    public class SpringClient {

    private static ApplicationContext context;

    /*** Load up the Spring container*/

    private void loadContext() {String filename = "beanDefinition.xml";context = new FileSystemXmlApplicationContext(filename);

    }

    public static void main(String[] args) {

    SpringClient client = new SpringClient();client.loadContext();//getBean spring.catalog Interceptor method not invoked.

    Catalog catalog = (Catalog) context.getBean("spring.catalog");

    System.out.println(catalog.journal);System.out.println(catalog.publisher);System.out.println(catalog.edition);System.out.println(catalog.title);System.out.println(catalog.author);

    String test = catalog.getTestMessage();System.out.println(test);

    }}Right-click on the SpringClient.java application and select Run As>Java Application.

    As the Catalog JavaBean is not configured with a method interceptor the method calls do not getintercepted and the values returned by the accessor methods get output.

  • 8/6/2019 Spring Questions Word Document

    13/31

    Figure 7We could also have used a XmlBeanFactory method to instantiate the Spring bean, and also used settermethods to set the bean propertiess values instead of instantiating the bean in thebeanDefinition.xml. The output would be the same, but would include the message Loading XML beandefinitions from class path resource (beanDefinition.xml).

    In the second version of the SpringClient application we shall instantiate the proxy bean that isconfigured with the interceptor method. We shall reference the proxy through the Spring container.Create a XmlBeanFactory object.

    XmlBeanFactory beanFactory = new XmlBeanFactory(newClassPathResource("beanDefinition.xml"));Instantiate the proxy bean with the getBean method.

    Catalog catalog = (Catalog) beanFactory.getBean("proxyBean");Invoke the getTestMessage method and output the value returned.

    String test = catalog.getTestMessage();System.out.println(test);Invoke the setter methods to set the values of the bean properties.

    catalog.setJournal("Oracle Magazine");catalog.setPublisher("Oracle Publishing");catalog.setEdition("November-December 2010");

  • 8/6/2019 Spring Questions Word Document

    14/31

    catalog.setTitle("Agile Enterprise Architecture");catalog.setAuthor("Bob Rhubart");Output the bean properties.

    System.out.println(catalog.journal);

    System.out.println(catalog.publisher);System.out.println(catalog.edition);System.out.println(catalog.title);System.out.println(catalog.author);The SpringClient.java is listed below.

    package spring.catalog;

    import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;

    public class SpringClient {

    public static void main(String[] args) {

    // reference the proxy through the Spring container//getBean proxyBean Interceptor method invoked.

    /** * Load up the Spring container */XmlBeanFactory beanFactory = new XmlBeanFactory(newClassPathResource("beanDefinition.xml"));

    //getBean proxyBean Interceptor method invoked.

    Catalog catalog = (Catalog) beanFactory.getBean("proxyBean");

    String test = catalog.getTestMessage();System.out.println(test);

    catalog.setJournal("Oracle Magazine");catalog.setPublisher("Oracle Publishing");catalog.setEdition("November-December 2010");catalog.setTitle("Agile Enterprise Architecture");catalog.setAuthor("Bob Rhubart");System.out.println(catalog.journal);System.out.println(catalog.publisher);System.out.println(catalog.edition);System.out.println(catalog.title);

    System.out.println(catalog.author);}

    }Right-click on SpringClient.java and select Run As>Java Application. As the proxy bean is configuredwith the interceptor method, the method invocations get intercepted. The invoke method of theCatalogInterceptor class gets invoked. The method name and the name of the containing class are theoutput. The bean properties values are not in the output as the method invocations are intercepted.

  • 8/6/2019 Spring Questions Word Document

    15/31

    Figure 8.aop NamespaceSpring implements AOP using a spring-aop schema and the aop namespace. Alternatively, AOP may beimplemented using the @Aspect annotation.

    In this section we shall use the aop namespace to implement AOP. We will develop a Web applicationconsisting of a JSF page for user input, and applicationContext.xml to create a BeanFactory and anApplicationContext. Some of the aop namespace tags are discussed in the following table.

    Tag Description

    aop:aspectj-autoproxyEnables @AspectJ aspects in Spring withautoproxying beans.

    aop:configAll aspects and advisors must be declared

    within aop:config

    aop:pointcut Declares a pointcut

    aop:aspect Declares an aspect

  • 8/6/2019 Spring Questions Word Document

    16/31

    aop:after-returningDeclares advice that runs after matched

    method execution completes normally

    aop:after-throwing

    Declares advice that runs after matched

    method execution exits by throwing an

    exception

    aop:advisor Declares an advisor

    aop:beforeDeclares advice that runs before matched

    method execution

    aop:afterDeclares advice that runs no matter how the

    matched method execution exits

    Creating a Spring & JSF Faceted Web ProjectNow we will create a Web project, SpringJSF, for the Spring AOP similar to the Spring project that wecreated for the method interceptor example.

    1. Select Web>Dynamic Web Project in New wizard.2. In New Dynamic Web Project specify a Project name, SpringJSF, and configure a target runtime

    for Oracle WebLogic Server 11gR1 Patch Set 2.3. Select the Default Configuration and click on Next.4. Accept the default Java settings (source folder as src and output folder as build/classes). Click

    on Next.

    5.

    Specify a Context Root (SpringJSF) and a Content directory, select the checkboxG

    enerateweb.xml deployment descriptor, and click on Finish. A Web project for Spring AOP gets created.6. Now right-click on SpringJSF in the Project Explorer and select Properties.7. Select Project Facets. Configure the Spring facet as discussed previously. Also select the JSF 1.2

    facet.8. Click on Further configuration required.9. In JSF Capabilites select JSF Implementation Library Type as User Library and click on

    the Download library... button.10.Select the JSF 1.2 (Mojarra JSF API Implementation) and click on Next.11.Accept the terms of the license and click on Finish. The JSF 1.2 library gets added to the project.12.Click on Next. In the Spring configuration select the checkbox Create a Spring bean definition

    file and select the default fileapplicationContext.xml.13.Click on OK.14. In Properties>Project Facets click on Apply to install the project facets JSF 1.2 and Spring 2.5.15.Click on OKto configure the target runtimes with the project facet libraries. A Spring and JSF

    faceted Web project gets created. The JSF and Spring libraries are also shown added to theproject.

  • 8/6/2019 Spring Questions Word Document

    17/31

    Figure 9.The web.xml file gets updated with a contextConfigLocation context parameter that specifies thelocation of the applicationContext.xml file. Also, a listener classorg.springframework.web.context.ContextLoaderListener gets configured. The listener class is thebootstrap listener to start up Springs root org.springframework.web.context.WebApplicationContext.The Spring AOP framework requires the AspectJ library in the classpath. Add the AspectJ library anddependencies shown below to the Java Build path.

  • 8/6/2019 Spring Questions Word Document

    18/31

    Figure 10.Creating a Bean ClassIn this section we shall create a JavaBean that we shall configure as a Spring bean and use in a pointcut

    expression.

    Create a Java class Catalog.java by selecting Java>Class in New wizard. To Catalog.java JavaBean addproperties for journal, publisher, edition, title, and author. Also, add accessor methods for theJavaBean properties. The getter accessor methods should return the initial values for the beanproperties. The Catalog JavaBeans properties may alternatively be instantiated using property/valueor constructor-arg in the applicationContext.xml. The JavaBean class Catalog.java is shown below.

    package spring.catalog;

    public class Catalog {

    public String journal;public String publisher;public String edition;public String title;public String author;

    public Catalog() {}

  • 8/6/2019 Spring Questions Word Document

    19/31

    public Catalog(String journal, String publisher, String edition,String title, String author) {

    this.journal = journal;this.publisher = publisher;this.edition = edition;this.title = title;

    this.author = author;}

    public java.lang.StringBuffer getAuthor() {return new StringBuffer("Bob Rhubart");

    }

    public java.lang.StringBuffer getEdition() {return new StringBuffer("November-December 2010");

    }

    public java.lang.StringBuffer getJournal() {return new StringBuffer("Oracle Magazine");

    }

    public java.lang.StringBuffer getPublisher() {return new StringBuffer("Oracle Publishing");

    }

    public java.lang.StringBuffer getTitle() {return new StringBuffer("Agile Enterprise Architecture");

    }

    public void setAuthor(String author) {this.author = author;

    }

    public void setEdition(String edition) {this.edition = edition;

    }

    public void setJournal(String journal) {this.journal = journal;

    }

    public void setPublisher(String publisher) {this.publisher = publisher;

    }

    public void setTitle(String title) {

    this.title = title;}}Creating a AOP JavaBeanIn this section we shall create a JavaBean that we will configure with the Spring framework inapplicationContext.xml as a Spring AOP aspect. Create a Java class CatalogAOP.java in the samepackage as the Catalog.java JavaBean. Thepointcut expression is used to matchjoin points andthe advice is the action taken by the aspect. The aspects pointcut expression and advice type areconfigured in the applicationContext.xml, in the next section. We shall be configuring aop:after-

  • 8/6/2019 Spring Questions Word Document

    20/31

    returning advice for each of the getter accessor methods in the Spring bean Catalog that return valuesfor bean properties as SpringBuffer.

    In the advice we shall invoke the setter methods of the Spring aspect bean CatalogAOP.java with thevalues returned by the getter methods of the Catalog JavaBean as arguments. The Spring AOPframework shall invoke the setter methods of the Spring aspect bean CatalogAOP dynamically after

    returning from the getter methods of the Spring bean Catalog.

    In the setter methods in the aspect bean we shall first output the bean property values returned by theCatalog bean prepended by the method name and subsequently modify the bean property values byadding labels to precede the bean properties. For example, the setTitle(String title) method modifiesthe Catalog JavaBeans title property by prepending it with "Title: ".

    public void setTitle(StringBuffer title) {System.out.println("setTitle method invoked" + title);title.insert(0, "Title: ");

    }

    The CatalogAOP.java Spring AOP aspect is shown below.

    package spring.catalog;

    public class CatalogAOP {

    public String journal;public String publisher;public String edition;public String title;public String author;

    public CatalogAOP() {}

    public CatalogAOP(String journal, String publisher, String edition,String title, String author) {

    this.journal = journal;this.publisher = publisher;this.edition = edition;this.title = title;this.author = author;

    }

    public String getAuthor() {return author;

    }

    public String getEdition() {return edition;

    }

    public String getJournal() {return journal;

  • 8/6/2019 Spring Questions Word Document

    21/31

    }

    public String getPublisher() {return publisher;

    }

    public String getTitle() {

    return title;}

    public void setEdition(StringBuffer edition) {System.out.println("setEdition method invoked" + edition);//edition.insert(0, "Edition: ");

    }

    public void setJournal(StringBuffer journal) {System.out.println("setJournal method invoked" + journal);//journal.insert(0, "Journal: ");

    }

    public void setPublisher(StringBuffer publisher) {System.out.println("setPublisher method invoked" + publisher);// publisher.insert(0, "Publisher: ");}

    public void setTitle(StringBuffer title) {System.out.println("setTitle method invoked" + title);

    // title.insert(0, "Title: ");}

    public void setAuthor(StringBuffer author) {System.out.println("setAuthor method invoked" + author);//author.insert(0, "Author: ");

    }

    }Creating a Bean Definition FileThe Spring AOP including the pointcuts, aspects, and advices are configured in the Spring configurationfile applicationContext.xml. For using the Spring AOP framework we need to enable AspectJ support.

    First, configure a Spring bean for the CatalogAOP JavaBean, which we shall subsequently configure asan aspect. To enable the AspectJ support add the aop:aspectj-autoproxy element. Add the

  • 8/6/2019 Spring Questions Word Document

    22/31

    Figure 11.All aspects and advisors are declared within the aop:config element. Add the aop:config element to theSpring configuration file. Add a

  • 8/6/2019 Spring Questions Word Document

    23/31

    If the target object does not use any interfaces Spring AOP uses CGLIB to create the proxy for thetarget object. To specify the use of CGLIB set the proxy-target-class attribute of aop:config to true.Right-click on aop:config and select Add Attribute>proxy-target-class.

    Figure 13.Next, add a pointcut defintion to the aop:config element. A pointcut is used to match join points; ajoin point in Spring AOP is a method execution.

    A pointcut definition within a aop:config may be used for several aspects and advisors. We shall use thebean pointcut designator (PCD) to specify the Catalog Spring bean for matching join points. Right-click on aop:config and select Add Child>pointcut.

  • 8/6/2019 Spring Questions Word Document

    24/31

    Figure 14.Specify the value of the expression attribute as bean(catalogBean). Next, add an aspect. Right-clickon aop:config and select Add Child>aspect.

    Figure 15.

  • 8/6/2019 Spring Questions Word Document

    25/31

    To the aop:aspect element add a ref attribute to refer to the Spring bean catalogBeanAOP, which isthe aspect Spring bean. Right-click on aop:aspect and select Add Attribute>ref. The Spring beancatalogBeanAOP gets configured as an aspect.

    Figure 16.Next, we shall configure after-returning advice for each of the getter methods of Spring beancatalogBean, which is the JavaBean Catalog. Right-click on aop:aspect and select Add Child>after-returning. After returning advice is the advice to be run when a method returns without throwing an

    exception.

  • 8/6/2019 Spring Questions Word Document

    26/31

    Figure 17.To the after-returning element add the attributes explained in the following table, which applies forone join point.

    Attribute Value Description

    pointcutexecution(java.lang.StringBuffer

    getJournal(..))

    Specifies the pointcutexpression to match a join

    point, which is the method

    call to getJournal thatreturns a StringBuffer

    method setJournalThe method of the aspect

    to invoke

    returning journal

    The advice methodparameter to which the

    return value of thegetJournal method is

    passed as an argument. The

    type of the returned value

    should match the parameter

    type.

  • 8/6/2019 Spring Questions Word Document

    27/31

    arg-names journalSpecifies argument names

    for the advice method

    Similarly, add after-returning advice for other join points. The Spring configuration file with after-returning advice for all the join points is listed below.

  • 8/6/2019 Spring Questions Word Document

    28/31

    method="setEdition" returning="edition" arg-names="edition" />

    Creating a JSF Page

    In this section we shall create a JSF page to output the Spring bean catalogBean (Catalog.javaJavaBean) properties. We shall demonstrate the effect of the aspect Spring bean catalogAOPBean onthe catalogBeans propeties values.

    1. Select File>New. In New wizard select Web>JSP and click on Next.2. Select the SpringJSF/WebContent folder and specify File name as catalog.jsp and click on Next.3. Click on Finish. A JSP catalog.jsp get added to the SpringJSF project.In the catalog.jsp output the values for the catalogBean properties using the h:outputText element.Catalog.jsp is listed below.

    Catalog

  • 8/6/2019 Spring Questions Word Document

    29/31

    To the faces-config.xml add the following application element so that EL expressions also recognizeSpring beans as managed beans.

    org.springframework.web.jsf.el.SpringBeanFacesELResolverRunning the JSF PageIn this section we shall run the catalog.jsp to demonstrate the effect of the aspect on the Spring beancatalogBeans properties. In the first run comment out the modifications, which prepend labels to theSpring beans property values in the CatalogAOP JavaBeans setter methods.

    //edition.insert(0, "Edition: ");//journal.insert(0, "Journal: ");

    //publisher.insert(0, "Publisher: ");//title.insert(0, "Title: ");//author.insert(0, "Author: ");Right-click on catalog.jsp and select Run As>Run on Server. Start the WebLogic server if not alreadystarted and click on Finish. The SpringJSF application gets deployed to the WebLogic server as an auto-generated EAR file. The server output shows that the setter methods of the Spring AOP aspect bean getinvoked and the Spring bean catalogBeans property values are the output.

    Figure 18.Run the catalog.jsp with url http://localhost:7001/SpringJSF/faces/catalog.jsp . The Spring beancatalogBeans property values are in the output.

  • 8/6/2019 Spring Questions Word Document

    30/31

    Figure 19.In the second run uncomment the modifications to the catalogBeans property values in the aspectSpring bean catalogAOPBean; modifications that prepend labels. The aspect gets applied and thecatalogBeans property values get prepened with labels, as shown in the output.

  • 8/6/2019 Spring Questions Word Document

    31/31

    Figure 20.