วิธีการโหลดสปริง
สำหรับวิธีการไฟล์ที่เรียกใช้งานได้วิธีการทั่วไปของเราในการโหลดการกำหนดค่าสปริงคือ
classpathxmlapplicationContext
โมฆะคงที่สาธารณะหลัก (สตริง [] args) {classpathxmlapplicationcontext xmlapplicationContext = ใหม่ classPathxMlapplicationContext ("classpath: spring-context.xml"); Demoservice demoservice = (demoservice) xmlapplicationContext.getBean ("demoservice"); สตริงข้อความ = demoservice.hello (); System.out.println (ข้อความ); -<beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3 http://www.springframework.org/schema/context/spring-context-3.2.xsd "default-autowire =" byname "default-lazy-init =" false "> <! base-package = "com.jin.lesson.context"/> </ebeans>
เริ่มต้นจาก Spring 3.0 เริ่มใช้คำอธิบายประกอบเพื่อลงทะเบียนการกำหนดค่าสปริง
โมฆะสาธารณะคงที่หลัก (สตริง [] args) {AnnotationConfigapplicationContext AnnotationConfigapplicationContext = ใหม่ AnnotationConfigaplicationContext (); // บอกแพ็คเกจที่จะสแกนโดยปกติแล้ว Application Class AnnotationConfigapplicationContext.scan (main.class.getPackage (). getName ()); // รีเฟรชบริบทและใช้ถั่วที่สอดคล้องกันเพื่อลงทะเบียน ANNOTATIONCONFIGAPPLICANCONTEXTETECT.REFRESH (); // รับ Demoservice ที่สอดคล้องกันโดยชื่อ Demoservice Demoservice = (Demoservice) AnnotationConfigapplicationContext.getBean ("Demoservice"); สตริงข้อความ = demoservice.hello (); System.out.println (ข้อความ); -Demoservice เป็นชื่อของบริการที่กำหนดไว้ วิธีการกำหนดค่า XML ยังสามารถตั้งค่าได้ว่าจะสแกนโดยใช้คำอธิบายประกอบเช่นใน 1
<บริบท: Annotation-config/>
Demoservice นั้นง่ายมากดังนี้
@Service (value = "demoservice") Demoservice คลาสสาธารณะ {String สาธารณะ Hello () {return "Hello World"; -การเริ่มต้นของเว็บแอปพลิเคชัน
web.xml วิธีการกำหนดค่า
ใช้ servlet ของ Spring สำหรับการลงทะเบียนครั้งแรก
<servlet> <servlet-name> springmvc </servlet-name> <servlet-lass> org.springframework.web.servlet.dispatcherservlet </servlet-lass> <init-param> <load-on-startup> 1 </load-on-startup> <sync-upported> True </async-supported> </servlet> <servlet-mapping> <servlet-name> springmvc </servlet-name> <url-pattern>/</url-pattern>
การลงทะเบียนโดยใช้ผู้ฟังเช่น Spring+Structs คือการเริ่มต้นเนื้อหาบริบทด้วยวิธีนี้
<Sistener> <Sistener-Lass> org.springframework.web.context.contextloaderlistener </listener-class> </listener> <sistener>
วิธีการใส่คำอธิบายประกอบ
นอกจากนี้ยังใช้ servlet เพื่อกำหนดค่าพารามิเตอร์การเริ่มต้น อย่างไรก็ตามในเวลานี้จะต้องใช้คำอธิบายประกอบคลาสที่ใช้คำอธิบายประกอบคำอธิบายประกอบและจะต้องลงทะเบียนและต้องลงทะเบียน servlet
@Override โมฆะสาธารณะ onStartup (servletContext servletContext) พ่น servletexception {servletregistration.dynamic dispatcher = servletContext.addservlet ("dispatcher", dispatcherservlet.class); dispatcher.setInitParameter ("contextConfigLocation", getClass (). getName ()); dispatcher.setInitParameter ("contextclass", AnnotationConfigWebapplicationContext.class.getName ()); dispatcher.addmapping ("/*"); dispatcher.setloadonstartup (1); -ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น