Méthode de chargement à ressort
Pour la méthode de fichier exécutable, notre façon générale de charger la configuration de ressort est
ClasspathxmlapplicationContext
public static void main (string [] args) {classpathxmlapplicationContext xmlapplicationContext = new ClassPathxmlApplicationContext ("classPath: spring-context.xml"); DemoService DemoService = (DemoService) xmlApplicationContext.getBean ("DemoService"); Texte de chaîne = DemoService.hello (); System.out.println (texte); }<Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns: context = "http://www.springframework xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/contex http://www.springframework.org/schema/context/spring-context-3.2.xsd "default-autowire =" byname "default-lazy-init =" false "> <! - Configurez le paquet à annotation -> <contexte: annotation-config /> <! - Configurer le pack Base-Package = "com.jin.lesson.context" /> </ beans>
À partir du printemps 3.0, commencez à utiliser les annotations pour enregistrer la configuration du ressort
public static void main (String [] args) {annotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext (); // indique au package d'être analysé, généralement la classe d'application annotationConfigApplicationContext.scan (main.class.getPackage (). GetName ()); // actualiser le contexte et utiliser le bean correspondant pour enregistrer avec succès AnnotationConfigApplicationContext.refresh (); // Obtenez le démoservice correspondant par nom DemoService DemoService = (DemoService) annotationConfigApplicationContext.getBean ("DemoService"); Texte de chaîne = DemoService.hello (); System.out.println (texte); }Demoservice est le nom d'un service défini. La méthode de configuration XML peut également être définie s'il faut scanner en utilisant l'annotation, comme dans 1
<Context: Annotation-Config />
Demoservice est très simple, comme suit
@Service (value = "DemoService") classe publique DemoService {public String hello () {return "Hello World"; }}Initialisation des applications Web
Méthode de configuration web.xml
Utilisez le propre servlet de Spring pour l'enregistrement initial
<VerTlet> <Servlet-Name> SpringMvc </ Servlet-Name> <Serplet-Class> org.springframework.wisservlet.dispatcherServlet </ Servlet-Class> <Init-Param> <AmAn-Name> ContextConfiglocation </ Param-name> <param-Value> CLASSPATH: printemps / Spring-Context.xml </ Param-Value> </ Init-Param> Innit-PARAM <Lofor-on-Startup> 1 </ Load-on-Startup> <async-Supported> true </ async-supported> </vrlet> <Servlet-Mapping> <Serplet-Name> Springmvc </ Servlet-name> <Url-Pattern> / </ URL-Pattern> </ Servlet-Mapping>
L'enregistrement utilisant l'auditeur, comme Spring + Struts, consiste à initialiser le contenu du contexte de cette manière
<Souciner> <auditeur-class> org.springframework.web.context.contextLoaderListener </ auteur-class> </duner> <ouciner> <écouteur-class> org.springframework.web.context.request.requestContextListener </ auditeur-class> </ouener>
Le chemin pour annoter
Il utilise également Servlet pour configurer les paramètres d'initialisation. Cependant, cette fois, la classe basée sur l'annotation AnnotationConfigWebApplicationContext doit être utilisée et le servlet doit être enregistré.
@Override public void OnStartup (servletContext ServletContext) lève ServletException {servletRegistng. Dispatcher.SetInitAramètre ("contextConfiglocation", getClass (). getName ()); Dispatcher.SetInitParameter ("contextClass", annotationConfigWebApplicationContext.class.getName ()); Dispatcher.AddMapping ("/ *"); Dispatcher.SetLoadOnStartup (1); }Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.