Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Reason Analysis and solution of @ ComponentScan invalid in spring

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the reasons for the invalidity of @ ComponentScan in spring and the solutions. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

@ ComponentScan is not valid in spring

When I implemented my first spring AOP program, I followed the mainstream recommendation and used the annotation @ ComponentScan @ Aspect @ Before to implement an aspect.

What makes me wonder is that. My program has never been able to call notifications correctly. And my notice is no different from the mainstream one. The code is as follows:

Notification class, where facets are defined:

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

Business category:

Package com.bfytech.spring_8_bean3; import org.springframework.stereotype.Component; @ Componentpublic 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 configuration class:

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@ComponentScanpublic class BeanConfig {@ Bean public Advice advice () {return new Advice ();}}

AppicationContext.xml

The final call class 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 ());}}

When I'm depressed. A lot of attempts were made, and it was later found that the following line was added to ApplicationContext.xml:

After that, the AOP can be started normally.

After looking up a lot of information, we found out the reason.

It turns out that xml configuration and annotation configuration are confused in a lot of materials!

When you use xml configuration, the content of ApplicationContext.xml will take effect. But only if you need to use FileSystemXmlApplicationContext or ClassPathXmlApplicationContext to read the xml before the configuration will take effect! And @ ComponentScan will be ignored!

When you use annotated configuration, you should use AnnotationConfigApplicationContext to load it, and the @ ComponentScan in the configuration class will take effect.

Modify the code App.java to

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

The operation results are normal!

By the way, there's another hole. Because execution expressions are not checked at compile time, any punctuation errors will be ignored at run time. I wonder why an exception is not thrown), so it needs to be checked over and over again. For example, do you think there is anything wrong with the following expression?

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

This expression is wrong because (*) should be (..). This does not report any errors at run time. But the sliced code will not run.

@ Component and @ ComponentScan generally understand the connection between @ Component and @ ComponentScan

The purpose of the @ Component annotation is to inject the bean we wrote into the container for use.

The @ ComponentScan annotation scans the bean in the package (for example, Spring doesn't know you define a bean unless it knows where to find the bean,ComponentScan and all it does is tell Spring where to find the bean), and it's up to you to define which packages need to be scanned.

Once you specify it, Spring will look for bean in the specified package and its subordinate packages, and these two annotations will be used together.

@ SpringBootApplication and @ ComponentScan, the difference between scanning packages

If your other packages are using the @ SpringBootApplication annotated main app package and its subordinate package, then you don't have to do anything. SpringBoot will automatically scan all the other packages for you. If you have some bean packages that are not in main app and its subordinate packages, then you need to manually add the @ ComponentScan annotation and specify the package where the bean is located.

So much for the analysis of the reasons for the invalidity of @ ComponentScan in spring and the solutions. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report