@ComponentScan在spring中无效的原因分析及解决方案

 更新时间:2021年11月05日 14:47:15   作者:CandCplus  
这篇文章主要介绍了@ComponentScan在spring中无效的原因分析及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

@ComponentScan在spring中无效

在我实现第一个spring AOP程序的时候,我按照主流的推荐,采用注解@ComponentScan @Aspect @Before 来实现一个切面。

让我十分纳闷的是。 我的程序始终无法正确调用到通知。而且我的通知和主流的毫无差别。代码如下:

通知类,其中定义了切面:

package com.bfytech.spring_8_bean3; 
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
 
@Aspect
@Component
public class Advice { 
 @Before("execution(* com.bfytech.spring_8_bean3.Person.getName(..))")
 public void logBeforeFunction() {
  System.out.println("function begin");
 }
 @After("execution(* com.bfytech.spring_8_bean3.Person.*(..))")
 public void logAfterFunction() {
  System.out.println("function end");
 }
}

业务类:

package com.bfytech.spring_8_bean3; 
import org.springframework.stereotype.Component; 
@Component
public class Person {
 private String name;
 private int age;
 public String getName() {
  System.out.println("getName...");
  return name;
 }
 public void setName(String name) {
  this.name = name;
  System.out.println("setName...");
 }
 public int getAge() {
  System.out.println("getAge...");
  return age;
 }
 public void setAge(int age) {
  System.out.println("setAge...");
  this.age = age;
 } 
}

Bean配置类:

package com.bfytech.spring_8_bean3; 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@EnableAspectJAutoProxy
@ComponentScan

public class BeanConfig { 
 @Bean
 public Advice advice() {
  return new Advice();
 }
}

AppicationContext.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
</beans>

最后的调用类App

package com.bfytech.spring_8_bean3; 
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
 
/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );        
        ApplicationContext context = new FileSystemXmlApplicationContext("ApplicationContext.xml");
        Person person = (Person) context.getBean(Person.class);
          person.setName("Tony");
          person.setAge(88);
          System.out.println(person.getName());
          System.out.println(person.getAge());
    }
}

郁闷之余。做了大量尝试,后来发现在ApplicationContext.xml中添加如下行:

<context:component-scan base-package="com.bfytech.spring_8_bean3"></context:component-scan>

之后可以正常把AOP启动起来。

查了大量资料之后,找到了原因

原来很多资料中把xml配置和注解配置混淆在一起了!

当你采用xml配置的时候,则ApplicationContext.xml的内容会生效。但是前提是你需要采用FileSystemXmlApplicationContext或者ClassPathXmlApplicationContext去读取这个xml,配置才会生效!同时@ComponentScan会被忽略!

而当你采用注解配置的时候,则你应该使用AnnotationConfigApplicationContext来加载,这时配置类的中的@ComponentScan就会生效。

修改代码App.java为

package com.bfytech.spring_8_bean3; 
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
 
/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );     
        ApplicationContext context = new AnnotationConfigApplicationContext(BeanConfig.class);
        Person person = (Person) context.getBean(Person.class);         
          person.setName("Tony");
          person.setAge(88);
          System.out.println(person.getName());
          System.out.println(person.getAge());
    }
}

运行结果正常了!

顺便说,还有一个坑。execution表达式因为没有编译时检查,任何标点符号的错误也会在运行时忽略(??我很纳闷,为什么不抛异常),所以需要反复检查。比如说下面的表达式,你觉得有错么?

@Before("execution(* com.bfytech.spring_8_bean3.*.*(**))")

这个表达式是错误的,因为(**)应该是(..).而运行时这个不会报任何错误。但是切片的代码不会运行.....

@Component和@ComponentScan常规理解

@Component和@ComponentScan的联系

@Component 这个注解的作用是把我们写的bean注入到容器中,以供使用。

@ComponentScan 注解的作用则是扫描包中的bean(比如:Spring不知道你定义了某个bean除非它知道从哪里可以找到这个bean,ComponentScan做的事情就是告诉Spring从哪里找到bean),由你来定义哪些包需要被扫描。

一旦你指定了,Spring将会将在被指定的包及其下级包中寻找bean,这两个注解进行配合使用。

@SpringBootApplication和@ComponentScan,扫描包的区别

如果你的其他包都在使用了@SpringBootApplication注解的main app所在的包及其下级包,则你什么都不用做,SpringBoot会自动帮你把其他包都扫描了如果你有一些bean所在的包,不在main app的包及其下级包,那么你需要手动加上@ComponentScan注解并指定那个bean所在的包。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java异常处理原理与用法实例分析

    Java异常处理原理与用法实例分析

    这篇文章主要介绍了Java异常处理原理与用法,结合实例形式分析了Java异常处理相关概念、原理、用法及操作注意事项,需要的朋友可以参考下
    2020-04-04
  • 解决java.lang.ClassCastException的java类型转换异常的问题

    解决java.lang.ClassCastException的java类型转换异常的问题

    这篇文章主要介绍了解决java.lang.ClassCastException的java类型转换异常的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • 使用Java实现Redis限流的方法

    使用Java实现Redis限流的方法

    限流的作用是防止某个段时间段内的请求数过多,造成模块因高并发而不可用。这篇文章给大家介绍使用Java实现Redis限流的相关知识,一起看看吧
    2021-09-09
  • MyBatis持久层框架的用法知识小结

    MyBatis持久层框架的用法知识小结

    MyBatis 本是apache的一个开源项目iBatis,接下来通过本文给大家介绍MyBatis持久层框架的用法知识小结,非常不错,具有参考借鉴价值,感兴趣的朋友一起学习吧
    2016-07-07
  • java 页面url传值中文乱码的解决方法

    java 页面url传值中文乱码的解决方法

    本节讲的是ajax 的URL参数中有中文值,传到服务端,在用request.getParameter()方法,得到的常常会是乱码。
    2013-03-03
  • idea神级插件及如何安装Bito插件【Bito-ChatGPT】

    idea神级插件及如何安装Bito插件【Bito-ChatGPT】

    这篇文章主要介绍了介绍一款idea神级插件【Bito-ChatGPT】,Bito插件的强大之处在于它可以帮助开发人员更快地提交代码,同时还提供了一些有用的功能,如自动补全提交信息、快速查看历史记录等,需要的朋友可以参考下
    2023-04-04
  • 一文带你全面了解Java Properties类

    一文带你全面了解Java Properties类

    Properties是JDK1.0中引入的java类,目前也在项目中大量使用,主要用来读取外部的配置,那除了这个,你对它其他的一些api也了解吗? 你了解它是怎么实现的吗? 如果不清楚的话,就通过本篇文章带你一探究竟
    2022-09-09
  • Java处理日期时间的方法汇总

    Java处理日期时间的方法汇总

    这篇文章主要给大家介绍了利用Java中的Calendar 类处理日期时间的方法汇总,其中包括取日期的每部分、取当月的第一天或最后一天、求两个日期之间相隔的天数以及一年前的日期等等的示例代码,有需要的朋友们可以直接参考借鉴,下面来一起看看吧。
    2016-12-12
  • Spring boot通过切面,实现超灵活的注解式数据校验过程

    Spring boot通过切面,实现超灵活的注解式数据校验过程

    这篇文章主要介绍了Spring boot通过切面,实现超灵活的注解式数据校验过程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • 在SpringBoot中静态资源访问方法

    在SpringBoot中静态资源访问方法

    这篇文章给大家介绍了在SpringBoot中静态资源访问方法,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-11-11

最新评论