1、接口的设计
package com.aa.spring;
public interface UserManager {
public void modifyUser(String id,String username,String password) ;
public void deleteUser(String id) ;
public void addUser(String username,String password) ;
}
2、接口的实现
package com.aa.spring.impl;
import com.hejunfeng.spring.UserManager;
public class UserManagerImpl implements UserManager {
public void addUser(String username, String password) {
System.out.println("----------UserManagerImpl.addUser()----------------");
 }
public void deleteUser(String id) {
System.out.println("----------UserManagerImpl.deleteUser()----------------");
 }
public void modifyUser(String id, String username, String password) {
System.out.println("----------UserManagerImpl.modifyUser()----------------");  
 }
}
4、实现此切点并进行相应的一些操作(annoation注解)
package com.aa.spring.impl;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;//这里采用注解方式
@Aspect
public class MySecurityManagerImpl {
 
//定义切入点addMethod(),只负责用来描述切入那些方法.这里是add方法.其它的可能很多
//接收所有的ADD方法是否有返回值是否有*无参数都接收
@Pointcut("execution(* add*(..))")
private void allAddMethod(){
  
 }
//定义Advice方法用来标识在切入点的何处进行织入
//抽出一个切面就是安全性检查security()
@Before("allAddMethod()")
public void security() {
  
System.out.println("---------作用就是进行安全性检查---------");
 }
}
5、applicationContext.xml文件的配置信息:
<beans http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans"
      xmlns:http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance"
      xmlns:http://www.springframework.org/schema/aop">http://www.springframework.org/schema/aop"
      xmlns:http://www.springframework.org/schema/tx">http://www.springframework.org/schema/tx"
      xsi:schemaLhttp://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<aop:aspectj-autoproxy/>
<bean id="mySecurityManagerImpl" class="com.aa.spring.impl.MySecurityManagerImpl">
</bean>
<bean id="userManager" class="com.aa.spring.impl.UserManagerImpl"></bean>
</beans>
5、测试,和其它方法效果一致
package com.aa.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.aa.spring.UserManager;
public class TestStatic {
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
UserManager userManager = (UserManager)factory.getBean("userManager");
userManager.addUser("aaa", "110") ;
 }
}运行结果报如下错:
log4j:ERROR Could not find value for key log4j.appender.logfile
log4j:ERROR Could not instantiate appender named "logfile".
2009-10-13 10:47:15,921 INFO [org.springframework.core.CollectionFactory] - JDK 1.4+ collections available
2009-10-13 10:47:15,953 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [applicationContext-other.xml]
2009-10-13 10:47:16,125 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=21185076]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [org.springframework.aop.config.internalAutoProxyCreator,securityHandler,userManager]; root of BeanFactory hierarchy
2009-10-13 10:47:16,125 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - 3 beans defined in application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=21185076]
2009-10-13 10:47:16,140 INFO [org.springframework.aop.framework.DefaultAopProxyFactory] - CGLIB2 not available: proxyTargetClass feature disabled
2009-10-13 10:47:16,203 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Bean 'org.springframework.aop.config.internalAutoProxyCreator' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2009-10-13 10:47:16,203 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@f9c40]
2009-10-13 10:47:16,203 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@110c31]
2009-10-13 10:47:16,203 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [org.springframework.aop.config.internalAutoProxyCreator,securityHandler,userManager]; root of BeanFactory hierarchy]
2009-10-13 10:47:16,406 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Destroying singletons in {org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [org.springframework.aop.config.internalAutoProxyCreator,securityHandler,userManager]; root of BeanFactory hierarchy}
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userManager' defined in class path resource [applicationContext-other.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut allAddMethod
Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut allAddMethod
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:315)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:172)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.checkReadyToMatch(AspectJExpressionPointcut.java:162)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.getClassFilter(AspectJExpressionPointcut.java:103)
at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:171)
at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:231)
at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:256)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:68)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:54)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:247)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:311)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1038)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:420)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:290)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:348)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:92)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:77)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:68)
at com.lei.spring.Client.main(Client.java:10)

解决方案 »

  1.   

    1、要让@Aspect注解生效,需要在xml文件中声明:
    <aop:aspectj-autoproxy/>2、实现类也应该交给容器管理
    如果用注解方式给交容器,在@Aspect上面或下面加上 @Component
    这时容器的xml又要打开扫描注解装配bean功能
    <context:component-scan base-package="你的包名"/>
      

  2.   

    加了这些东西后,还要注意schema命名空间,即头部<beans>的属性指定这些命名,例如:
    <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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    ">
      

  3.   

    最后这个问题,我自己解决了。是因为Jar包的问题,
      

  4.   

    大哥,是什么jar包问题?小弟也遇到了这个错误