spring mvc配置aop拦截器的问题

酒剑仙 2013-09-26 04:01:11
本来是拦截所有的service,但我配置后发现,使用注解的service无法拦截。而使用spring文件配置的service是可以被拦截的。
<bean id="actionBeanNameProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>logService</value>
</list>
</property>
</bean>
...全文
216 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zuxianghuang 2013-09-28
  • 打赏
  • 举报
回复
没时间搞,有个c++的问题要处理, 给你几个类,加上我之前一个aspect 拦截方法的工程,试试。我就是在那个工程上加的几个类。 工程下载 (要2分哦)spring3.05的 http://download.csdn.net/download/zuxianghuang/4138590 你把这几个类加上 ,你试试。我这是可以的

/**
 * 
 */
package com.zuxiang.autoproxy.service;

/**
 * @author zuxiang
 *
 */
public interface AnnotationService {

	void doAnnotationService();
}

/**
 * 
 */
package com.zuxiang.autoproxy.service;

import org.springframework.stereotype.Service;

/**
 * @author zuxiang
 *
 */
@Service
public class AnnotationServiceImpl implements AnnotationService{

	@Override
	public void doAnnotationService() {
		// TODO Auto-generated method stub
		System.out.println("\n\tAnnotationServiceImpl ---- doAnnotationService() \n");
	}

}


/**
 * 
 */
package com.zuxiang.autoproxy.service;

/**
 * @author zuxiang
 *
 */
public interface XMLService {
   
	 void doXMLServcie();
}

/**
 * 
 */
package com.zuxiang.autoproxy.service;

/**
 * @author zuxiang
 *
 */
public class XMLServiceImpl implements XMLService{

	@Override
	public void doXMLServcie() {
		// TODO Auto-generated method stub
		System.out.println("\n\tXMLServiceImpl ---- doXMLServcie() \n");
	}

}

/**
 * 
 */
package com.zuxiang.autoproxy;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * @author zuxiang
 *
 */
public class AutoproxyInterceptor implements MethodInterceptor{

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		
		String methodStr = "method '" + invocation.getMethod().getName() + "' of class [" +
				invocation.getThis().getClass().getName() + "]";
		System.out.println("begig ......" + methodStr);
		Object object = invocation.proceed();
		System.out.println("end ......" + methodStr + "\n");
		return object;
	}

}

package com.zuxiang;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zuxiang.autoproxy.service.AnnotationService;
import com.zuxiang.autoproxy.service.XMLService;

public class Test {

    public static void main(String[] args) {
		
    	ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
	    
    	//UserService us = (UserService) ac.getBean("userServiceImpl");
    	XMLService xmlService = (XMLService) ac.getBean("xmlService");
    	AnnotationService annotationService = (AnnotationService) ac.getBean("annotationServiceImpl");
    	//us.getUser();
    	//us.update();
    	
    	//us.delete();
    	//us.remove();
    	//us.insert();
    	xmlService.doXMLServcie();
    	annotationService.doAnnotationService();
    }
}





酒剑仙 2013-09-27
  • 打赏
  • 举报
回复
现在使用另一种aop通知的方式也同样有这样的问题。 注解的service调用时无法得到通知。
酒剑仙 2013-09-27
  • 打赏
  • 举报
回复
引用 2 楼 ZuxiangHuang 的回复:
我试过了,注解和 xml配置都可以拦截, 你要注意,注解的beanName @Service(value = "")这个没指定名字 默认是 类名的第一个字母小写,如果 AnnotationServiceImpl beanName 是 annotationServiceImpl , 反正是java命名规范, 我的xml 配的没有加后Impl ,没拦截到,后来在beanNames里加一个 *ServiceImpl 配置就是这样的,应该是beanName 没匹配。注意一下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName"> <context:annotation-config /> <!-- 开启使用spring注解 --> <context:component-scan base-package="com.zuxiang*" /> <!-- 指定扫描哪些包下的注解 --> <aop:aspectj-autoproxy /> <!-- 开启Aspectj模式的aop代理 --> <bean id="myInterceptor" class="com.zuxiang.MyInterceptor"> </bean> <bean id="xmlService" class="com.zuxiang.service.XMLServiceImpl"> </bean> <bean id="actionBeanNameProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*ServiceImpl</value> <value>*Service</value> </list> </property> <property name="interceptorNames"> <list> <value>myInterceptor</value> </list> </property> </bean> </beans>
我试过了,的确是不行的。 @Service(value="umService") public class UmService implements IUmService 以前没加value属性 现在加了value属性都不行 spring的头文件我看引导的都和你一样的。但代理拦截,就是没有到注解的service里面去。 在spring里面配置的几个service都是可以的。 感到的确非常奇怪。而且我感觉的aop老是无法切入。好像没有配置成功。 能问下你用的spring的什么版本和引入了那些jar包吗?
zuxianghuang 2013-09-27
  • 打赏
  • 举报
回复
我晚上有时间时,把工程给你
zuxianghuang 2013-09-26
  • 打赏
  • 举报
回复
我试过了,注解和 xml配置都可以拦截, 你要注意,注解的beanName @Service(value = "")这个没指定名字 默认是 类名的第一个字母小写,如果 AnnotationServiceImpl beanName 是 annotationServiceImpl , 反正是java命名规范, 我的xml 配的没有加后Impl ,没拦截到,后来在beanNames里加一个 *ServiceImpl 配置就是这样的,应该是beanName 没匹配。注意一下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName"> <context:annotation-config /> <!-- 开启使用spring注解 --> <context:component-scan base-package="com.zuxiang*" /> <!-- 指定扫描哪些包下的注解 --> <aop:aspectj-autoproxy /> <!-- 开启Aspectj模式的aop代理 --> <bean id="myInterceptor" class="com.zuxiang.MyInterceptor"> </bean> <bean id="xmlService" class="com.zuxiang.service.XMLServiceImpl"> </bean> <bean id="actionBeanNameProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*ServiceImpl</value> <value>*Service</value> </list> </property> <property name="interceptorNames"> <list> <value>myInterceptor</value> </list> </property> </bean> </beans>
酒剑仙 2013-09-26
  • 打赏
  • 举报
回复
怎么才能使使用注解的和配置的service都能被拦截呢? 如果要统一成一样的话,改的文件也要比较多。麻烦。 求高手解惑

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧