Announcement Announcement Module
Collapse
No announcement yet.
Using JSF with Spring AOP in STS Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using JSF with Spring AOP in STS

    I want to use JSF with Spring AOP. I want my aspect execute before the function called by user is executed.

    I have stock jsf web application and files are :

    --application-config.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...ring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">



    <bean id="stockBean"
    class="stock.introduction.StockValueFetcher">
    </bean>

    <bean class="stock.introduction.AspectTest" />
    <aop:aspectj-autoproxy/>

    </beans>

    -------------------------------------------------------------------
    --faces-config.xml---
    <?xml version='1.0' encoding='UTF-8'?>

    <faces-config version="1.2"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">

    <application>
    <variable-resolver>
    org.springframework.web.jsf.DelegatingVariableReso lver
    </variable-resolver>
    </application>

    <managed-bean>
    <managed-bean-name>stockBean</managed-bean-name>
    <managed-bean-class>
    stock.introduction.StockValueFetcher
    </managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>

    <navigation-rule>
    <description>Navigation from the hello page.</description>
    <from-view-id>/stockInput.jsp</from-view-id>
    <navigation-case>
    <from-outcome>stockOutputSuccess</from-outcome>
    <to-view-id>/stockOutputSuccess.jsp</to-view-id>
    </navigation-case>
    <navigation-case>
    <from-outcome>stockOutputFailure</from-outcome>
    <to-view-id>/stockOutputFailure.jsp</to-view-id>
    </navigation-case>
    </navigation-rule>

    </faces-config>

    -------------------------------------------------------------------------
    --index.jsp---
    <%@page contentType="text/html"%>
    <%@page pageEncoding="UTF-8"%>

    <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
    <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>


    <f:view>
    <html>
    <head>
    <title>
    Stock Input Page
    </title>
    </head>
    <body>
    <h:form id="stockForm">

    <h1>
    Please Enter the Stock Symbol and click the button
    </h1>

    <p>
    <h:inputText id="stockSymbolInput" value="#{stockBean.symbolName}"
    required="true">
    </h:inputText>
    </p>

    <h:commandButton id="stockSubmit" type="submit" value="Submit Symbol"
    action="#{stockBean.findStockValue}">
    </h:commandButton>

    </h:form>

    </body>
    </html>
    </f:view>
    --------------------------------------------------------------------------
    package stock.introduction;

    import java.util.*;





    public class StockValueFetcher implements StockValueFetcherInterface {

    private Map<String, String> stockSymbolsAndValues;

    private String symbolName;
    private String symbolValue;

    public StockValueFetcher() {
    stockSymbolsAndValues = new HashMap<String, String>();
    stockSymbolsAndValues.put("ABC", "10");
    stockSymbolsAndValues.put("DEF", "20");
    stockSymbolsAndValues.put("GHI", "30");
    stockSymbolsAndValues.put("JKL", "40");
    }

    public String getSymbolName() {
    return symbolName;
    }

    public void setSymbolName(String symbolName) {
    this.symbolName = symbolName;
    }

    public String getSymbolValue() {
    return symbolValue;
    }

    public void setSymbolValue(String symbolValue) {
    this.symbolValue = symbolValue;
    }

    public String findStockValue(){
    System.out.println("HI -------------------------------------------------------------------------------------------------------------------------");
    boolean symbolFound = stockSymbolsAndValues.containsKey(symbolName);
    if (symbolFound){
    symbolValue = stockSymbolsAndValues.get(symbolName);
    return "stockOutputSuccess";
    }else{
    return "stockOutputFailure";
    }
    }


    }
    -------------------------------------------------------------------------
    package stock.introduction;

    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;

    @Aspect
    public class AspectTest {

    @Before("execution(* stock.introduction.StockValueFetcher.find*())")
    public void logInfo(){
    System.out.println(" .... in logInfo() ...");
    }

    }
    ------------------------------------------------------------------------
    package stock.introduction;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlAp plicationContext;

    public class Demo {

    /**
    * @param args
    */
    public static void main(String[] args) {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("stock/introduction/application-config.xml");
    StockValueFetcherInterface svf = (StockValueFetcherInterface) ctx.getBean("stockBean");
    //svf.setSymbolName("ABC");
    svf.findStockValue();
    }

    }
    ----------------------------------------------------------------------

    When i run above application with STS as Java/AspectJ application aspects are applied and output is :

    "log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlA pplicationContext).
    log4j:WARN Please initialize the log4j system properly.
    .... in logInfo() ...
    HI -------------------------------------------------------------------------------------------------------------------------
    -----------------------------------------------------------------------------"


    but when i run application as Web application it only prints out the following and aspects are not applied.

    "HI -------------------------------------------------------------------------------------------------------------------------"

    Can anyone help me with this?
Working...
X