关于spring aop 报错(我没有分了,大家可怜下,这个问题困扰我很久了)
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.Service;
import com.service.UserService;
public class Test {
public static void main(String args[]){
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
UserService userService=(UserService)ac.getBean("userService");
userService.add();
}
}
package com.aop;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class Interceptor {
@Before("execution(public void com.service.UserService.add())")
public void before(){
System.out.println("before");
}
}
package com.service;
import org.springframework.stereotype.Component;
@Component
public class UserService{
public void add(){
System.out.println("add");
}
}
<?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:aop="http://www.springframework.org/schema/aop"
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">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com"></context:component-scan>
<aop:aspectj-autoproxy />
</beans>
报了一下异常,是为什么呢,哪里出错了??
--------------------编程问答-------------------- UserService userService=(UserService)ac.getBean("userService");
改成
UserService userService=(UserService)ac.getBean("UserService");
注意大小写。。 --------------------编程问答--------------------
不是啊 我改了也不行
不知道是不是我的包错了,一下是我的包
--------------------编程问答-------------------- 你把错误信息发出来。。 --------------------编程问答--------------------
错误跟上面一样, 我直接改成UserService userService=new UserService();也是这样 --------------------编程问答-------------------- 关注中…… --------------------编程问答--------------------
= = 。。你的图只贴到error create bean。。后面没有。。所以看不出到底为什么不能创建bean --------------------编程问答--------------------
哦,是图太长了, 这是第一行的
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in file [D:\工作\java\webwork\test_Spring2\bin\com\service\UserService.class]: BeanPostProcessor before instantiation of bean failed; nested exception is java.lang.NullPointerException --------------------编程问答-------------------- 我遇到和你同样的问题,请问你是怎么解决的。谢谢。 --------------------编程问答-------------------- 不要用 annotation 用XML注入试一下。
补充:Java , Web 开发