This article is based on the previous article Springmvc, plus spring. The main purpose of spring is called inversion of control (dependency injection, Ioc/di) and aspect-oriented programming (AOP), this article only introduces IoC, because the main application scenario for AOP is logging, temporarily not needed, and so on when I want to integrate several frameworks are integrated together when added.
Pom.xml does not need to add anything new, because Spring-core and other packages are imported as dependencies when importing SPRING-WEBMVC, so look directly at the configuration.
1. Web. xml
Spring wants to inject an instance of this object into the program when it needs an object, and by default, Spring maintains an instance of all the objects that need to be injected in a single case, where the corresponding instance needs to be given, and spring's own instance, The management of the program's Operation forms the spring's own container, and the first step is to register it in Web. XML, initialize the container:
<Context-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>Classpath:/meta-inf/applicationcontext.xml</Param-value></Context-param> <Listener> <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class></Listener>
Register a Contextloaderlistener here and specify the location of the spring configuration file, under the Classpath/meta-inf/ Applicationcontext.xml, now create the directory Meta-inf under the MAVEN resources package, the src/main/resources below, Create the Applicationcontext.xml in this directory, which is the spring configuration file.
2. Applicationcontext.xml
Applicationcontext.xml is the core configuration file of Spring, Spring4 and previous version of a very big difference, is recommended that the bean is not configured in the XML file, but by scanning fixed annotation class, depending on the type of object or name automatically loaded, previously in Applicationcontext.xml need to configure a large number of beans, it is not necessary now, but still have to tell spring, which package to look for these with annotation waiting to be scanned class:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:jee= "Http://www.springframework.org/schema/jee"xmlns:p= "http://www.springframework.org/schema/p"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 Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/ Spring-jee-3.0.xsd "> <Context:component-scanBase-package= "Org.zhangfc.demo4ss.service"/></Beans>
In front of this pile of things first without tube, paste well, in fact, many are still not used, but there is no relationship, first put in this good, really useful configuration on a word, need spring management class, please go to org.zhangfc.demo4ss.service below to find.
3. Userservice.java
Write a service class below to get a list of registered users, create a package:org.zhangfc.demo4ss.service, create an interface userservice below, and write a method to get all the user names.
Public Interface UserService { public list<string> getallusernames ();}
Then create a class Userserviceimpl to implement this interface:
@Service Public class Implements UserService { public list<string> getallusernames () { Listnew Arraylist<string>(); Users.add ("Zhangsan"); Users.add ("Lisi"); Users.add ("Wangwu"); return users; }}
The key here is @service this annotation, it tells Spring, I am a service, need you to manage me.
4. HomeController
Back to the controller, the previous article I wrote a method called JSON to return a JSON object, now change this method, through the previous write userservice to get the user list and return to the client.
First define the global variable UserService for a class:
@Autowired Private UserService UserService;
Note Here is autowire this annotation, it is to tell spring, this object is not instantiated, you need to inject a userservice instance, the problem is, Userserivce is an interface, Don't specify who knows which implementation class you want to use, spring will first see if there is an object in its container called UserService (the name of the Userserviceimpl object is called Userserviceimpl), If not found in the configuration file is configured in the path below to find UserService implementation class, found the object to bring it over, in addition to the service that annotation can also specify a value:
@Service ("UserService")
In this way, for instance of the class Userserviceimpl, spring gives it the name is not Userserviceimpl, but UserService, if an interface has more than one implementation class, you can use this method to specify which implementation class, Personally, if each interface has only one implementation class, then it is very convenient to do so, but if there are multiple implementation classes and may be replaced, it is not as clear as the configuration file (of course, now this way can also be extracted from the parameters in the configuration file, but still some trouble).
Then modify the JSON method:
@RequestMapping ("/json") @ResponseBody public list<string> json () { return userservice.getallusernames ();}
Spring's role here is to inject the userservice needed by HomeController, run the program, and access the Http://localhost:8080/demo4springmvc-spring/json:
["Zhangsan", "Lisi", "Wangwu"]
SOURCE download
Another feature of spring is that AOP is not needed, and so on when the log is required to write, of course, spring itself has a lot of complex mechanisms, and later in the specific problem slowly introduced.
Construction method and example of "Java EE" springmvc+spring