Preface
We wrote the foundation of spring yesterday, and continue to write the knowledge related to web today
The use of spring web
- Import dependency first
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.3.RELEASE</version> </dependency>
- Configure the web.xml file
<servlet> <!--servlet Name--> <servlet-name>springservlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--spring Location of files--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:Applaction.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springservlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
- Write controller
@Controller public class RequestController { @ResponseBody @RequestMapping("/request") public String getrequest(){ return this.toString(); } }
- Configure Application.xml file
<bean id="request" class="RequestController" scope="request"></bean> <bean id="session" class="SessionController" scope="session"></bean> <bean id="applaction" class="ApplactionController" scope="singleton"></bean>
scope attribute in web:
Lazy loading of Bean
Setting method:
Be careful:
The Spring container will initialize the bean of SigSingleton scope in advance when creating the container. However, the bean is marked with lazy init = true '. The bean will be initialized only when it is needed.
Only valid for singleton scoped bean s
Advantages: save resources as much as possible.
Disadvantage: may cause an operation response time to increase
Bean property inheritance
There are two ways to go in life, one is the way we must go, the other is the way we want to go. We must finish the way we want to go before we can go the way we want to go.