yesterday, the data set to write interface, accidentally fell into the pit struggled for a long while, and finally found that spring and springmvc they are father and son but not harmonious, so in this.
Spring and SPRINGMVC, as the default framework for the Bean Management container and the MVC layer, have been adopted by many Web applications, and in real-world development, many XML-based configurations have been replaced by the powerful annotations feature, but in real-world projects, We often configure spring and SPRINGMVC configuration files, layered to manage them, but sometimes there will be some strange anomalies, once into the pit, so that you can not extricate themselves, in yesterday to the data set interface, I went into the pit, in the pit of the degree divided into the year, Although it was difficult to be very impetuous at the moment, but at this time in the general writing this blog post I am very happy, really everyone.
First, I have to help you understand the parent-child containers (2 containers) and how they are initialized, and how their parent and son share the bean resources. For example: Lao Tzu's assets son can use, but the son of the property of Lao Tzu is generally not used; Conversely, the spring parent container is not visible to the bean in the SPRINGMVC child container, whereas in the child container, the bean in the parent container is seen. Meaning is this meaning, these concepts I am also these two genius brain complement, before also not a shape, you will read it, the article at the end is I specifically to give you a beautiful picture, please slow use (is about the parent-child container in the visibility of the bean and how the Web container initializes them).
Here, I don't have to say a lot of nonsense, directly to everyone I went to the pit yesterday and out of the scene of a simple description.
Scenario One: The scheduled task is started by a parent-child container, and the timing task of the son fails due to the size of the packet scan. Although they are father and son, but also not harmonious ah, you think it is loving family resources sharing Ah, my child ah is not. The Com.zxz.action package structure is scanned in the core configuration file of Springmvc, and the annotation configuration <task:annotation-driven/> of the scan timed task is first opened in the Spring management configuration file. and then scanned the Com.zxz.service package structure, but did not touch the sub-container site, this bad for the ignorant of my rookie, at the same time to open the two world of timed tasks, but also in the silly opening of the console to execute it, but only dad's scheduled task, but the son's scheduled task hangs, ......... After a period of tangled, found that the problem, because they are 2 containers, 2 containers ah, not you in this container configuration, it means that the container will also come into effect ah, no, you have to distinguish between father's father, son is son.
The project package structure is as follows:
Scheduled Tasks for service tiers:
1 /**2 * Start the Spring container scan package within the scope of the scheduled task 3* /4 @Scheduled (cron = "0/3 * * * *?") )5 public void servicetask () {6 System.out.println ("Service **********"); 7 }
timing Tasks for the controller layer:
1 /**2 * Open the SPRINGMVC container Scan package within the range of scheduled tasks 3* /4 @Scheduled (cron = "0/6 * * * *?") )5 public void webtask () {6 System.out.println ("controller****** ****"); 7 }
The timing task of the son did not perform the configuration:
1 <!--Task Task Scan annotations-->2 <task:annotation-driven/>3 4 <!--Open the Spring container's package scan-->5 <context: Component-scan base-package = "Com.zxz.service"/>
Expand the configuration of the package structure for scheduled task scanning:
1 <!--Task Task Scan annotations-->2 <task:annotation-driven/>3 4 <!--Open the Spring container's package scan-->5 <context: Component-scan base-package = "Com.zxz"/>
Scenario Two: a constant value in a configuration file loaded by a parent-child container in a project that is inadvertently causing the configuration in the parent container's configuration file to not be configured to load the resource file, and the value of the relevant constant is never obtained in the service layer's code? As stated above, 2 containers each use their own. We often have in the development of such an action, that is, the immutable constants and their values in the project are stored in a resource file, if you need to use their values in the code can be directly obtained, it is not necessary to find the corresponding constants in the code and modify their values, it is foolish, extremely inconvenient, Smart people are going to pull it out. For easy maintenance and easy to modify. I this project all the constants and their values are placed in the Resource.propertie resource file, but due to my neglect, I thought highly the spring their family relations, resulting in my service layer of code has been reported NullPointerException exception, let me debug the breakpoint for half a day ....... Hey, finally found that I did not configure the add-in in the parent container's configuration file, I completely trance, do not know whether they are the main idea or the dish, in short, these are some very subtle details, everyone.
The controller layer and service layer load constants simultaneously (if you use spring and SPRINGMVC as containers, remember to configure them at the same time):
to load the configuration of a resource file in a parent-child container:
1 <!-- -2<location = "classpath*:p roperties/*. Propertie "/>
All right, everyone, I share something for the time being so much, because I am sure that so much, but I see from the internet, father and son containers caused by a lot of problems, such as can let the transaction failure ah what, rookie in further exploration, if the results will give you on time report. If there is anything wrong in the blog post, please leave a message, I will check and revise in time, thank you for your welcome (also do not forget to look at the picture).
Figure One: The visibility of beans in a parent-child container.
Figure Two: Initialization of a parent-child container.
Spring and SPRINGMVC Father's container---[prev]