当前位置:编程学习 > JAVA >>

Spring aop 没有起作用,一个简单的例子

Spring配置信息<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<aop:aspectj-autoproxy/>
<context:component-scan base-package="advice,service,service.impl"/>


</beans>

一个简单接口package service;

public interface Person {
public void say();
}
接口实现类package service.impl;

import org.springframework.stereotype.Component;

import service.Person;
@Component
public class Chinese implements Person {

@Override
public void say() {
System.out.println("说的话");
}

}
Spring AOP Before
package advice;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class BeforeAdviceTest {

@Before("execution(* service.impl.*.*(..))")
public void authority(){
System.out.println("检查");
}
}
测试类package shi;

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

import service.Person;

public class Test {
public static void main(String[] args) {
ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");

Person p = act.getBean("chinese",Person.class);

p.say();
}
}
运行之后,没有输出 "检查" --------------------编程问答-------------------- Annotation少了一个 实例表注 。 现在企业开发中用ANNOTATION多 还是XML 文档多? --------------------编程问答--------------------
引用 1 楼 w8023w1314 的回复:
Annotation少了一个 实例表注 。 现在企业开发中用ANNOTATION多 还是XML 文档多?


lz需要加上<context:annotation-config/> --------------------编程问答-------------------- 除
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,