Spring loading method
For executable file method, our general way of loading Spring configuration is
ClassPathXmlApplicationContext
public static void main(String[] args) { ClassPathXmlApplicationContext xmlApplicationContext = new ClassPathXmlApplicationContext("classpath:spring-context.xml"); DemoService demoService = (DemoService) xmlApplicationContext.getBean("demoService"); String text = demoService.hello(); System.out.println(text); }<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-autowire="byName" default-lazy-init="false"> <!-- Configure beans in annotation --> <context:annotation-config/> <!-- Configure the package to be scanned --> <context:component-scan base-package="com.jin.lesson.context"/></beans>
Starting from spring 3.0, start using annotations to register spring configuration
public static void main(String[] args) { AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(); // Tell the package to be scanned, usually the Application class annotationConfigApplicationContext.scan(Main.class.getPackage().getName()); // Refresh the context and use the corresponding bean to register successfully annotationConfigApplicationContext.refresh(); // Get the corresponding DemoService by name DemoService demoService = (DemoService) annotationConfigApplicationContext.getBean("demoService"); String text = demoService.hello(); System.out.println(text); }demoService is the name of a service defined. The xml configuration method can also be set whether to scan using annotation, such as in 1
<context:annotation-config/>
demoService is very simple, as follows
@Service(value = "demoService")public class DemoService { public String hello() { return "hello world"; }}Initialization of web applications
web.xml configuration method
Use spring's own Servlet for initial registration
<servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
Registration using Listener, like Spring+structs, is to initialize the context content in this way
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>
The way to annotate
It also uses Servlet to configure initialization parameters. However, this time, the annotation-based class AnnotationConfigWebApplicationContext must be used, and the Servlet must be registered.
@Override public void onStartup(ServletContext servletContext) throws 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); }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.