首页 新闻 会员 周边 捐助

Spring AOP不起作用

0
悬赏园豆:80 [待解决问题]

(1)web.xml加载Spring配置:

<!-- spring start -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>

<listener>
    <listener-class>
                 org.springframework.web.context.ContextLoaderListener
            </listener-class>
</listener>

<!-- spring end -->

(2)applicationContext.xml:
<!-- 扫描注解,注册bean -->
<context:component-scan base-package="cn.bookstore">
<!-- 跳过@Controller -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!-- 自动代理 -->
<aop:aspectj-autoproxy />

(3)目标对象类SourceController.java(代码不重要,方法名为download()。AOP拦截这个方法):
@RequestMapping(value="/download")
public ResponseEntity<byte[]> download(HttpServletRequest request, HttpServletResponse response) throws Exception {
/
获取文件名及编号
/
Integer sourceId = 0;
String sourceName = request.getParameter("sourceName");
String sourceNum = request.getParameter("sourceNum");
if(null != sourceNum && !"" .equals(sourceNum)) {
sourceId = Integer.parseInt(sourceNum);
}
System.out.println("sourceName:"+sourceName);
System.out.println("sourceNum:"+sourceNum);
/

* 获取文件名后缀
/
File getSuffixFile = new File(sourceName);
String fileName = getSuffixFile.getName();
String suffix = fileName.substring(fileName.lastIndexOf(".")+1);
System.out.println("suffix:"+suffix);
/

* 获取文件路径
*/
Source source = sourceService.getById(sourceId);
String sourceDest = source.getSourceDest();
System.out.println("sourceDest:"+sourceDest);

    // 编码
    request.setCharacterEncoding("UTF-8");
    // 根目录(本地目录)

// String path = "E:\大三\books\你的气质里藏着你读过的书\书籍\[白鹿原].陈忠实.pdf";
String path = sourceDest + "\" + sourceName;
// 下载时看到的文件名
String filename = sourceName;
File file = null;
HttpHeaders headers = null;
file = new File(path);
// 请求头
headers = new HttpHeaders();
// 解决文件名乱码
String filename1 = new String(filename.getBytes("UTF-8"),"ISO-8859-1");
// 通知浏览器以attachment(下载方式)来打开
headers.setContentDispositionFormData("attachment", filename1);
// 最常见的文件下载
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers,HttpStatus.OK);
}

(4)切面:
@Aspect
@Component
public class DownloadCount {
@Pointcut("execution(* cn.bookstore.controller.SourceController.download(..))")
public void download() {}

@Before("download()")
public void print() {
    try {
        System.out.println("下载成功!");
    } catch (Exception e) {
        System.out.println("下载失败!");
    }
}

}

Tomcat跑代码没有报错。但是执行到Controller的download()方法时,切面的方法没有执行。这是为什么呢??

austinhaha的主页 austinhaha | 初学一级 | 园豆:122
提问于:2018-10-23 20:20
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册