Spring annotation feature, IOC control inversion
The first thing to know about dependency injection is that you don't need a new object every time you instantiate an object , Spring manages the object, when you configure been or @service
Spring will automatically help you instantiate the object, you can call it yourself;
Spring helps you manage objects in two ways
-
- Configure the Bean in the sping configuration file so that the object is instantiated
class is a managed object, the ID Name property injects another object for this object, or the string name is the ID of the object to be injected into the configuration bean, the value of the object to be configured
<BeanID= "Userpoiserver"class= "Com.ifengstar.user.service.impl.UserPoiServiceImpl"> < Propertyname= "Usersyncinfodao"><refLocal= "Userpoidao"/></ Property> < Propertyname= "Navigatordao"><refLocal= "Navigatordao"/></ Property> </Bean>
In spring configuration file configuration, scan package configuration (easy, recommended)
<!-- the package is automatically scanned so that SPRINGMVC that the class with the @controller annotation under the package is the controller
Srping automatically scans for objects under the package withan automatic instantiation of the @ Service, @Controller
<base-package= "Com.ifengsearch.*.controller"/>
<base-package= "Com.ifengsearch.*.service.impl"/>
Simple Configuration Injection parsing
@Service is used to label business layer components (which we typically define as service tiers) @Controller for labeling control-layer components such as actions in struts @Repository for labeling data access Components, That is, DAO component @component refers to the component, we can use this annotation to annotate when the component is not well categorized.
Simple configuration file Template
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/beans HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPR Ing-beans-3.1.xsd Http://www.springframework.org/schema/context http://w Ww.springframework.org/schema/context/spring-context-3.1.xsd Http://www.springframework.org/schem A/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "> <!--support for the default annotation mappings - <Mvc:annotation-driven/> <!--access to a static file - <Mvc:default-servlet-handler/> <!--the package is automatically scanned so that SPRINGMVC that the class with the @controller annotation under the package is the controller - <Context:component-scanBase-package= "Com.ifengsearch.*.controller" /> <Context:component-scanBase-package= "Com.ifengsearch.*.service.impl" /> <!--Avoid IE when performing Ajax, return json appears download file - <BeanID= "Mappingjacksonhttpmessageconverter"class= "Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> < Propertyname= "Supportedmediatypes"> <List> <value>Text/html;charset=utf-8</value> </List> </ Property> </Bean> <!--start the SPRINGMVC annotation feature to complete the mapping of requests and annotations Pojo - <Beanclass= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> < Propertyname= "Messageconverters"> <List> <refBean= "Mappingjacksonhttpmessageconverter" /> <!--JSON Converter - </List> </ Property> </Bean> <!--define the prefix of the file to jump, view mode configuration - <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--the configuration I understand here is to automatically add a prefix and suffix to the return string of the method that follows the action, which becomes an available URL address - < Propertyname= "prefix"value= "/web-inf/jsp/" /> < Propertyname= "suffix"value= ". jsp" /> </Bean> <!--configuration file Upload, if not using file upload can not be configured, of course, if not, then the configuration file does not have to introduce the upload component package - <BeanID= "Multipartresolver"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--default Encoding - < Propertyname= "Defaultencoding"value= "Utf-8" /> <!--Maximum file size - < Propertyname= "Maxuploadsize"value= "10485760000" /> <!--the maximum value in memory - < Propertyname= "Maxinmemorysize"value= "40960" /> </Bean> </Beans>
Simple Insights from Java spring