gpt4 book ai didi

java - Aspect 停用 Spring Controller Mapping

转载 作者:行者123 更新时间:2023-11-30 06:52:17 25 4
gpt4 key购买 nike

我创建了一个 Spring 网站。我使用了一个具有不同实现的抽象通用 Controller 类。如果我不激活任何 Controller 上的方面类,它会很好地工作。

如果我激活了一个方面,那么所有的映射似乎都被停用了:

DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/contact/2]
WARN : org.springframework.web.servlet.PageNotFound - No mapping found forHTTP request with URI [/clubhelperbackend/contact/2] in DispatcherServlet with name 'appServlet'

这是我的抽象 Controller :

public abstract class AbstractController<T extends Data> implements ClubController<T> {

protected Dao<T> dao;
private Class<T> elementClass;

public AbstractController(Dao<T> dao, Class<T> element) {
super();
this.dao = dao;
this.elementClass = element;
}

@Override
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String getAsView(@PathVariable("id") long id, @RequestParam(required = false) boolean ajax, Model m) {
String mapping = elementClass.getSimpleName();
m.addAttribute(mapping, getById(id));
return mapping + "Get" + (ajax ? "Ajax" : "");
}

@Override
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "application/json")
public T delete(@PathVariable("id") long id) {
T obj = getById(id);
// dao.delete(id);
return obj;
}
}

和一个实现:

@Controller
@RequestMapping("/contact")
public class ContactController extends AbstractController<Contact> {

@Autowired
public ContactController(Dao<Contact> contactDao) {
super(contactDao, Contact.class);
}

}

这是我的方面:

@Aspect
@Component
public class DeletedStorageAspect {
//
// private DeletedEntriesDao deletedEntriesDao;
//
// @Autowired
// public DeletedStorageAspect(DeletedEntriesDao deletedEntriesDao) {
// super();
// this.deletedEntriesDao = deletedEntriesDao;
// }

@Pointcut("execution (public * de.kreth.clubhelperbackend.controller.abstr.AbstractController.delete(..))")
private void invocation() {
}

@AfterReturning(pointcut = "invocation()", returning = "deleted")
public void storeDeleted(JoinPoint joinPoint, Data deleted) {
System.out.println("Deleted: " + deleted);
String tableName = deleted.getClass().getSimpleName();
long id = deleted.getId();
Date now = new Date();
DeletedEntries entry = new DeletedEntries(-1L, tableName, id, now, now);
System.out.println(entry);
// deletedEntriesDao.insert(entry);
}
}

这是我的 beans.xml 的一部分:

<aop:aspectj-autoproxy>
<aop:include name="mysqlDbCheckAspect" />
<aop:include name="daoLoggerAspect" />
<aop:include name="controllerSecurityAspect" />
<aop:include name="deletedStorageAspect" />
</aop:aspectj-autoproxy>

我可以通过评论 deletedStorageAspect 来恢复全部功能。

是什么导致了这种行为?为什么不能识别带有方面的映射? Controller 上不允许方面吗?

希望得到一些帮助。

最佳答案

默认情况下,当将 AOP 与 Spring 一起使用时,spring 会创建代理。根据您是否在类上实现接口(interface)(或不实现接口(interface))的事实,它将创建一个 JDK 动态代理(基于接口(interface))或基于 CGLIB 的代理(基于类)。

public abstract class AbstractController<T extends Data> implements ClubController<T> {

在基于接口(interface)的代理(适用于您)的情况下,MVC 基础结构无法看到 @RequestMapping注释不再,将不再检测您的映射。这也是适用于您实现接口(interface)的情况。另见 the reference guide关于使用请求映射进行代理的问题。

要修复它,您必须强制使用基于类的代理,为此添加 proxy-target-class="true"<aop:aspectj-auto-proxy /> .

<aop:aspectj-autoproxy proxy-target-class="true">

关于java - Aspect 停用 Spring Controller Mapping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39217436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com