Spring-- annotations development, junit testing, web application development

 

Notes developers need to be configured using annotations in the configuration file;

<-! Open comments Development -> 
<context: Annotation-config /> 
    
! <- he wrote notes in class. Scanning also need to find the packet -> 
<context: Scan-Component Base -package = " DAO " > </ context: Component-Scan>

Base-Package: package name is a full path of the scanning packets Footnote

 

 

Notes development

  First, create notes of Bean 

    1, @ Componet do not know when that layer used in the current bean

    2, @ Repository for dao layer.

    3, @ Service for service layer.

    4, @ Controller for the presentation layer.

 

  Second, the injection comment

    1, @ Value ( "") simple injection:

      Tag properties may be injected, may be injected in the setter methods. (Written in the property, you can not create get, set method)

    2, @ Autowired complex injection: automatic assembly. (The default is assembled according to the type.)

    3, @ Qualifier ( "") Prevention plurality bean automatic assembly are not

    4、@Resource(name = "") 

 

  Third, other notes  

    1, @ Scope ( "") In fact, this property and would like to attribute xml Scope of the bean configuration. It can be set to single, multi-column and so on.

    2, @ PostConstruct initialization;    

    3, @ PreDestroy destruction method;

 

   Junit development

  Spring-text need to import a jar. Junit test can be carried out

@RunWith (. The SpringJUnit4ClassRunner class ) // find the configuration file is then used RunWith 
@ContextConfiguration (locations = " CLASSPATH: Spring / the application.xml " )
 public  class text { 
    
    @Autowired // use dynamic assembly for 
    ways way; // needs assembly class 
    
    @Test need // 
    public  void texting on () { 
        
        way.text (); // method to be tested. 
    } 
}

 

  In the spring of web applications

   Spring-web need to import a jar

  1, configure the Listener

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

  This ContextLoaderListener realized servletContextListener interfaces, in this listener, when the server starts,

  ApplicationContext will find an object, in fact, it is an implementation webApplicationContext objects stored in servletContext.

 

  2, the configuration in web.xml configuration file applicationContext.xml

    By default, you will find in the WEB-INF directory applicationContext file. If you do not have this file under the path,

  Then, you need to configure.

//classpath: 配置文件的路径。
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/application.xml</param-value>
</context-param>

  

 

  

 

 

Guess you like

Origin www.cnblogs.com/huangcan1688/p/11869814.html