SpringBoot基础

1、Spring的发展

1、Spring1.x 时代

在Spring1.x时代,都是通过xml文件配置bean,随着项目的不断扩大,需要将xml配置分放到不同的配置文件中,需要频繁的在java类和xml配置文件中切换。

2、Spring2.x时代

随着JDK 1.5带来的注解支持,Spring2.x可以使用注解对Bean进行申明和注入,大大的减少了xml配置文件,同时也大大简化了项目的开发。

那么,问题来了,究竟是应该使用xml还是注解呢?

最佳实践:

  1. 应用的基本配置用xml,比如:数据源、资源文件等;
  2. 业务开发用注解,比如:Service中注入bean等;

3、Spring3.x到Spring4.x

从Spring3.x开始提供了Java配置方式,使用Java配置方式可以更好的理解你配置的Bean,现在我们就处于这个时代,并且Spring4.x

4、Spring的Java配置方式

Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置。

4、1@Configuration 和 @Bean

      Spring的Java配置方式是通过 @Configuration 和 @Bean 这两个注解实现的:

      1、@Configuration 作用于类上,相当于一个xml配置文件;

      2、@Bean 作用于方法上,相当于xml配置中的<bean>;

4、2示例

      该示例演示了通过Java配置的方式进行配置Spring,并且实现了Spring IOC功能。

      和Spring boot都推荐使用java配置的方式。

<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.itcast</groupId>
  <artifactId>springProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
   <!-- Spring Boot 启动父依赖 -->
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
    
    <!-- 如果不想用parent, 可以使用下面配置  -->
    <!-- 
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
     -->
    <dependencies>
    
        <!-- 连接池 -->
        <dependency>
            <groupId>com.jolbox</groupId>
            <artifactId>bonecp-spring</artifactId>
            <version>0.8.0.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        
        
        <!-- 为了支持热交换, hotSwap, 方便开发时候测试 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>xdoclet</groupId>
            <artifactId>xdoclet</artifactId>
            <version>1.2.3</version>
        </dependency>
    </dependencies>
    
    
    <!-- To create an executable jar, 
    we need to add the spring-boot-maven-plugin to our pom.xml. 
    To do so, insert the following lines just below the dependencies section: -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值