现状:在测试@within注解的使用,使用@bean创建的对象可以应用切面,使用@Component注解却没有应用到切面
前提条件:
1.类UserServiceImplTwo继承UserServiceImpl
2.UserServiceImpl有注解ClassLog
从容器中获取UserServiceImplTwo类型的对象,执行父类中的方法
切面如下:
@Before("@within(cn.zz.realmlord.springdemo.annotation.ClassLog)") public void classlog() { System.out.println("@within --- ClassLog"); }
业务类
package cn.zz.realmlord.springdemo.service.impl; import cn.zz.realmlord.springdemo.annotation.ClassLog; import cn.zz.realmlord.springdemo.service.UserService; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; /** * @author */ @Service("userService") @ClassLog public class UserServiceImpl { public void name() { System.out.println("UserServiceImpl.getUserName"); } public void getHello(){ System.out.println("UserServiceImpl.getHello()"); } }
package cn.zz.realmlord.springdemo.service.impl; import org.springframework.stereotype.Service; @Service("userService2") public class UserServiceImplTwo extends UserServiceImpl{ public void getUserName2(){ System.out.println("UserServiceImplTwo.getUserName2()"); } }
注解类:
package cn.zz.realmlord.springdemo.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface ClassLog { }
测试类
package cn.zz.realmlord.springdemo; import cn.zz.realmlord.springdemo.service.Boy; import cn.zz.realmlord.springdemo.service.Human; import cn.zz.realmlord.springdemo.service.Man; import cn.zz.realmlord.springdemo.service.impl.UserServiceImplTwo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; /** * @author */ @SpringBootApplication public class SpringDemoApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SpringDemoApplication.class, args); UserServiceImplTwo userService2 = context.getBean("userService2", UserServiceImplTwo.class); System.out.println(userService2.getClass()); userService2.name(); userService2.getHello(); context.close(); } // @Bean(name = "userService2") public UserServiceImplTwo userService2() { return new UserServiceImplTwo(); } }
结果如下,从结果中看出未执行切面逻辑
class cn.zz.realmlord.springdemo.service.impl.UserServiceImplTwo$$EnhancerBySpringCGLIB$$28e2a95
UserServiceImpl.getUserName
|--- Service Invocation Monitor | Result ---|
|--- 【SPRING】cn.zz.realmlord.springdemo.service.impl.UserServiceImplTwo.name [0] ms
|--- 【SPRING】cn.zz.realmlord.springdemo.service.impl.UserServiceImpl.name [0] ms
UserServiceImpl.getHello()
|--- Service Invocation Monitor | Result ---|
|--- 【SPRING】cn.zz.realmlord.springdemo.service.impl.UserServiceImplTwo.getHello [0] ms
|--- 【SPRING】cn.zz.realmlord.springdemo.service.impl.UserServiceImpl.getHello [0] ms
现在取消UserServiceImplTwo类上的@service注解
//@Service("userService2") public class UserServiceImplTwo extends UserServiceImpl{ public void getUserName2(){ System.out.println("UserServiceImplTwo.getUserName2()"); } }
改为在SpringDemoApplication中通过@Bean的方式创建对象,如下
@Bean(name = "userService2") public UserServiceImplTwo userService2() { return new UserServiceImplTwo(); }
其余不变,执行结果如下,可以看到已经应用了切面
class cn.zz.realmlord.springdemo.service.impl.UserServiceImplTwo$$EnhancerBySpringCGLIB$$e89b1e5
@within --- ClassLog
UserServiceImpl.getUserName
|--- Service Invocation Monitor | Result ---|
|--- 【SPRING】cn.zz.realmlord.springdemo.service.impl.UserServiceImpl.name [0] ms
@within --- ClassLog
UserServiceImpl.getHello()
|--- Service Invocation Monitor | Result ---|
|--- 【SPRING】cn.zz.realmlord.springdemo.service.impl.UserServiceImpl.getHello [0] ms
以上是现象,后面有时间在做具体分析,有人知道的也请告知一下原因,谢谢。