Springboot(一)

Springboot(一)

(一)Springboot入门程序

1.1原始入门程序

@RestController
@RequestMapping("hello")
@EnableAutoConfiguration //允许启动自动配置
public class HelloController {

    @GetMapping("show")
    public String hello(){
        return "hello springboot!";
    }

    public static void main(String[] args) {
        SpringApplication.run(HelloController.class);
    }
}
  • 缺点:这种原始入门程序将Controller与main方法写在同一个类中,缺点是多个Controller类之间不能共享。(即定义多个Controller类就需要写多个main方法)
  • 小技巧:查看某个类或者接口的源码时 选中类名 再按Ctrl+Q可以看到格式化后的注解信息
  • image.png
  • @EnableAutoConfiguration注解:

1.2优化入门程序

定义两个Controller和一个公共的入口类Application image.png

Application:

@EnableAutoConfiguration //启动Springboot应用的自动配置
@ComponentScan//类似于<content:component-scan base-packages="">
//扫描该类所在的包以及该包的子子孙孙
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  • ComponentScan: Configures component scanning directives for use with @Configuration classes. Provides support parallel with Spring XML's context:component-scan element. Either basePackageClasses or basePackages (or its alias value) may be specified to define specific packages to scan. If specific packages are not defined, scanning will occur from the package of the class that declares this annotation. 该注解和Spring的XML<context:component-scan>具有相同的功能 用于组件的扫描如果指定特殊的路径则扫描该路径下的组件 如果不指定特殊路径则寻找该类所在包以及其子孙包下的组件

(二)Springboot的属性注入(以注入数据库连接池druid为例)

引入druid的依赖:

<dependency>
      <groupId>com.github.drtrang</groupId>
      <artifactId>druid-spring-boot2-starter</artifactId>
      <version>1.1.10</version>
</dependency>

2.1使用@Value注解

创建JdbcConfig类:

@Configuration //声明该类是Java的配置类 相当于一个XML配置文件
public class JdbcConfig {


    @Value("${jdbc.driverClassName}")
    private String driverClassName;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    @Bean //将返回值注入到Spring容器中
    public DataSource dataSource(){
        DruidDataSource2 dataSource2 = new DruidDataSource2();
        dataSource2.setUrl(this.url);
        dataSource2.setDriverClassName(this.driverClassName);
        dataSource2.setUsername(this.username);
        dataSource2.setPassword(password);
        return dataSource2;
    }
}
  • 缺点:无法被其他类引用

2.2 定义全局的属性类

创建JdbcProperties

@ConfigurationProperties(prefix = "jdbc")
public class JdbcProperties {
    
    private String driverClassName;

    private String url;

    private String username;

    private String password;

    //生成getter和setter
    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

修改JdbcConfig类

@Configuration //声明该类是Java的配置类 相当于一个XML配置文件
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfig {

    @Autowired
    private JdbcProperties jdbcProperties;

    @Bean //将返回值注入到Spring容器中
    public DataSource dataSource(){
        DruidDataSource2 dataSource2 = new DruidDataSource2();
        dataSource2.setUrl(this.jdbcProperties.getUrl());
        dataSource2.setDriverClassName(this.jdbcProperties.getDriverClassName());
        dataSource2.setUsername(this.jdbcProperties.getUsername());
        dataSource2.setPassword(this.jdbcProperties.getPassword());
        return dataSource2;
    }
}

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×