Detailed notes Springboot

      springboot is spring developed a framework to quickly build javaWeb project, its biggest feature is simple and quick, simplify the project a lot of Spring configuration file, the core idea is greater than the agreed configuration, while the notes of this simple and quick way to become not natural two options are summarized below springboot in some common notes, will also continuously updated to add new comment, of course, for @Controller @RequestMapping this is no longer too common example of.

     (A) Class Parameters

       1.@RequestParam    属性  value = "name",required = true/false  defaultValue = xxx 

public void say(@RequestParam("name") String name,@RequestParam("pwd") String password){

}

        2. @ PathVariable and @RequestParam similar style based restful 

@RequestMapping("/demo/{id}/{name}")
public void say(@PathVariable("id") String id,@PathVariable("name") String name1){

}

      3. @ RequestBody the front end of the name attribute in a form bound to the property of the entity classes, consistent with the requirements of the form name and the property name of the entity class name.

public void say(@RequestBody User user){

}

     4. @ RequstAttribute values ​​acquired from the request, the interceptor @ModelAttribute or pre-stored in the attribute value default value true, the Throws ServletRequestBindingException no value, the required attribute can be set to false.

     (B). Annotation scanning class

      1. On @ Component @Controller @Service @Repository @Component three subclasses, the packet is added to the scanned IOC container, for no packet is available to scan the scanning @ComponentScan specified package, a class definition

      2. @ Bean told Spring this method produces a class of its management, generate an instance of Bean, @ Bean @Component more flexible with respect to

      3. @ Configuration of the spring corresponding to the xml <beans>

                 Xml + @ Bean is equivalent to the <bean> 

Package com.dxz.demo.configuration; 

Import org.springframework.context.annotation.Bean;
 Import org.springframework.context.annotation.Configuration;
 Import org.springframework.context.annotation.Scope; 

@Configuration 
public  class TestConfiguration {
     public TestConfiguration () { 
        System.out.println ( "start initialization container TestConfiguration ..." ); 
    } 

    // @Bean annotation registration bean, and can specify the initialization and destruction methods
     // @Bean (name = "the testBean", the initMethod = " Start ", and destroyMethod =" The cleanUp ") 
    @Bean 
    @Scope ( " the prototype " )
     publicTestBean testBean () {
         return  new TestBean (); 
    } 
}

TestBean

package com.dxz.demo.configuration;

public class TestBean {

    private String username;
    private String url;
    private String password;

    public void sayHello() {
        System.out.println("TestBean sayHello...");
    }

    public String toString() {
        return "username:" + this.username + ",url:" + this.url + ",password:" + this.password;
    }

    public void start() {
        System.out.println("TestBean 初始化。。。");
    }

    public void cleanUp() {
        System.out.println("TestBean 销毁。。。");
    }
}

Test category

Copy the code
 Package com.dxz.demo.configuration; 

Import org.springframework.context.ApplicationContext;
 Import org.springframework.context.annotation.AnnotationConfigApplicationContext; 

public  class the testMain {
     public  static  void main (String [] args) { 

        // @Configuration annotation loading spring containers, ClassPathXmlApplicationContext replaced with AnnotationConfigApplicationContext 
        the ApplicationContext context = new new AnnotationConfigApplicationContext (TestConfiguration. class ); 

        // if loading spring-context.xml file:
         // the ApplicationContext context = new new
        // ClassPathXmlApplicationContext("spring-context.xml");
        
         //获取bean
        TestBean tb = (TestBean) context.getBean("testBean");
        tb.sayHello();
    }
}

result:

@Componet can also be added with the use @Configration class TestBean      

(C) the resource class introduced

    1.  @Value

     @Value ( "$ {book.name}") acquired application.properties (also can be your other profiles) of book.name = old man and sea

     @Value ( "# {name}") Get the name attribute of the entity class book

   2.@Resource 和@Autowired

      Bean is used as the injection, @ Resource is java annotations, Spring also support this comment, there Name and Type attributes, when not specified with the name attribute; @Autowired Spring annotation is annotation of default is ByType

      When a class has multiple implementations, @ Resource (name = "xxx ") or @Qualifier ( "woman") and the needs and @Primary @Autowired on the implementation class (man and woman to achieve people, the man or woman in need achieve)

   3. @ ImportResource ( "classpath: xxxxx.xml") to load xml configuration file

   4.@ConfigurationProperties 

    Sometimes necessary configuration information file package entity classes, this will be very easy to call up,

    Profiles:  

connection.username=admin
connection.password=kyjufskifas2jsfs
connection.remoteAddress=192.168.1.1

    Entity classes:

@Component 
@ConfigurationProperties (prefix = "Connection" ) 
@Data     // lombok lombok comments need to be introduced depends IDEA tool mounting lombok effect generated automatically get set method 
public  class ConnectionSettings { 

    Private String username;
     Private String of the remoteAddress;
     Private String password; 
}

 when using it

@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    ConnectionSettings conn;

    @RequestMapping(value = {"/", ""})
    public String hellTask(){
        String userName = conn.getUsername();
        return "hello task !!";
    }

}

Of course, you can also directly use the @ConfigurationProperties and @Bean put together so that you do not need to add annotation on the class ConnectionSettings 

@Bean
    @ConfigurationProperties(prefix = "connection")
    public ConnectionSettings connectionSettings(){
        return new ConnectionSettings();
}

(D) Transaction class

   1.@Transactional  

 

    

Guess you like

Origin www.cnblogs.com/hs5201314tx/p/11595146.html