Home  >  Spring Core

StopWatch Spring Example

By Arvind Rai, August 14, 2013
org.springframework.util.StopWatch works like its name. Spring has provided a StopWatch to verify performance while development spring application. It is not intended for production application. StopWatch is not thread safe. StopWatch.start() starts the time and can be stopped by StopWatch.stop(). Find the example to understand how to use.

UserAspect.java
package com.concretepage;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.util.StopWatch;
@Aspect
public class UserAspect {
	@Around("execution(* com.concretepage.bean.UserService.doTask(..))")
	public void userAdvice(ProceedingJoinPoint joinPoint) throws Throwable{
		StopWatch stwatch = new StopWatch(getClass().getSimpleName());
		try{
			stwatch.start(joinPoint.getSignature().getName());
			joinPoint.proceed();
		}finally{
			stwatch.stop();
			System.out.println(stwatch.prettyPrint());
		}
	}
}
 


AspectConfig.java
package com.concretepage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import com.concretepage.bean.UserService;
@Configuration
@EnableAspectJAutoProxy
public class AspectConfig {
  @Bean
  public UserService userService(){
	 return new UserService();
  }
  @Bean	
  public UserAspect userAspect(){
	 return new UserAspect();
  }
}
 


UserService.java
package com.concretepage.bean;
public class UserService {
	public void doTask()  {
		System.out.println("do some task.");
	}
}
 


pom.xml
<dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>3.1.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
      <optional>true</optional>
    </dependency>
    
    <dependency>
	  <groupId>cglib</groupId>
      <artifactId>cglib-nodep</artifactId>
	  <version>2.1_3</version>
	  <optional>true</optional>
    </dependency>
    
    <dependency>
	 <groupId>org.hibernate</groupId>
     <artifactId>hibernate-annotations</artifactId>
     <version>3.5.2-Final</version>
     <optional>true</optional>
    </dependency>
    
    <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-core</artifactId>
     <version>3.6.0.Final</version>
     <optional>true</optional>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-jdbc</artifactId>
     <version>2.0.8</version>
     <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-tx</artifactId>
	  <version>3.1.0.RELEASE</version>
	  <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp-all</artifactId>
      <version>1.3</version>
      <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-aop</artifactId>
      <version>3.1.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    	
    <dependency>
	  <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>3.1.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-core</artifactId>
     <version>3.1.0.RELEASE</version>
     <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-expression</artifactId>
     <version>3.1.0.RELEASE</version>
     <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-asm</artifactId>
	  <version>3.1.0.RELEASE</version>
	 <scope>compile</scope>
    </dependency>	
    
    <dependency>
	 <groupId>mysql</groupId>
	 <artifactId>mysql-connector-java</artifactId>
	 <version>5.1.17</version>
    </dependency>
    
    <dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
    </dependency>

  </dependencies>
 


SpringAOPTest.java
package com.concretepage;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.concretepage.bean.UserService;
public class SpringAOPTest {
   public static void main(String[] args) {
	   AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
       ctx.register(AspectConfig.class);
       ctx.refresh();
       UserService userService = ctx.getBean(UserService.class);
       userService.doTask();
   }
} 
 


Output
do some task.
StopWatch 'UserAspect': running time (millis) = 0
-----------------------------------------
ms     %     Task name
-----------------------------------------
00000  ?  doTask
 
Download Source Code
stopwatch-spring-example.zip
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us