How does java,spring: let the project know that annotation scanning is on?

Spring"s actual combat book is really piecemeal and piecemeal.
or the common fault of master writing, some things for beginners do not know how to operate, but in the eyes of experts are all written as soon as the project is written, do not mention it or mention it before, and then do not mention even the necessary premise of the later operation.

Section 2 of Chapter 2 mentions that the Automated Assembly Bean, mentions the use of @ ComponentScan annotations in the java-config file before informing the project that annotation scanning is turned on. no, no, no.
but the question is how do you let the project know about this java-config file?
ApplicationContext context = new AnnotationConfigApplicationContext (CDPlayerConfig.class);
how about this? But which java file of the project is this created in? If it is a web project, it feels more appropriate to put it in the birth method of ServletContext listeners, what about other projects?
Hey. Heart tiredness

Mar.15,2021

you can refer to the section of the document .

the context is created in the main method.


there is such a line configuration in xml

< context:component-scan base-package= "com.fuck.java.*" / >

if you are using the Java configuration method, you can use the comment @ ComponentScan

on the class where the main method resides.

ApplicationContext context = new. (..)
for annotation scanning, automatic assembly or explicit configuration is required.
because even annotation scanning just doesn't require you to write the @ Bean method and some possible injections in the configuration file. Instead, Spring scans the @ Component classes and some of them @ Autowired , and then Spring creates the @ Bean method corresponding to the corresponding class and implements the possible injection into the configuration file. So the configuration file must also be loaded.

then in the case of the most common and simplest JAVA program, there must be a public static void main (String args []) method to run.
should load the Spring context context, in this main method and the object that references the variable to be used should be obtained by the getBean method of the context context. Only the object (instance) referred to by the reference variable will be automatically assembled (for example, if there is a member variable of @ Autowired in this class, then only the member variable in the instance obtained through contex.getBean will have been injected with appropriate Bean (the class required by the member variable must be annotated with @ Component ).

after solving these problems, it is not difficult to understand why member variables cannot be automatically injected into Servlet in a web application (or the implementation of injecting member variables into Servlet should not be considered when getting started with Spring). Because of the entire web project, you can't see the main method, and the servlet is not created by the getBean () method in the context of Spring, but by Tomcat. The member variables that you expect to be automatically injected into the Servlet created in this way are naturally empty. If you want to interfere with the creation of Servlet, you should intervene in the process of creating Servlet by Tomcat and make changes, which should not be tried when getting started with Spring (or getting started with JavaEE).

Menu