суббота, 4 января 2014 г.

Spring 3.2.6 + AspectJ 1.7.4 + LTW + WebSphere Application Server v8.5.5.1

Hello!
Happy New Year 2014!

Today I want to prove the work of AspectJ LTW in WebSphere Application Server v8.5.5.1.



Here are my tools and libs:
1) Eclipse for JEE developers kepler 4.3 SR1
2) AJDT 2.2.3
3) AspectJ 1.7.4
4) WebSphere Application Server for developers (ILAN) v8.5.5.1
5) WebSphere Application Server Developer Tools for Eclipse
6) Spring 3.2.6
7) Linux Mint 16 64 bit

Here are steps:
1) Install WebSphere Application Server through the installation manager
2) Create the appserver server1.

If the installation fail, then try these commands:
cd /bin
sudo rm sh
sudo ln -s bash sh

 and try install again.

3) Install the WebSphere Application Server Developer Tools for Eclipse. 
Add the created server1 on eclipse server view. 

Note: the eclipse user must have rights on starting and stopping server1.

4) In eclipse create Dynamic Web Project v2.5.
5) In lib folder copy the following jars:
aspectjrt.jar                     spring-context-support-3.2.6.RELEASE.jar
aspectjweaver.jar                 spring-core-3.2.6.RELEASE.jar
spring-aop-3.2.6.RELEASE.jar      spring-expression-3.2.6.RELEASE.jar
spring-beans-3.2.6.RELEASE.jar    spring-instrument-3.2.6.RELEASE.jar
spring-context-3.2.6.RELEASE.jar  spring-web-3.2.6.RELEASE.jar
 

6) In a typical application you would have aspects, services and servlets.
Let's create the structure of an app.
Create packages: ru.chernykh.aspects,
ru.chernykh.services,
ru.chernykh.servlets.

7) Create class MyService.
package ru.chernykh.services;

public class MyService {
    public MyService() {
        System.out.println("Constructor service");
    }

    public void executeService() {
        System.out.println("Service executed");
    }


8) Create servlet MyServlet.
package ru.chernykh.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.chernykh.services.MyService;

public class Myservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Myservlet() {
        super();
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
    }

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        forwardRequest(request, response);
    }

    private void forwardRequest(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        System.out.println("Hello from servlet");
        MyService service = new MyService();
        service.executeService();

        RequestDispatcher rd = request
                .getRequestDispatcher("/WEB-INF/jsp/index.jsp");
        rd.forward(request, response);
    }
}
 

9) Create aspect TraceAspect.
package ru.chernykh.aspects;

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

@Aspect
public class TraceAspect {
    @Pointcut("!within(TraceAspect) && execution(* *(..))")
    public void nothere() {
    }

    @Before("nothere()")
    public void before(JoinPoint jp) {
        System.out.println("Entering " + jp.toString());
    }
}


10) web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
    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-app_2_5.xsd">
    <display-name>SpringAspectLTW</display-name>
    <servlet>
        <description>
        </description>
        <display-name>Myservlet</display-name>
        <servlet-name>Myservlet</servlet-name>
        <servlet-class>ru.chernykh.servlets.Myservlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Myservlet</servlet-name>
        <url-pattern>
        /Myservlet</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>


11) applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

<!-- this switches on the load-time weaving -->
 <context:load-time-weaver
            weaver-class="org.springframework.instrument.classloading.websphere.WebSphereLoadTimeWeaver"/>

</beans>


Here I explicitly defined the weaver.

12) in META-INF create text file aop.xml:
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE aspectj PUBLIC
        "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>

    <weaver options="-showWeaveInfo -verbose -debug">

        <!-- only weave classes in our application-specific packages -->
        <include within="ru.chernykh.servlets.*" />
        <include within="ru.chernykh.services.*" />
        <include within="ru.chernykh.aspects.*" />
    </weaver>

    <aspects>
        <!-- weave in just this aspect -->
        <aspect name="ru.chernykh.aspects.TraceAspect" />
    </aspects>

</aspectj>


Please note, that aspects themselves must be weaved.  That's why I have the line:
<include within="ru.chernykh.aspects.*" />.

13) In WEB-INF/jsp create index.jsp with the following:
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>My test page</title>
</head>
<body>
If you see this, then it is time to see log.
</body>
</html>


14) Also, in order to deploy the project create the EAR and add the reference to project.

15) Now deploy the ear to WebSphere and run the servlet:
http://localhost:9080/SpringAspectLTW/Myservlet,
then look the console view:
[04.01.14 18:51:31:872 MSK] 000000ba ServletWrappe I com.ibm.ws.webcontainer.servlet.ServletWrapper init SRVE0242I: [SpringAspectLTWEAR] [/SpringAspectLTW] [Myservlet]: Initialization successful.
[04.01.14 18:51:31:873 MSK] 000000ba SystemOut     O Hello from servlet
[04.01.14 18:51:31:988 MSK] 000000ba SystemOut     O Constructor service
[04.01.14 18:51:31:997 MSK] 000000ba SystemOut     O Entering execution(void ru.chernykh.services.MyService.executeService())
[04.01.14 18:51:31:997 MSK] 000000ba SystemOut     O Service executed
[04.01.14 18:51:32:007 MSK] 000000ba ServletWrappe I com.ibm.ws.webcontainer.servlet.ServletWrapper init SRVE0242I: [SpringAspectLTWEAR] [/SpringAspectLTW] [/WEB-INF/jsp/index.jsp]: Initialization successful.
 


Please, note that servlet execution is not intercepted by the aspect. Only calls within servlet itself are intercepted. That's all!
Feel free to ask the questions about the article.

Комментариев нет:

Отправить комментарий