1.项目中配置切面(AOP),横向监控
可以看这里:http://ntzrj513.blog.163.com/blog/static/27945612201362232315/
① 实例代码(基于注解的方式):
@Component
@Aspect
@Order(1)
public class ServiceLoggingAspect {
private final Logger logger = LoggerFactory.getLogger(ServiceLoggingAspect.class);
@Around("execution(public * com.raipeng.micro..service.*Service.*(..))")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
Object result = joinPoint.proceed();
long time = System.currentTimeMillis() - startTime;
logger.info("The method {}.{}() time consuming {}ms", joinPoint.getTarget().getClass().getSimpleName(), joinPoint.getSignature().getName(), time);
return result;
}
}
joinpoint可以获取到当前执行的方法,参数等,方法如下:
可以看这里:http://my.oschina.net/itblog/blog/211693
joinPoint.getArgs()//获取参数
joinPoint.getTarget()//获取被织入的目标对象
joinPoint.getTarget().getClass().getSimpleName()//获得被织入的类全称
joinPoint.proceed()//获得该织入对象的执行结果并返回
joinPoint.getSignature()//返回目标方法的签名
joinPont.getSignature().getName()//返回目标方法的名称
扫描包
<context:component-scan base-package="com.raipeng.micro.enterprise.**.service"/>
<context:component-scan base-package="com.raipeng.micro.core.support.aspect"/>
<aop:aspectj-autoproxy/>
2.实现ApplicationContextAware接口,覆盖其setApplicationContext方法
可以看这里:http://blog.csdn.net/kaiwii/article/details/6872642
目的:随意获取到spring容器中的bean
具体代码:
package com.raipeng.micro.core.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* Created on 2015/7/16.
*
* @author
* @version 1.0
*/
public class AppUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
AppUtils.applicationContext = applicationContext;
}
public static Object getBean(String name) {
return applicationContext.getBean(name);
}
public static <T> T getBean(String name, Class<T> clazz) {
return applicationContext.getBean(name, clazz);
}
}
配置:
web.xml
//spring自己的监听器,用于加载自己本身所需的配置文件
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
//指定配置文件(不配置的话读取默认路径下的applicationContext.xml文件)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
applicationContext.xml
注册用于获取bean的类(spring-mvc配置文件中配置可以)
<bean class="com.raipeng.micro.core.utils.AppUtils"/>
3.实现applicationListener接口,覆盖其onApplicationEvent(ContextRefreshedEvent event)方法
可以看这里:http://yangyongbyjava.iteye.com/blog/1600581
加载两次的问题:http://zhaoshijie.iteye.com/blog/1974682 或者加个boolean的参数,执行完后变为false/true
实例代码:配置静态资源和动态资源的默认路径
package com.raipeng.micro.core.web.listener;
import com.raipeng.micro.core.web.conf.WebConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
/**
* Created on 2015/6/16.
*
* @author
* @version 1.0
*/
@Component
public class WebApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
private final Logger logger = LoggerFactory.getLogger(WebApplicationListener.class);
private WebConfig webConfig;
@Resource
public void setWebConfig(WebConfig webConfig) {
this.webConfig = webConfig;
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext applicationContext = event.getApplicationContext();
WebApplicationContext webApplicationContext = (WebApplicationContext) applicationContext;
ServletContext servletContext = webApplicationContext.getServletContext();
webConfig.setContext(servletContext.getContextPath());
webConfig.setResource(webConfig.getContext() + "/public");
servletContext.setAttribute("webConfig", webConfig);
servletContext.setAttribute("context", webConfig.getContext());
servletContext.setAttribute("resource", webConfig.getResource());
servletContext.setAttribute("revision", webConfig.getRevision());
logger.debug("context:{}, resource:{}, revision:{}", webConfig.getContext(), webConfig.getResource(), webConfig.getRevision());
}
WebConfig.java
package com.raipeng.micro.core.web.conf;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* Created on 2015/6/16.
*
* @author
* @version 1.0
*/
@Component
public class WebConfig {
private String context;//WEB应用上下文
private String resource;//静态文件
private String revision;//CSS、js版本号
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public String getResource() {
return resource;
}
public void setResource(String resource) {
this.resource = resource;
}
public String getRevision() {
return revision;
}
@Value("${config.git.revision}")
public void setRevision(String revision) {
this.revision = revision;
}
}