欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

@ComponentScan在spring中無(wú)效的原因分析及解決方案

 更新時(shí)間:2021年11月05日 14:47:15   作者:CandCplus  
這篇文章主要介紹了@ComponentScan在spring中無(wú)效的原因分析及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

@ComponentScan在spring中無(wú)效

在我實(shí)現(xiàn)第一個(gè)spring AOP程序的時(shí)候,我按照主流的推薦,采用注解@ComponentScan @Aspect @Before 來(lái)實(shí)現(xiàn)一個(gè)切面。

讓我十分納悶的是。 我的程序始終無(wú)法正確調(diào)用到通知。而且我的通知和主流的毫無(wú)差別。代碼如下:

通知類,其中定義了切面:

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");
 }
}

業(yè)務(wù)類:

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>

最后的調(diào)用類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());
    }
}

郁悶之余。做了大量嘗試,后來(lái)發(fā)現(xiàn)在ApplicationContext.xml中添加如下行:

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

之后可以正常把AOP啟動(dòng)起來(lái)。

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

原來(lái)很多資料中把xml配置和注解配置混淆在一起了!

當(dāng)你采用xml配置的時(shí)候,則ApplicationContext.xml的內(nèi)容會(huì)生效。但是前提是你需要采用FileSystemXmlApplicationContext或者ClassPathXmlApplicationContext去讀取這個(gè)xml,配置才會(huì)生效!同時(shí)@ComponentScan會(huì)被忽略!

而當(dāng)你采用注解配置的時(shí)候,則你應(yīng)該使用AnnotationConfigApplicationContext來(lái)加載,這時(shí)配置類的中的@ComponentScan就會(huì)生效。

修改代碼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());
    }
}

運(yùn)行結(jié)果正常了!

順便說(shuō),還有一個(gè)坑。execution表達(dá)式因?yàn)闆](méi)有編譯時(shí)檢查,任何標(biāo)點(diǎn)符號(hào)的錯(cuò)誤也會(huì)在運(yùn)行時(shí)忽略(??我很納悶,為什么不拋異常),所以需要反復(fù)檢查。比如說(shuō)下面的表達(dá)式,你覺(jué)得有錯(cuò)么?

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

這個(gè)表達(dá)式是錯(cuò)誤的,因?yàn)?**)應(yīng)該是(..).而運(yùn)行時(shí)這個(gè)不會(huì)報(bào)任何錯(cuò)誤。但是切片的代碼不會(huì)運(yùn)行.....

@Component和@ComponentScan常規(guī)理解

@Component和@ComponentScan的聯(lián)系

@Component 這個(gè)注解的作用是把我們寫(xiě)的bean注入到容器中,以供使用。

@ComponentScan 注解的作用則是掃描包中的bean(比如:Spring不知道你定義了某個(gè)bean除非它知道從哪里可以找到這個(gè)bean,ComponentScan做的事情就是告訴Spring從哪里找到bean),由你來(lái)定義哪些包需要被掃描。

一旦你指定了,Spring將會(huì)將在被指定的包及其下級(jí)包中尋找bean,這兩個(gè)注解進(jìn)行配合使用。

@SpringBootApplication和@ComponentScan,掃描包的區(qū)別

如果你的其他包都在使用了@SpringBootApplication注解的main app所在的包及其下級(jí)包,則你什么都不用做,SpringBoot會(huì)自動(dòng)幫你把其他包都掃描了如果你有一些bean所在的包,不在main app的包及其下級(jí)包,那么你需要手動(dòng)加上@ComponentScan注解并指定那個(gè)bean所在的包。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java異常處理原理與用法實(shí)例分析

    Java異常處理原理與用法實(shí)例分析

    這篇文章主要介紹了Java異常處理原理與用法,結(jié)合實(shí)例形式分析了Java異常處理相關(guān)概念、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下
    2020-04-04
  • 解決java.lang.ClassCastException的java類型轉(zhuǎn)換異常的問(wèn)題

    解決java.lang.ClassCastException的java類型轉(zhuǎn)換異常的問(wèn)題

    這篇文章主要介紹了解決java.lang.ClassCastException的java類型轉(zhuǎn)換異常的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • 使用Java實(shí)現(xiàn)Redis限流的方法

    使用Java實(shí)現(xiàn)Redis限流的方法

    限流的作用是防止某個(gè)段時(shí)間段內(nèi)的請(qǐng)求數(shù)過(guò)多,造成模塊因高并發(fā)而不可用。這篇文章給大家介紹使用Java實(shí)現(xiàn)Redis限流的相關(guān)知識(shí),一起看看吧
    2021-09-09
  • MyBatis持久層框架的用法知識(shí)小結(jié)

    MyBatis持久層框架的用法知識(shí)小結(jié)

    MyBatis 本是apache的一個(gè)開(kāi)源項(xiàng)目iBatis,接下來(lái)通過(guò)本文給大家介紹MyBatis持久層框架的用法知識(shí)小結(jié),非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧
    2016-07-07
  • java 頁(yè)面url傳值中文亂碼的解決方法

    java 頁(yè)面url傳值中文亂碼的解決方法

    本節(jié)講的是ajax 的URL參數(shù)中有中文值,傳到服務(wù)端,在用request.getParameter()方法,得到的常常會(huì)是亂碼。
    2013-03-03
  • idea神級(jí)插件及如何安裝Bito插件【Bito-ChatGPT】

    idea神級(jí)插件及如何安裝Bito插件【Bito-ChatGPT】

    這篇文章主要介紹了介紹一款idea神級(jí)插件【Bito-ChatGPT】,Bito插件的強(qiáng)大之處在于它可以幫助開(kāi)發(fā)人員更快地提交代碼,同時(shí)還提供了一些有用的功能,如自動(dòng)補(bǔ)全提交信息、快速查看歷史記錄等,需要的朋友可以參考下
    2023-04-04
  • 一文帶你全面了解Java?Properties類

    一文帶你全面了解Java?Properties類

    Properties是JDK1.0中引入的java類,目前也在項(xiàng)目中大量使用,主要用來(lái)讀取外部的配置,那除了這個(gè),你對(duì)它其他的一些api也了解嗎??你了解它是怎么實(shí)現(xiàn)的嗎??如果不清楚的話,就通過(guò)本篇文章帶你一探究竟
    2022-09-09
  • Java處理日期時(shí)間的方法匯總

    Java處理日期時(shí)間的方法匯總

    這篇文章主要給大家介紹了利用Java中的Calendar 類處理日期時(shí)間的方法匯總,其中包括取日期的每部分、取當(dāng)月的第一天或最后一天、求兩個(gè)日期之間相隔的天數(shù)以及一年前的日期等等的示例代碼,有需要的朋友們可以直接參考借鑒,下面來(lái)一起看看吧。
    2016-12-12
  • Spring boot通過(guò)切面,實(shí)現(xiàn)超靈活的注解式數(shù)據(jù)校驗(yàn)過(guò)程

    Spring boot通過(guò)切面,實(shí)現(xiàn)超靈活的注解式數(shù)據(jù)校驗(yàn)過(guò)程

    這篇文章主要介紹了Spring boot通過(guò)切面,實(shí)現(xiàn)超靈活的注解式數(shù)據(jù)校驗(yàn)過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 在SpringBoot中靜態(tài)資源訪問(wèn)方法

    在SpringBoot中靜態(tài)資源訪問(wèn)方法

    這篇文章給大家介紹了在SpringBoot中靜態(tài)資源訪問(wèn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-11-11

最新評(píng)論