spring cannot be injected

Recently, I am learning the spring framework. It is the so-called do-it-yourself, well-fed and well-fed, and I found so many pits after writing the code. The summary is as follows:
Of course, first of all, it is necessary to build a SpringMVC project based on maven, and I will not go into details here.
First, after testing, it is found that after the tomcat server is started, spring will automatically initialize the beans configured in the context. If you directly use the main function entry to call, the injected object will have a NULLPOINT error, because the bean to be injected has not been injected. Of course, there is a solution. At this time, you can use manual access to the ApplicationContext, specify the required beanID from it, and use the getBean() method to manually inject it.
Second, when using Spring automatic injection, if the bean we need is injected in class B, when other places need to call the method in class B to use this bean, if we want this bean to be injected successfully, then class B itself must also be declared as a bean to be injected where it is called. Don't use the new B() method, you must use Spring dependency injection, otherwise a nullPoint error will still occur.
Third, during dependency injection, not all beans annotated with autowired can be injected successfully. In the Spring context, the package path for annotation scanning is limited, such as
<context:component-scan base-package="com " />
    Only the annotations under the package path can be successfully recognized, this part of the autowired annotation can play the automatic injection function. If there are external jar packages and beans outside the declared package path that need to be injected, the getter and setter must be enforced. Method
Fourth , I believe there are more pits waiting for me to fill. . . . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326316759&siteId=291194637