Then select to create a Maven webapp instance. Of course, you can also create a Maven webapp project by using the command line method and then import it to MyEclipse.
Configure Spring dependencies in pom. xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>wx.spring</groupId><artifactId>wx.spring.helloworld</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>wx.spring.helloworld Maven Webapp</name><url>http://maven.apache.org</url><build><finalName>wx.spring.helloworld</finalName></build><properties><spring.version>4.0.6.RELEASE</spring.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- Spring dependencies --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency></dependencies></project>
2. Framework configuration (Spring MVC)
In this sample code, the Spring MVC Architecture adopts the xml configuration method, while the IOC operation adopts the Java Based Annotated method.
L web. xml
Web. xml is the overall configuration file of the entire project. In this file, you must specify the project Servlet matching method and the configuration file of the context of the entire project. A Servlet is configured in the web. xml file, which is required by Spring MVC. If you need to use Spring in Struts and other frameworks, You need to declare a listener. In other words, dispatcher-servlet and applicationContext are the same configuration files used in different scenarios.
<Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
<Display-name> Archetype Created Web Application </display-name>
<Servlet>
<Servlet-name> dispatcher </servlet-name>
<Servlet-class>
Org. springframework. web. servlet. DispatcherServlet
</Servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-name> dispatcher </servlet-name>
<Url-pattern>/</url-pattern>
</Servlet-mapping>
<Context-param>
<Param-name> contextConfigLocation </param-name>
<Param-value>/WEB-INF/applicationContext. xml </param-value>
</Context-param>
<Listener>
<Listener-class>
Org. springframework. web. context. ContextLoaderListener
</Listener-class>
</Listener>
</Web-app>
2 dispatcher-servlet.xml
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation ="
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd>
<Context: component-scan base-package = "wx. spring. helloworld. controller"/>
<Bean
Class = "org. springframework. web. servlet. view. InternalResourceViewResolver">
<Property name = "prefix">
<Value>/WEB-INF/views/</value>
</Property>
<Property name = "suffix">
<Value>. jsp </value>
</Property>
</Bean>
</Beans>
3 applicationContext. xml
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation ="
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd>
<Context: component-scan base-package = "wx. spring. helloworld. controller"/>
<Bean
Class = "org. springframework. web. servlet. view. InternalResourceViewResolver">
<Property name = "prefix">
<Value>/WEB-INF/views/</value>
</Property>
<Property name = "suffix">
<Value>. jsp </value>
</Property>
</Bean>
</Beans>
3. Code
1 HelloWorldController
Package wx. spring. helloworld. controller;
Import org. springframework. stereotype. Controller;
Import org. springframework. ui. Model;
Import org. springframework. web. bind. annotation. RequestMapping;
Import org. springframework. web. bind. annotation. RequestParam;
@ Controller
Public class HelloWorldController {
@ RequestMapping ("/hello ")
Public String hello (
@ RequestParam (value = "name", required = false, defaultValue = "World") String name,
Model model ){
Model. addAttribute ("name", name );
Return "helloworld ";
}
}
2/WEB-INF/views/helloworld. jsp
<% @ Page language = "java" contentType = "text/html; charset = ISO-8859-1"
PageEncoding = "UTF-8" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = ISO-8859-1">
<Title> Spring4 MVC-HelloWorld </title>
</Head>
<Body>
<H1> Hello :$ {name}
</Body>
</Html>
3. MessagePrinter. java: service interface class. The implementation of specific functions depends on the MessageService interface.
MessageService members in this class will be automatically injected by Spring at runtime without self-initialization.
Package wx. spring. helloworld. bean;
Import org. springframework. beans. factory. annotation. Autowired;
Import org. springframework. stereotype. Component;
@ Component
Public class MessagePrinter {
Private MessageService service;
@ Autowired
Public void setMessageService (MessageService service ){
This. service = service;
}
Public void printMessage (){
System. out. println (this. service. getMessage ());
}
}
4. MessageService: Interface Class
Package wx. spring. helloworld. bean;
Public interface MessageService {
String getMessage ();
}
5 MyConfiguration: configuration class
Package wx. spring. helloworld. config;
Import org. springframework. context. annotation. Bean;
Import org. springframework. context. annotation. ComponentScan;
Import org. springframework. context. annotation. Configuration;
Import wx. spring. helloworld. bean. MessagePrinter;
Import wx. spring. helloworld. bean. MessageService;
@ Configuration
@ ComponentScan (value = "wx. spring. helloworld. test ")
Public class MyConfiguration {
@ Bean
MessageService getMessageService (){
Return new MessageService (){
Public String getMessage (){
Return "Hello World! IOC & DI ";
}
};
}
@ Bean
MessagePrinter getMessagePrinter (){
Return new MessagePrinter ();
}
}
6 Application
Package wx. spring. helloworld. test;
Import org. springframework. context. ApplicationContext;
Import org. springframework. context. annotation .*;
Import wx. spring. helloworld. bean. MessagePrinter;
Import wx. spring. helloworld. bean. MessageService;
Import wx. spring. helloworld. config. MyConfiguration;
Public class Application {
Public static void main (String [] args ){
ApplicationContext context =
New AnnotationConfigApplicationContext (MyConfiguration. class );
MessagePrinter printer = context. getBean (MessagePrinter. class );
Printer. printMessage ();
}
}
The main content of spring is: ioc aop Transaction Management remote calls are the first three. If you get familiar with a small project, you will basically use it. The original intention of spring design is to facilitate development and design. Another aspect of spring design is that it is not intrusive to the system. It is easier to get started with spring, but it takes some time to be proficient. We recommend that you read the spring in action book.
How to Use spring mvc to write a log template table
Spring is an open-source framework created to solve the complexity of enterprise application development. One of the main advantages of this framework is its layered architecture, which allows you to choose which component to use and provides J2EE application development for an integrated framework.
This Spring series includes three parts-Part 1. I will introduce the Spring framework. I will begin to describe the functions of the framework from the perspective of the underlying model of the Framework. Then I will discuss two of the most interesting modules: Spring-oriented programming (AOP) and IOC containers. Next, we will use several examples to demonstrate the use of IOC containers in typical applications. These examples will also be extended as a basic series of discussions. The second part of this article will introduce the Spring framework's structure through Spring AOP.
See download, download the Spring framework and Apache Ant, and run the sample applications in this series.
Spring framework
The Spring framework is a layered architecture consisting of seven well-defined modules. Is built on the top of the core container of this module. the method defined in Spring core container for creating, configuring, and managing beans is shown in Figure 1.
Figure 1. Seven modules of the Spring framework
Spring framework icon
Each module (or component) enables the Spring framework to be independent or combined with one or more other modules. The functions of each module are as follows:
?? Core container: the Core container provides the basic functions of the Spring framework. The main component of the core container is BeanFactory, which is the implementation of the factory mode. Separate BeanFactory's control reversal (IOC) mode application configurations and dependency specifications with actual application code.
?? Spring context: the Spring context is a configuration file that provides context information to the Spring framework. Spring context includes enterprise services, such as JNDI, EJB, email, internationalization, checksum and scheduling.
?? With the configuration management feature of Spring AOP, the Spring AOP module directly integrates Aspect-oriented programming functions into the Spring framework. Therefore, you can easily make any Spring framework object support AOP. The Spring AOP module provides the Transaction Management Service Based on objects in Spring applications. With Spring AOP, You can integrate declarative transaction management into applications without relying on EJB components.
?? Spring DAO: The jdbc dao abstraction layer provides a meaningful exception hierarchy to manage the structure of exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the number of codes you need to write (such as opening and closing connections) for exceptions ). The JDBC-oriented exceptions of Spring DAO follow the common DAO exception hierarchy.
?? * Spring ORM: The Spring framework inserts some ORM frameworks to provide ORM Object Relational tools, including JDO, Hibernate, and iBatis SQL Map. All of these comply with the general transactions and DAO exception hierarchies of Spring.
?? Spring Web module: The Web context module is built on the application context module, providing context for Web-based applications. Therefore, the Spring framework supports integration with Jakarta Struts. The Web module also simplifies processing multiple requests and Binding Request Parameters to domain objects.
?? * Spring MVC framework: the MVC Framework is a full-featured MVC implementation for Building Web applications. Through the policy interface and MVC Framework, MVC supports a large number of view technologies, including JSP, speed, tile, iText, and POI, which are highly configurable.
The functions of the Spring framework can be used on any J2EE server. Most of the functions are also applicable to non-management environments. Core Content of Spring: Reusable services and data access objects that are not bound to a specific J2EE service are supported. There is no doubt that this object is in different J2EE environments (Web or EJB ).