Método de carregamento da mola
Para o método de arquivo executável, nossa maneira geral de carregar a configuração da mola é
ClassPathXMLApplicationContext
public static void main (string [] args) {classPathXMLApplicationContext xmlapplicationContext = new ClassPathXMLApplicationContext ("ClassPath: spring-Context.xml"); Demoservice DEMOSERVICE = (DEMOSERVICE) XMLAPplicationContext.getBean ("Demoservice"); Text string = DEMOSERVICE.HELLO (); System.out.println (texto); }<Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmlschema-nstance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd "default-utowire =" slameans "default----Init-" false "> <fault-oTowire =" slameans "infault----Init =" <Contexto: anotação-config/> <!-Configure o pacote a ser digitalizado-> <Contexto: component-scan-package = "com.jin.lesson.Context"/> </ Beans>
A partir da primavera 3.0, comece a usar anotações para registrar a configuração da mola
public static void main (string [] args) {anoTationConfigApplicationContext anoTationConfigApplicationContext = new AnoTationConfigApplicationContext (); // Diga ao pacote a ser digitalizado, geralmente a classe de aplicativo anoTationConfigApplicationContext.Scan (main.class.getpackage (). GetName ()); // Atualize o contexto e use o feijão correspondente para se registrar com êxito AnoTationConfigApplicationContext.Refresh (); // Obtenha o Demoservice correspondente por nome Demoservice Demoservice = (Demoservice) AnoTationConfigApplicationContext.getBean ("DemosService"); Text string = DEMOSERVICE.HELLO (); System.out.println (texto); }Demoservice é o nome de um serviço definido. O método de configuração XML também pode ser definido se deve digitalizar usando anotação, como em 1
<Contexto: anotação-config/>
Demoservice é muito simples, como segue
@Service (value = "Demoservice") classe pública Demoservice {public String hello () {return "Hello World"; }}Inicialização de aplicativos da Web
Método de configuração web.xml
Use o próprio servlet da Spring para o registro inicial
</servlet> <Verlet-name> springmvc </servlet-name> <Servlet-class> org.springframework.web.servlet.dispatcherServlet </servlet-class> <init-param> <amam-name> contextConfigLocation </param-name <amam-Class-ClassPath: spring/spring/springle/springlem> springle/springm>/springm>/springm> springlexl/springnxting.xlam> spring.xam/spring.xam/spring.xam> spring.xam> spring.xam> springn-concortconxt <amam-name> <amam-Val-VALM> <ampring/spring-conxtion. <Oad-on-startup> 1 </olload-on-startup> <sync-suported> true </sync-suported> </ervlet> <sirtlet-mapping> <ervlet-name> springmvc </servlet-name> <url-stattern>/</url-pattern> </servlet-mapping>
O registro usando o ouvinte, como o Spring+Structs, é inicializar o conteúdo do contexto dessa maneira
<Ilanter> <Ilvier-class> org.springframework.web.context.contextLoaderListener </ouvinte-class> </sivener> <lister> <lister-class> org.springframework.web.context.request.requestContextListner </lister-clarer-clars-clarer-clarer-clant.web.Context.request.requestContetner </lister-clarer-clars-class>
A maneira de anotar
Ele também usa o servlet para configurar os parâmetros de inicialização. No entanto, desta vez, a classe AnotationConfigPapplicationCexication baseada em anotação deve ser usada, e o servlet deve ser registrado.
@Override public void OnStartUp (servletContext servletContext) lança servletexception {servletRegistration.dynamic despacher = servletContext.addServlet ("despachante", despachoserverlet.class); Dispatcher.setInitParameter ("ContextConfigLocation", getClass (). getName ()); Dispatcher.setInitParameter ("ContextClass", AnoTationConfigWebApplicationContext.class.getName ()); Dispatcher.addmapping ("/*"); Dispatcher.SetLoadOnStartup (1); }O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.