Metode pemuatan pegas
Untuk metode file yang dapat dieksekusi, cara umum kami memuat konfigurasi pegas adalah
ClasspathxmlapplicationContext
public static void main (string [] args) {classpathxmlapplicationContext xmlapplicationContext = new ClassPathXMLapPlicationContext ("classPath: spring-ontext.xml"); Demoservice demoservice = (demoservice) xmlapplicationContext.getBean ("Demoservice"); String text = DemoService.hello (); System.out.println (teks); }<beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns: konteks = "http: XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-weans-3.2 http://www.springframework.org/schema/context/spring-context-3.2.xsd "default autowire =" byname "default-lazy-init =" false "> <!-konfigurasi kacang di annotation-> <konteks: annotation-init =" false "> <! Base-package = "com.jin.lesson.context"/> </tact>
Mulai dari Spring 3.0, mulailah menggunakan anotasi untuk mendaftarkan konfigurasi pegas
public static void main (string [] args) {annotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext (); // Beri tahu paket yang akan dipindai, biasanya kelas aplikasi AnnotationConfigApplicationContext.scan (main.class.getPackage (). GetName ()); // Segarkan konteksnya dan gunakan kacang yang sesuai untuk mendaftar anotasi yang berhasil AnotasiconfigapplicationContext.refresh (); // Dapatkan demoservice yang sesuai dengan nama demoservice demoservice = (demoservice) annotationconfigapplicationContext.getBean ("demoservice"); String text = DemoService.hello (); System.out.println (teks); }Demoservice adalah nama layanan yang ditentukan. Metode konfigurasi XML juga dapat ditetapkan apakah akan memindai menggunakan anotasi, seperti pada 1
<Konteks: Anotasi-Konfigurasi/>
Demoservice sangat sederhana, sebagai berikut
@Service (value = "Demoservice") kelas publik Demoservice {public string hello () {return "hello world"; }}Inisialisasi aplikasi web
Metode Konfigurasi Web.xml
Gunakan servlet Spring sendiri untuk pendaftaran awal
<servlet> <servlet-name> springmvc </servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherServlet </servlet-class> <Ilin-param> <param-name> contextConfiglocation </param-name> <param-value> Classpath: Spring/Spring/Spring/PARAMLOX.MANT. <param-name> <param-value> Classpath: Spring/Spring/Spring/PARAMLEX.PAREX. <Boad-on-startup> 1 </boid-on-startup> <Sinync-Supported> true </Supported-didukung> </servlet> <servlet-Mapping> <servlet-name> springmvc </servlet-name> <rerl-pola>/</rerl-pola> </servlet-mapping>
Pendaftaran menggunakan pendengar, seperti Spring+Structs, adalah menginisialisasi konten konteks dengan cara ini
<listener> <listener-class> org.springframework.web.context.contextLoaderListener </listener-class> </engeaner> <listener> <leand-class> org.springframework.web.context.request.requestcextListener </listener.web.context.request.requestcex
Cara untuk Anotasi
Ini juga menggunakan Servlet untuk mengonfigurasi parameter inisialisasi. Namun, kali ini, kelas anotasi AnnotationConfigWebApplicationContext harus digunakan, dan servlet harus didaftarkan.
@Override public void onStartup (servletContext servletContext) melempar servletException {servletregistration.dynamic dispatcher = servletcontext.addservlet ("dispatcher", dispatcherServlet.class); dispatcher.setinitparameter ("ContextConfigLocation", getClass (). getName ()); dispatcher.setInitparameter ("ContextClass", AnnotationConfigWebApplicationContext.class.getName ()); dispatcher.addmapping ("/*"); Dispatcher.setloadAnstartup (1); }Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.