Normal integration of servlets and spring no problem
However, each time the servlet executes, the spring configuration is loaded, and the spring environment is loaded.
- Workaround: Load the Spring configuration file in the Servlet init method?
- This servlet is currently available, but the other servlets are not available!!!
- Puts the loaded information content into ServletContext. The object that is global when the ServletContext object is created. When the server is started, the spring environment is loaded when the ServletContext is created.
- Servletcontextlistener: Used to listen for the creation and destruction of ServletContext objects.
import; Spring-web-3.2.0.release.jar
Configure 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>
To modify the program's code:
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());WebApplicationContext applicationContext = (WebApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
Spring Integrated JUnit Test
1. There is a junit environment in the program.
2. Import a jar package. Spring and JUnit integrate the jar package.
- Spring-test-3.2.0.release.jar
3. Test the code:
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations="classpath:applicationContext.xml")public class SpringTest {@Autowiredprivate UserService userService;@Testpublic void demo1(){ userService.sayHello(); }}
(1) to (6) Summary
STRUTS2: Hibernate: more knowledge points. Spring:aop. The idea of facing the tangent plane. Spring Framework IOC. Aop. Data access. Integration. Web
- IOC: Control inversion. Give the object the creation right to spring.
- DI: Dependency Injection. DI requires an IOC environment, and DI injects the object's dependent properties into the class when creating the object.
IOC Assembly Bean: (XML)
IOC Assembly Bean: (annotations)
@Component describing beans in the spring framework
@Repository used to label DAO implementation classes
@Service used to label Service implementation classes
@Controller used to label the Controller implementation class
DI attribute injection
- Normal properties:
- Object properties:
Bean's life cycle:
- Post-processing Bean.beanpostprocessor class.
Spring consolidates Web projects:
Spring consolidates JUnit tests:
Spring Integrated Web Development (6)