Spring integrated web development (6)

There is no problem in integrating Servlet and Spring normally,
but each time the Servlet is executed, the Spring configuration is loaded and the Spring environment is loaded.

  • Solution: Load the Spring configuration file in the init method of the servlet?
    • Currently this servlet can be used, but other servlets cannot be used!!!
    • Put the loaded information into the ServletContext. The ServletContext object is a global object. It is created when the server starts. The Spring environment is loaded when the ServletContext is created.
    • ServletContextListener: Used to monitor the creation and destruction of ServletContext objects.

Import; spring-web-3.2.0.RELEASE.jar
configured in web.xml:

 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
 </context-param>

Modify the code of the program:

WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext applicationContext = (WebApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

Spring integration with JUnit tests

1. There is a Junit environment in the program.
2. Import a jar package. Spring integrates the jar package with junit.

  • spring-test-3.2.0.RELEASE.jar

3. Test code:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class SpringTest {
@Autowired
private UserService userService;

@Test
public void demo1(){
    userService.sayHello();
    }
}








(1) to (6) Summary

Struts2:


Hibernate: There are more knowledge points.


Spring: AOP. Aspect-Oriented Thinking.


Spring Framework IOC. AOP. Data Access. Integration. Web

  • IOC: Inversion of Control. Pass the creation of objects to Spring.
  • DI: Dependency injection. DI needs to have an IOC environment. When DI creates an object, it injects the object's dependent properties into the class.

IOC Assembly Bean: (XML)

  • <bean id=”” class=””/>
  • Configure other properties of the Bean:

    • init-method destroy-method scope
  • DI injected properties:

    • Common properties:
      • <property name=”property name” value=”property value”>
    • Object properties:

      • <property name=”property name” ref=”other class id or name”>
    • Injection of collection properties:

IOC Assembly Bean: (Annotation)

@Component describes the Bean in the Spring framework @Repository is
used to annotate the DAO implementation class
@Service is used to annotate the Service implementation class
@Controller is used to annotate the Controller implementation class

DI property injection

  • Common properties:
    • @Value
  • Object properties:
    • AutoWired
    • Resource

Bean life cycle:

  • Post-processing Bean.BeanPostProcessor class.

Spring Integration Web Project:


Spring integrates Junit tests:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324852797&siteId=291194637