67,538
社区成员
发帖
与我相关
我的任务
分享Map<String, Show> beansOfType = applicationContext.getBeansOfType(Show.class) 获取的map是size为1.感觉多出来的那个是AspectJ框架干的事。查看反编译的class文件,发现多出来一些方法是ajc开头的。请问有什么办法可以避免这种情况吗?
private static /* synthetic */ void ajc$postClinit() {
AShow2.ajc$perSingletonInstance = new AShow2();
}
package com.springAction4.aop;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* 演出类,aop练习1
*/
@Component
@Aspect
@Order(0)
public class Show {
Show(){
System.out.println("切面Show()实例化ok");
System.out.println(this.hashCode());
}
@Pointcut("execution(* com.springAction4.bean.Actor.*(..))")
public void shello() {
}
@Before(value = "shello()")
public void say1() {
System.out.println("演出要开始了");
}
@After(value = "shello()")
public void say2() {
System.out.println("演出要结束了");
}
}
package com.springAction4.bean;
/**
* 演员类,aop练习2,目标
*/
public class Actor {
public void name() {
System.out.println("演出开始,演员自我介绍: my name is Jack");
}
public void sing(int i ){
System.out.println("这是第"+i+"首歌曲,请欣赏!!");
}
}
package com.springAction4.aspectjAop;
import com.springAction4.bean.Worker;
/**
* AspectJ 的类 Au.aj
*/
public aspect Au {
private Worker worker;
public Au(){
}
//通过setter方法注入
public void setWorker(Worker worker){
this.worker = worker;
System.out.println("工作人员已入场");
}
// 定义 Actor 的 构造器 切点 和 后置通知
pointcut actor() : execution(* com.springAction4.bean.Speaker.Speaker() );
after():actor(){
worker.sendMsg("Speaker");
}
// 定义 Actor 的 name 切点 和 后置通知
pointcut name() : execution(* com.springAction4.bean.Speaker.name() );
after():name(){
worker.sendMsg("Speaker name");
}
// 定义 Actor 的 sing 切点 和 后置通知
/* pointcut sing() : execution(* com.springAction4.bean.Actor.sing(..) );
after():sing(){
worker.sendMsg("sing");
}*/
}
package com.springAction4.bean;
/**
* 切面的协作bean
*
*/
public class Worker {
public void take(){
System.out.println("观众已全部交出手机");
}
public void sendMsg(String name){
System.out.println(name + "表演即将开始,请各位观众交出手机");
}
public void broadcast(String performer, String title){
System.out.println(performer + "演奏完毕,刚才演奏的曲子叫:" + title);
}
}
[code=xml]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<bean id="worker" class="com.springAction4.bean.Worker"/>
<!--Spring需要通过静态方法aspectOf获得audience实例-->
<bean class="com.springAction4.aspectjAop.Au" factory-method="aspectOf">
<property name="worker" ref="worker" /><!--通过Spring把协作的bean注入到切面中-->
</bean>
</beans>
package com.springAction4.bean;
public class Speaker {
public void name() {
System.out.println("自我介绍: my name is Jackson");
}
}