当前位置: 代码迷 >> J2EE >> spring2.5.6实现注解AOP出现奇怪异常
  详细解决方案

spring2.5.6实现注解AOP出现奇怪异常

热度:37   发布时间:2016-04-22 01:54:17.0
spring2.5.6实现注解AOP出现奇怪错误
代码如下:
拦截类
Java code
package com.spring.aop.service;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;@Aspectpublic class InterCeptor {    @Pointcut("execution (public void com.spring.aop.service.impl.PersonServiceBeanImpl.*(..))")    private void anyMethod(){}//声明一个切入点            @Before("anyMethod()")//放入切入点的名称带()    public void doBeforCheck(){        System.out.println("我是前置通知!!");    }            }

业务接口:
Java code
package com.spring.aop.service;public interface PersonServiceBean {    public void save(String name);    public void update(String name,Integer id);    public String getPersonName(Integer id);}

实现类:
Java code
package com.spring.aop.service.impl;import com.spring.aop.service.PersonServiceBean;public class PersonServiceBeanImpl implements PersonServiceBean{        public void save(String name) {        System.out.println("我是存入方法:"+name);    }    public void update(String name, Integer id) {        System.out.println("我是更新方法:"+id);    }    public String getPersonName(Integer id) {        return "测试:"+id;    }}

配置文件:
XML code
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">    <!--    <bean name="myTest" class="com.service.impl.PersonServiceImpl">    </bean>          <bean id="myTest3" name="myTest4" class="com.service.impl.PersonServiceImpl"/>    <bean id="myTest2" name="myTest" class="com.service.impl.PersonServiceImpl"/>    <bean id="personServiceImpl" class="com.service.impl.PersonServiceImpl" lazy-init="false" scope="prototype"></bean>    -->    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>    <bean id="interCeptor" class="com.spring.aop.service.InterCeptor"></bean>    <bean id="personServiceBeanImpl" class="com.spring.aop.service.impl.PersonServiceBeanImpl"></bean></beans>

测试类:
Java code
package mytest;import java.lang.reflect.Method;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.spring.aop.service.PersonServiceBean;import com.spring.aop.service.impl.PersonServiceBeanImpl;//import com.service.impl.PersonServiceImpl;public class SpringTest {    @Test public void myTest(){        ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"bean.xml"});//        PersonServiceImpl personServiceFactory = (PersonServiceImpl)ctx.getBean("personServiceImpl");//        PersonServiceImpl personServiceFactory2 = (PersonServiceImpl)ctx.getBean("personServiceImpl");//        if(personServiceFactory == personServiceFactory2){//            System.out.println("====2=====");//        }//        Class<PersonServiceImpl> cPersonServiceImpl = PersonServiceImpl.class;//        personServiceFactory.save();        PersonServiceBean personServiceBeanImpl = (PersonServiceBeanImpl)ctx.getBean("personServiceBeanImpl");        personServiceBeanImpl.save("test");    }}
  相关解决方案