1 declaration when complex queries using jpa, the specification declared as static methods, resulting in the injection of service can not be used, so think of two ways, a manual injection a comment injection, when used in this text annotation injection;
Solve the static method call injection service
1 // Let me talk about the solution 2 @Autowired 3 Private AService AService; 4 5 // declare static variables, after calling for Service 6 public static ClassA class classA with; 7 8 // key: by injection annotations achieve 9 @PostConstruct 10 public void the init () { . 11 class classA with = the this ; 12 is veesimpl.aService = AService;. 9 } 13 is 14 // use 15 veesimpl.aService method.;
@PostConstruct java development executive order
1 constructor == "postConstruct ==> init == destory ==> predestory == Uninstall ;; the servlet 2 3 starting from Java EE5 specification, Servlet increase the influence of two Servlet life cycle annotations (Annotation): @ PostConstruct and @PreConstruct. Both annotations are used to modify a non-static void () method. Moreover, this method can not have thrown a statement. . 4 . 5 use, for example: . 6 . 7 . 8 . 1 @PostConstruct // mode. 1 . 9 2 public void the someMethod () { 10 . 3 ... . 11 . 4 } 12 is . 5 13 is . 6 public @PostConstruct void the someMethod () { // embodiment 2 14 . 7 ... 15 . 8 } 16 . 17 . 1 . DESCRIPTION the PostConstruct @ 18 is . 19 is a modified method @PostConstruct run when the server loads the Servlet, and will be called a server, a similar Serclet Inti ( )method. @PostConstruct modified method will be run before the init () method after the constructor. 20 21 2 . @ PreDestroy Description 22 23 is @PreDestroy modified method runs when you uninstall Servlet server, and the server will only be called once, similar to the Servlet's destroy () method. @PreDestroy modified method will run after destroy () method, is completely uninstalled before Servlet. (See below for practical application) 24 25 . 3 . Practice program 26 is 27 the web.xml 28 29 30 . 1 <-! @PostConstruct and @PreDestroy annotation -> 31 2 <servlet> 32 3 <servlet-name>AnnotationServlet</servlet-name> 33 4 <servlet-class>com.servlet.AnnotationServlet</servlet-class> 34 5 </servlet> 35 6 <servlet-mapping> 36 7 <servlet-name>AnnotationServlet</servlet-name> 37 8 <url-pattern>/servlet/AnnotationServlet</url-pattern> 38 9 </servlet-mapping> 39 40 AnnotationServlet 41 42 1 package com.servlet; 43 2 44 3 import java.io.IOException; 45 4 import java.io.PrintWriter; 46 5 import java.sql.Time; 47 6 import java.text.SimpleDateFormat; 48 7 import java.util.Date; 49 8 50 9 import javax.annotation.PostConstruct; 51 10 import javax.annotation.PreDestroy; 52 11 import javax.servlet.ServletException; 53 12 import javax.servlet.http.HttpServlet; 54 13 importthe javax.servlet.http.HttpServletRequest; 55 14 Import javax.servlet.http.HttpServletResponse; 56 is 15 57 is 16 public class AnnotationServlet the extends the HttpServlet { 58 . 17 DF = the SimpleDateFormat new new the SimpleDateFormat ( "HH: mm: ss.SSS"); // set the date format, precision of milliseconds 59 18 is 60 . 19 public AnnotationServlet () { 61 is 20 is System.out.println ( "time:" + df.format ( new new a date ()) + "constructor is executed ..." ); 62 21 } 63 is 22 is 64 23 public void destroy() { 65 24 this.log("时间:"+df.format(new Date())+"执行destroy()方法..."); 66 25 //super.destroy(); // Just puts "destroy" string in log 67 26 // Put your code here 68 27 } 69 28 70 29 @PostConstruct 71 30 public void someMethod(){ 72 31 //this.log ( "modified executed @PostConstruct the someMethod () method ..."); // Note: this will Error 73 is 32 System.out.println ( "Time:" + df.format ( new new a Date ()) + "performing the someMethod @PostConstruct modified () method ..." ); 74 33 is } 75 34 is 76 35 @PreDestroy 77 36 public void otherMethod () { 78 37 [System.out.println ( "time:" + df.format ( new new a Date ()) + "performed @PreDestroy modified otherMethod () method ..." ); 79 38 is } 80 39 81 40 public void doGet(HttpServletRequest request, HttpServletResponse response) 82 41 throws ServletException, IOException { 83 42 this.log("时间:"+df.format(new Date())+"执行doGet()方法..."); 84 43 } 85 44 86 45 public void init() throws ServletException { 87 46 // Put your code here 88 47 this.log("时间:"+df.format(new Date())+"执行init()方法..."); 89 48 } 90 49 91 50 protected void service(HttpServletRequest request, HttpServletResponse response) 92 51 throws ServletException, IOException{ 93 52 this.log("时间:"+df.format(new Date())+"执行service()方法..."); 94 53 super.service(request, response); 95 54 } 96 55 97 56 } 98 99 运行结果: 100 101 102 1034 . Notes 104 105 notes how much it will affect the startup speed of the server. All class files and WEB-INF / lib all jar files in the server at startup, will traverse the Web application WEB-INF / classes, which classes to check the use of annotations. If the program does not use any annotations may be provided <web-app> in web.xml the attribute to true metadatacomplete turn off the server boot routine check. 106 107 108 109 support annotation server needs to support and to Servlet2.5 above specification, so Tomcat 6.0.X and above to the job.
Use of specific methods
1 @PostConstruct notes a lot of people thought it was provided by Spring. Java is actually their own comments. 2 . 3 the Java instructions in the annotation: @PostConstruct annotations are used to modify the non-static void a () method. @PostConstruct be modified method runs when you load the Servlet server, and the server will only be executed once. After performing PostConstruct constructor, init () method before execution. . 4 . 5 We typically use the Spring Framework @PostConstruct annotation to the annotation method the execution order of the entire initialization Bean: . 6 . 7 the Constructor (constructor) -> @Autowired (DI) -> @PostConstruct (annotated method) . 8 . 9 method in a static method call dependence Bean injection of: real. 10 . 11 Package com.example.studySpringBoot.util; 12 is 13 is Import com.example.studySpringBoot.service.MyMethorClassService; 14 Import org.springframework.beans.factory.annotation.Autowired; 15 import org.springframework.stereotype.Component; 16 17 import javax.annotation.PostConstruct; 18 19 @Component 20 public class MyUtils { 21 22 private static MyUtils staticInstance; 23 24 @Autowired 25 private MyMethorClassService myService; 26 27 @PostConstruct 28 public void init(){ 29 staticInstance.myService = myService; 30 } 31 32 public static Integer invokeBean(){ 33 return staticInstance.myService.add(10,20); 34 } 35 }
This is about some explanation @PostConstruct, such as the implementation process and the like
Starting Java EE 5 specification, Servlet Servlet impact in increasing the life cycle of two notes (Annotion); @ PostConstruct and @PreDestroy. Both annotations are used to modify a non-static void () method. There are two ways of writing: @PostConstruct Public void the someMethod () {} or public @PostConstruct void the someMethod () {} was run @PostConstruct modified method when the server loads Servle, and the server will only be executed once. After performing PostConstruct constructor, init () method before execution. The PreDestroy () method is performed after a destroy () method execution
Annotated Servlet life cycle
needs to be noted that the notes will more or less affect the startup speed of the server. Server startup time will traverse the Web application's WEB all class files and WEB-INF / lib all the jar files under -INF / classes, which classes to check the use of annotations. If you are not using any application notes, metadata- can be set in the Web.xml complete property is true. (@PreDestroy support @PostConstruct and the server needs to support Servlet2.5 .Tomcat5.x specification only supports Servlet2.4 specification. ) I say now is illustrated by an example what does it do. For example, I have a situation before I loaded the servlet initialization I want to deal with some things, like load the cache and so on. How to do it. @PostConstruct comes in handy. So why this stuff is not much use of it, because if we are to be loaded before the initialization process or some thing entirely to deal with in the constructor to initialize, but this method requires its own rewrite the constructor. Ok. Directly on the code look at the specific time to use it how to do it.