SpringBoot

Spring boot自动配置 完成项目打成jar包发布
spring boot-starter 基础包

配置 properties
yml

主入口类

双重作用 启动程序 启动tomcat

创建Bean。官网自动生成组入口类,properties

IOC 功能

组件放容器中产生对象 产生依赖关系

@Configuration //beans
@Bean 产生两个Bean

组件扫描

@componentScan 开启组件扫描
@component 扫描标记

Bean对象参数注入

作用:将applicationContext.properties文件参数传入组件

注解标记

@EnableConfigurationProperties
启用参数注入功能
@EnableConfigurationProperties //启动这个@ConfigurationProperties

默认将application.properties中同名属性注入到Bean对象

@Value("$()")

SpringBoot自动配置

@EnableAutoConfiguration
可以顶替一系列的@Enable配置文件
如 :@EnableAspectJAutoProxy

除了上述标记,还可以自动追加功能组件配置,创建出对象支持 MVC AOP
C:\Users\v_huidongqu.m2\mybaitsPlusRes\org\springframework\boot\spring-boot-autoconfigure\1.4.2.RELEASE\spring-boot-autoconfigure-1.4.2.RELEASE.jar— META-INF----spring.factories

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\
org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration,\
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\
org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\
org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.hornetq.HornetQAutoConfiguration,\
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration,\
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.SecurityFilterAutoConfiguration,\
org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration,\
org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\
org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\
org.springframework.boot.autoconfigure.social.SocialWebAutoConfiguration,\
org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration,\
org.springframework.boot.autoconfigure.social.LinkedInAutoConfiguration,\
org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration,\
org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\
org.springframework.boot.autoconfigure.velocity.VelocityAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration,\
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.WebSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration

SpringBoot_第1张图片
@SpringBootApplication属于Boot顶级标记,内部包含之前学过的Controller service Respository

org.apache.commons.dbcp
cn/com/org 组织名 产品名 域名
主入口类(启动类)
添加@SpringBootApplication BootApplication 构建对象

SpringBoot_第2张图片

SpringBoot支撑RESTFul响应 不支持jsp响应,如何要支持,需要配置文件

IOC创建连接池,

自定义连接池

通过主入口类进行创建 默认子包扫描,

SpringBoot连接池

SpringBoot开启自动配置,可以自动创建DataSource连接池对象,id为dataSource.使用方法如下:
-在pom.xml添加Spring-boot-starter-jdbc定义
----在appliaction.properties参数注入
默认key为

spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

//**指定的连接数据池**
spring.datasource.type=org.apache.commons.dbcp.BasicDataSource

测试dataSource对象

首选创建tomcat-jdbc连接池对象
如果没找到tomcat连接池 接着查找HikarICP
如果HikarICP连接池,接着查找DBCP,但不推荐使用
如果没找到dbcp连接池,查找dbcp2
项目使用DBCP2

org.apache.tomcat.jdbc.pool.DataSource@2fb3260{ConnectionPool[defaultAutoCommit=null;
org.apache.commons.dbcp.BasicDataSource@2ca817f3
org.apache.commons.dbcp2.BasicDataSource@2b06b791

版本可以不指定,使自动跟parent自动匹配

<!--mysql 数据库连接 -->
	<dependency> 
    <groupId>mysql</groupId> 
    <artifactId>mysql-connector-java</artifactId> 
  
	</dependency>
<!-- jdb、dao tomcat连接池  默认为tomcat-jdbc --> 
	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
			<exclusions>
			<!-- 排除功能 -->
				<exclusion>
					<groupId>org.apache.tomcat</groupId>
					<artifactId>tomcat-jdbc</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	
	
	<!-- dbcp数据库连接池 -->
<!-- 	<dependency>
	    <groupId>commons-dbcp</groupId>
	    <artifactId>commons-dbcp</artifactId>
	    <version>1.4</version>
	</dependency> -->

	<!-- dbcp2 -->
	<dependency>
	    <groupId>org.apache.commons</groupId>
	    <artifactId>commons-dbcp2</artifactId>
	</dependency> 

自定义连接池对象

构建c3p0连接池
1.在pom.xml 添加c3p0定义

	<!-- c3p0 创建对象 -->
	
	<dependency>
	    <groupId>c3p0</groupId>
	    <artifactId>c3p0</artifactId>
	    <version>0.9.1.2</version>
	</dependency>
	</dependencies>

参数在application.properties

#c3p0     

db.c3p0.user=root
db.c3p0.password=123456
db.c3p0.jdbcUrl=jdbc:mysql://localhost:3306/test
db.c3p0.driverClass=com.mysql.jdbc.Driver
   
           

2.编写c3p0DataSource.java

@Configuration
public class C3p0DataSourceConfig {
	
	@Bean("c3p0")
	@Primary//当存在多个同类型连接池,默认采用这个连接池@Primary标记·的·
	@ConfigurationProperties(prefix = "db.c3p0")
	public DataSource createC3p0DataSource() throws PropertyVetoException {
		
		ComboPooledDataSource dataSource=new ComboPooledDataSource();
	//	dataSource.setUser("root");
	//	dataSource.setPassword("123456");
	//	dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/text");
	//	dataSource.setDriverClass("com.mysql.jdbc.Driver");
		
		//构建 ComboPooledDataSource对象com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 
		return DataSourceBuilder.create().type(ComboPooledDataSource.class).build();
		//构建tomcat jdb   corg.apache.tomcat.jdbc.pool.DataSource@3381423d
		//return DataSourceBuilder.create().build();
	//	return dataSource;
	}
}

3.测试

	@Test
	public void test4() throws ClassNotFoundException, SQLException {
		ApplicationContext ac=SpringApplication.run(BootApplication.class);
		DataSource dataSource=ac.getBean("c3p0",DataSource.class);
		System.out.println(dataSource);
	}
}

4.自定义创建连接池,默认连接池不再创建

注意事项
@Resource(name=“c3p0”)
private DataSource dataSource;
当多个连接池,使用name属性指定
对某个数据库操作使用自定义
当存在多个数据库连接池,使用多个数据库连接池进行自定义数据库连接池

自动配置

–搭建Boot工程

1.创建MAVEN project
-2.pomxml添加定义
boot-parent boot-starter boot-starter-jdbc jdbc定义
添加application.properties 定义默认连接池参数

—3.根据表操作,创建实体表

—4.主入口类
@SpringBootApplication

–5.创建userDao接口
6.实现接口,参数注入
@Repository
@Resource
7.测试

你可能感兴趣的:(SpringBoot)