Spring Integrated Web Development (6)

Source: Internet
Author: User
Tags aop

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)
    • <bean id= "" class= ""/>
    • To configure other properties of the bean:

      • Init-method Destroy-method Scope
    • DI Injection properties:

      • Normal 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: (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:
      • @Value
    • Object properties:
      • Autowired
      • Resource
Bean's life cycle:
    • Post-processing Bean.beanpostprocessor class.

Spring consolidates Web projects:

Spring consolidates JUnit tests:

Spring Integrated Web Development (6)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.