最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

@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è)注解的作用是把我們寫的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)文章

  • Spring Security靈活的PasswordEncoder加密方式解析

    Spring Security靈活的PasswordEncoder加密方式解析

    這篇文章主要介紹了Spring Security靈活的PasswordEncoder加密方式解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • SpringBoot使用GraphQL開(kāi)發(fā)Web API實(shí)現(xiàn)方案示例講解

    SpringBoot使用GraphQL開(kāi)發(fā)Web API實(shí)現(xiàn)方案示例講解

    這篇文章主要介紹了SpringBoot使用GraphQL開(kāi)發(fā)Web API實(shí)現(xiàn)方案,GraphQL是一個(gè)從服務(wù)端檢數(shù)據(jù)的查詢語(yǔ)言。某種程度上,是REST、SOAP、或者gRPC的替代品
    2023-04-04
  • SpringBoot中的multipartResolver上傳文件配置

    SpringBoot中的multipartResolver上傳文件配置

    這篇文章主要介紹了SpringBoot中的multipartResolver上傳文件配置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 關(guān)于Springboot的日志配置

    關(guān)于Springboot的日志配置

    Spring Boot默認(rèn)使用LogBack日志系統(tǒng),如果不需要更改為其他日志系統(tǒng)如Log4j2等,則無(wú)需多余的配置,LogBack默認(rèn)將日志打印到控制臺(tái)上,需要的朋友可以參考下
    2023-05-05
  • Java讀寫鎖ReadWriteLock的創(chuàng)建使用及測(cè)試分析示例詳解

    Java讀寫鎖ReadWriteLock的創(chuàng)建使用及測(cè)試分析示例詳解

    這篇文章主要為大家介紹了Java讀寫鎖ReadWriteLock的創(chuàng)建使用及測(cè)試分析示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • spring boot自定義404錯(cuò)誤信息的方法示例

    spring boot自定義404錯(cuò)誤信息的方法示例

    這篇文章主要介紹了spring boot自定義404錯(cuò)誤信息的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-09-09
  • Spring實(shí)戰(zhàn)之使用p:命名空間簡(jiǎn)化配置操作示例

    Spring實(shí)戰(zhàn)之使用p:命名空間簡(jiǎn)化配置操作示例

    這篇文章主要介紹了Spring實(shí)戰(zhàn)之使用p:命名空間簡(jiǎn)化配置操作,結(jié)合實(shí)例形式分析了spring p:命名空間簡(jiǎn)單配置與使用操作技巧,需要的朋友可以參考下
    2019-12-12
  • java面試題解LeetCode27二叉樹(shù)的鏡像實(shí)例

    java面試題解LeetCode27二叉樹(shù)的鏡像實(shí)例

    這篇文章主要為大家介紹了java面試題解LeetCode27二叉樹(shù)的鏡像實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • MySql多表查詢 事務(wù)及DCL

    MySql多表查詢 事務(wù)及DCL

    這篇文章主要介紹了MySql多表查詢 、事務(wù)、DCL的相關(guān)資料,需要的朋友可以參考下面文章內(nèi)容
    2021-09-09
  • Scala遞歸函數(shù)調(diào)用自身

    Scala遞歸函數(shù)調(diào)用自身

    這篇文章主要介紹了Scala遞歸函數(shù),Scala遞歸函數(shù)是一種函數(shù)可以調(diào)用自身的函數(shù),直到滿足某個(gè)特定的條件為止。在函數(shù)式編程的語(yǔ)言中,遞歸函數(shù)起著重要的作用,因?yàn)樗梢杂脕?lái)表示循環(huán)或迭代的邏輯
    2023-04-04

最新評(píng)論

邻水| 丽江市| 京山县| 武穴市| 沅江市| 临城县| 星座| 航空| 体育| 儋州市| 霍城县| 巨野县| 台南县| 微山县| 崇文区| 常熟市| 永平县| 区。| 南昌县| 佳木斯市| 宽城| 福建省| 南雄市| 皮山县| 石楼县| 桦川县| 东安县| 杭锦后旗| 开化县| 汝州市| 南昌市| 特克斯县| 通辽市| 峨眉山市| 湖北省| 天津市| 大港区| 永善县| 浦东新区| 卢龙县| 吴川市|