发表于: 2018-02-07 23:30:32

1 671


今天完成

1—学习SpringAOP,how2j中的讲解让我豁然开朗。所谓AOP就是面向切面编程。在面向切面编程思想里面,把功能分为核心业务功能和周边功能。所谓核心业务,比如登陆,数据库操作等都是核心业务。所谓周边功能,比如性能统计,日志,事务管理等等。周边功能在Spring的面向切面编程AOP思想里,即被定义为切面。在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发。然后把切面功能和核心业务功能“编织”在一起,这就叫做AOP。辅助功能又叫做切面。这种能够选择性的,低耦合的把切面和核心业务功能结合在一起的编程思想,就叫做面向切面编程。

按照tutorialspoint网站上的例子跑成了,现在Spring的配置文档,applicationContext.xml中增加一句<aop:aspectj-autoproxy/>,这样Spring就能发现@AspectJ风格的切面并且将切面应用到目标对象。增加一句<bean id = "logging" class = "com.byou.Logging"/>,为了将来注入logging这个方法对象。
之后配置logging.java,内容如下:
package com.byou;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class Logging {
    /** Following is the definition for a PointCut to select
     *  all the methods available. So advice will be called
     *  for all the methods.
     */
    @Pointcut("execution(* com.byou.*.* (..))")
    /**
     * @Pointcut("execution(* com.byou.*.*(..))")
     * @Pointcut 是用来指定哪些方法需要被执行AOP。
     * pointcut有很多种方式定义,其中execution()用的最多。
     * 格式是:execution(modifiers-pattern? ret-type-pattern
     * declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)
     * 其中return type pattern、 name pattern、 parameters pattern是必须的。
     *
     * 这句话的意思是:
     * 第一个*表示return任何返回值,全路径的类名等。
     * com.byou.*.*是指指定方法名,*代表所有方法,set*代表所有set开头的方法。
     * (..)表示所有参数,(*)代表一个参数,(*,String)表示第一个参数为任意值,第二个为String类型。
     */
    private void selectAll(){
    }
    /**
     * This is the method which I would like to execute
     * before a selected method execution.
     */
    @Before("selectAll()")
    public void beforeAdvice(){
        System.out.println("Going to setup student profile.");
    }
}
其中@Pointcut标签的用法已经在注释中体现。最后运行主函数,运行结果如下:
其中输出name和age是核心业务,输出going to setup student profile是所谓的辅助功能。
以上这个例子只是SpringAOP的一个非常简单的应用,但是根据任务要求,需要打印连接服务器时间,还是不知道从何入手。于是在网上找了一些例子,参考了一些师兄的日报。最中实现,先上结果:
    (1)首先在Spring配置文档applicationContext.xml中增加内容:
<bean id="LogAdvice" class="com.byou.util.LogAdvice"/>
<aop:config>
    <aop:pointcut id="aspect" expression="execution(* com.byou.service.CategoryServiceImpl.* (..))"/>
    <aop:aspect id="myAspect" ref="LogAdvice">
        <aop:around method="around" pointcut-ref="aspect"/>
    </aop:aspect>
</aop:config>
首先是一个打印日志方法的bean。之后的是aop的配置。
然后增加了一个打印日志方法:
public class LogAdvice {
    private static Logger loggerAdvice = Logger.getLogger(LogAdvice.class);
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        //获取组件类型
        String className = pjp.getSignature().getName();
        //获取调用方法名
        String method = pjp.getSignature().getName();
        //取得数据库连接前时间
        long begin = System.currentTimeMillis();
        //当前系统时间
        String date = new SimpleDateFormat("yyyy-MM-dd:mm:ss").format(new Date());
        Object obj = pjp.proceed();
        //取得数据库连接后时间
        long end = System.currentTimeMillis();
        //数据库响应时间
        int sqlTime = (int) (end-begin);
        String msg = date + "、执行了" + className + "." + method + "()";
        loggerAdvice.warn(msg + "\t数据库响应时间: " + sqlTime);
        return obj;
    }
}
这之后将log4j的配置文件,log4j.properties中的打印日志范围修改为warn,即只打印高于warn的日志,运行程序,就能够得到每一步操作与数据库连接所消耗的时间。
2---配置阿里云服务器上的三个服务器的端口,因为之前三个服务器的端口都是默认8080端口,这样的话,三个服务器无法同时启动,因为端口只能一个服务器占用。将jetty的端口改为8090,按照网上的方法发现找不到8080端口的设置。后来试着在jetty安装目录下的etc文件夹中有一个start.ini文件,将其中的
http port = 8080 这句之前的警号’#’删掉,将8080改为8090,登陆8090端口发现登陆成功。resin的端口改为8081,这个按照网上的方法就能修改成功。
但是之后的事情就诡异了,服务器反应变得很慢,去阿里云控制台查看,发现CPU的占用正常,但是网络却是暂无数据,说明阿里云服务器网络不好?直到要睡觉了也没有好。


明天计划:

学习脚本,争取能提交任务三。


遇到问题:

跑通了AOP的demo程序之后,发现还是不会完成任务中的要求。之后查看了一下之前师兄的日报,解决了。



返回列表 返回列表
评论

    分享到