初识springboot

SpringBoot作为Spring家族的新成员,简化了Spring和SpringMVC的配置过程,支持以Jar包形式独立运行,并内嵌Tomcat无需WAR部署。它还简化了Maven配置,实现了Spring的自动配置,并提供了生产级的功能,例如指标、健康检查等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        Springboot:

spring旗下的全新框架,指在简化spring,springmvc繁琐的配置,提高开发效率,从而使开发不再需要样板化的配置,成为热门的快速开发的领导者

springboot的特点:(需要jdk1.7以上版本运行)

1. 创建独立的Spring应用程序
Spring Boot可以以jar包的形式来运行,运行一个Spring Boot项目我们只需要通过Java -jar xx.jar类运行。非常方便
2. 嵌入的Tomcat,无需部署WAR文件: Spring Boot可以内嵌Tomcat,这样我们无需以war包的形式部署项目
3. 简化Maven配置: 使用Spring或者SpringMVC我们需要添加大量的依赖,而这些依赖很多都是固定的,这里Spring Boot 通过starter能够帮助我们简化Maven配置
4. 自动配置Spring: 事务,注解,在springboot中一个注解搞定,@SpringBootAppalication:自动添加 spring.xml文件 并且配置 自动扫描,自动添加web.xml 同时 在web.xml 配置过滤器 拦截器,@EnableTransactionManagement 表示启动全局事务支持
全局类:
/**
 * 自动添加 spring.xml文件 并且配置 自动扫描
 * 自动添加web.xml 同时 在web.xml 配置过滤器 拦截器
 * 
 * @EnableTransactionManagement 表示启动全局事务支持 用MVC设计时,还需要在服务层加上@Transactional注解,表示启用了事务,同时在查询方法上@Transactional(readOnly=true)
 */
@EnableTransactionManagement
@SpringBootApplication
public class Helloboot{
//	@Action(value="hello")
//	public String Hello() throws IOException{
//		ServletActionContext.getResponse().getOutputStream().write("hello springboot and struts2".getBytes());
//		return null;
//	}
	public static void main(String[] args) {
		SpringApplication.run(Helloboot.class, args);
	}
}


5. 提供生产就绪型功能,如指标,健康检查和外部配置
6. 绝对没有代码生成和对XML没有要求配置
附加:
1,.springboot的ioc,即spring的bean容器,实现如下,两个注解
/**
 * 相当于标示 该类是一个bean的容器
 * @author Administrator
 *
 */
@Configuration
public class BeanContainer {
	/**
	 * 表示调用该方法产生一个bean
	 * 方法名是bean的id
	 * @return
	 */
	@Bean
	public User userTest(){
		return new User();
	}
}
自定义的dbcp数据源:
@Configuration
public class BeanContainer {
	//<context:property-placeholder location="classpath:/jdbc.properties"/>
	@ConfigurationProperties(prefix="mydbcp")
	
	@Bean
	public DataSource basicDataSource(){
		BasicDataSource basicDataSource = new BasicDataSource(); 
		return basicDataSource;
	}
}
2.appalication.properties,springboot的核心配置文件
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=scott
spring.datasource.password=123456
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
# level 日志级别
#‘logging.level.*=LEVEL’ where ‘LEVEL’ is one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
logging.level.root=INFO
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=DEBUG


#自己引入的数据源
mydbcp.url=jdbc:oracle:thin:@localhost:1521:orcl
mydbcp.username=scott
mydbcp.password=123456
mydbcp.driverClassName=oracle.jdbc.OracleDriver	
3,mybatis和springboot,集成,maven依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.et</groupId>
  <artifactId>SpringbootTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   <!-- Inherit defaults from Spring Boot -->
 	  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    
    <dependencies>
    <!-- 表示可以发布一个web应用,启动一个tamcat -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

	<!-- 添加转译jsp的依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>
		<!-- jpa,操作数据源的入口 -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>cn.et</groupId>
	        <artifactId>ojdbc6</artifactId>
	        <version>6</version>
			<scope>system</scope>
			<systemPath>C:\app\Administrator\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar</systemPath>
		</dependency>
		
		<!-- dbcp数据源 -->
		<dependency>
		  <groupId>commons-dbcp</groupId>
		  <artifactId>commons-dbcp</artifactId>
		</dependency>
		<!-- struts2有关的包 -->
		<dependency>
		  <groupId>org.apache.struts</groupId>
		  <artifactId>struts2-core</artifactId>
		  <version>2.3.28</version>
		</dependency>
		<dependency>
		  <groupId>org.apache.struts</groupId>
		  <artifactId>struts2-spring-plugin</artifactId>
		  <version>2.3.28</version>
		</dependency>
		<dependency>
		  <groupId>org.apache.struts</groupId>
		  <artifactId>struts2-convention-plugin</artifactId>
		  <version>2.3.28</version>
		</dependency>
		<!-- mybatis集成  该包在mybatis官网-->
		<dependency>
		  <groupId>org.mybatis.spring.boot</groupId>
		  <artifactId>mybatis-spring-boot-starter</artifactId>
		  <version>1.2.1</version>
		</dependency>
		<!-- jsonlib与ajax -->
		<dependency>
		  	<groupId>net.sf.json-lib</groupId>
		  	<artifactId>json-lib</artifactId>
		  	<version>1.1</version>
  			<classifier>jdk15</classifier>
		</dependency>
    </dependencies>
  <build/>
</project>







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值