Springboot start
SpringApplication.run(MyBootApplication.class);
Springapplication.run start the Springboot application, the main process
- To create a spring container object
- Create a Bean Component object from the Mybootapplication annotation markup feature into the Spring container (@SpringBootApplication)
- In the case of a Web program, the Tomcat server is automatically started and the program is published to the server
- Users can access the Springboot program
@SpringBootApplication role
@SpringBootApplication annotation is a multi-function tag that mainly includes the following sub-tag features:
To a simple interface and implementation class:
// Interface Public Interface Ideptdao { void Save ();} // Implementation Class Public class Deptdao implements Ideptdao { publicvoid Save () { System. out. println (" Configuring ... " ); }}
//@Configuration//The meaning of the two is the same, @SpringBootConfiguration inherit from @configuration, both functions are consistent, labeling the current class is a configuration class,@SpringBootConfiguration//one or more instances of the method that are declared within the current class that are marked with the @bean annotation are included in the spring container, and the instance name is the method name. Public classMyconfigurationbean {//@Primary//main object, the object used by default injection@Bean//The current method is included in the spring container, and the ID defaults to the method name PublicIdeptdao Deptdao () {Ideptdao Dept=NewDeptdao (); returnDept; } @Bean (Name= "DeptDao1")//include the current object in the spring container and specify the name PublicIdeptdao Deptdao () {Ideptdao Dept=NewDeptdao (); returnDept; }}
Test:
Public class Testconfigurationbean {// Instantiate spring boot container public static void Main (string[] args) { = Springapplication.run (mybootapplication. Class, args); = App.getbean ("Deptservice", Deptservice. Class); Bean.adddept (); }}
Results:
Adding data to the database ....
Configuring ....
@EnableAutoConfiguration
Spring Boot automatic configuration tag. The springboot contains a large number of auto-configuration components that can be loaded into spring containers at startup, such as Dispatcherservlet, DataSource, JdbcTemplate, and so on.
Loading principle: After opening the @enableautoconfiguration tag, Call the Enableautoconfigurationimportselector component to load the Spring-boot-autoconfigure.jar package meta-inf/ Spring.factories file, a large number of auto-configuration components are defined in the file, loaded into the spring container.
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,
@ComponentScan
Springboot scan mark. Equivalent to <context:component-scan base-package= "xx"/>
@ComponentScan (basepackages={"Cn.xdl.service"})// equivalent to <context:component-scan base-package= "XX"/ Specify folder //@ComponentScan// Scan Current Package and child package components, generally need to put the class in the root package cn.xdlpublic class Mycomponentscanbean {}Deptservice Component Definitions
@Service Public class Deptservice { publicvoid adddept () { System.out.println ("Add a department information" ); }}
Instantiate the Spring boot container:
Public Static void Main (string[] args) { = springapplication.run (mybootapplication. class ); = Ac.getbean ("Deptservice", Deptservice. Class); System.out.println (deptservice); Deptservice.adddept ();}
Properties configuration Parameter Injection
The parameters in the Application.properties file can be injected into the bean object in the Spring container.
@Component // scan @ Configurationproperties (prefix= "Spring.datasource") // configuration parameter injection public class Mybean { private String Myname;// same as key in Application.properties String MyPassword; @Value ( "${DB.URL1}" private Span style= "color: #000000;" > String URL1; // omit set and get method }
@ConfigurationProperties Specifies to inject the same name parameter in Application.properties to the Bean object property, and if there is a common prefix for the key, you can specify it using the Prefix property. If you encounter a special key, you can use @value tag injection. @ConfigurationProperties need to turn on the automatic configuration feature @enableconfigurationproperties or @enableautoconfiguration.
Springboot built-in datasource mechanism
Springboot automatically creates DataSource objects by automatically configuring the component Datasourceautoconfiguration, with the ID name datasource. The creation process is as follows:
- First create a TOMCAT-JDBC Connection pool object (SPRING-BOOT-STARTER-JDBC contains)
- If TOMCAT-JDBC creation fails, the Create Hikari connection pool object is found
- If Hikari creation fails, the Create COMMONS-DBCP connection pool object is found
- If COMMONS-DBCP creation fails, the Create COMMONS-DBCP2 connection pool object is found
Tip: Spring-boot-starter-jdbc introduced the TOMCAT-JDBC connection pool in the 1.x release, but the HIKARICP connection pool was introduced in the 2.x release.
Use the following steps:
Create Maven project to define JDBC in Pom.xml
<Parent> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-parent</Artifactid> <version>1.4.7.RELEASE</version></Parent><Properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version></Properties><Dependencies> <!--includes DAO, AOP transaction, connection Pooling package (1.x tomcat/2.x HIKARICP) - <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-jdbc</Artifactid> </Dependency> <Dependency> <groupId>Com.oracle</groupId> <Artifactid>Ojdbc6</Artifactid> <version>11.2.0.3</version> </Dependency></Dependencies>
Append connection parameter definition in APPLICATION.YML or properties
Spring:datasource: username:scott password:tiger url:jdbc:oracle:thin: @localhost: 1521:xe DriverClassName:oracle.jdbc.OracleDriver
Turn on the automatic configuration feature before starting the class
@SpringBootApplication Public class mybootapplication {}
Test class
Public Static void Main (string[] args) { = springapplication.run (mybootapplication. class , args); = Ac.getbean ("DataSource", DataSource. Class); System.out.println (DS);}
Create a specified connection pooling method (method one, recommended)
Append the following parameters to the APPLICATION.YML or properties
spring.datasource.type=org.apache.commons.dbcp.BasicDataSource
Create a specified connection pool method (method two)
Customizing DataSource objects using @[email protected]
@SpringBootApplication Public class mybootapplication { @ConfigurationProperties (prefix= "Spring.datasource") @Bean Public DataSource DataSource () { new basicdatasource (); return ds; }}
Spring-boot a simple understanding