001--SpringBoot创建最简单的项目

话题一:创建一个最简单的SpringBoot工程

  • 1.Pom.xml引入文件

    4.0.0
    ZBoot-SimpleDemo
    ZBoot-SimpleDemo
    0.0.1-SNAPSHOT

    
        UTF-8
        UTF-8
        1.7
    
    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.1.RELEASE
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Camden.SR1
                pom
                import
            
        
    

    
        
            org.springframework
            spring-webmvc
        
        
            commons-logging
            commons-logging
            1.2
        
    

    
        src
        
            
                maven-compiler-plugin
                
                    1.7
                    1.7
                
            
        
    

  • 2.UserDao创建后没有加任何的标注
  • 3.创建SpringBoot的配置文件:@Configuration+文件扫描地址+@Bean
    @Configuration
    @ComponentScan(basePackages = "com.vincent.boot")
    public class MySpringConfig {
        @Bean
        // 通过该注解来表明是一个Bean对象,相当于xml中的
        public UserDao getUserDAO() {
            return new UserDao(); // 直接new对象做演示
        }
    }
  • 4.然后加载Spring的配置文件
    public static void main(String[] args) {
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MySpringConfig.class);
            
            UserService userService = context.getBean(UserService.class);
            
            List userList = userService.queryUserList();
            
            for (int i = 0; i < userList.size(); i++) {
                System.out.println("--------------userList:"+userList.get(i));
            }
            
            context.close();
        }

话题二:如何读取外部配置信息

1.使用@PropertySource进行相关配置
@PropertySource(value = { "classpath:jdbc.properties","classpath:other.properties" }, ignoreResourceNotFound = true)

    @Configuration
    @ComponentScan(basePackages = "com.vincent.boot")
    @PropertySource(value = { "classpath:jdbc.properties" }, ignoreResourceNotFound = true)
    public class MySpringConfig {
        @Bean
        // 通过该注解来表明是一个Bean对象,相当于xml中的
        public UserDao getUserDAO() {
            return new UserDao(); // 直接new对象做演示
        }
        
        @Value("${jdbc.url}")
        private String jdbcUrl;
    
        @Value("${jdbc.driverClassName}")
        private String jdbcDriverClassName;
    
        @Value("${jdbc.username}")
        private String jdbcUsername;
    
        @Value("${jdbc.password}")
        private String jdbcPassword;
    
        @Bean(destroyMethod = "close")
        public DataSource dataSource() {
            BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
            // 数据库驱动
            boneCPDataSource.setDriverClass(jdbcDriverClassName);
            // 相应驱动的jdbcUrl
            boneCPDataSource.setJdbcUrl(jdbcUrl);
            // 数据库的用户名
            boneCPDataSource.setUsername(jdbcUsername);
            // 数据库的密码
            boneCPDataSource.setPassword(jdbcUsername);
            // 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0
            boneCPDataSource.setIdleConnectionTestPeriodInMinutes(60);
            // 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0
            boneCPDataSource.setIdleMaxAgeInMinutes(30);
            // 每个分区最大的连接数
            boneCPDataSource.setMaxConnectionsPerPartition(100);
            // 每个分区最小的连接数
            boneCPDataSource.setMinConnectionsPerPartition(5);
            return boneCPDataSource;
        }       
    }

话题三:启动一个项目被外部访问

  • 1.pom中添加spring-boot-starter-web


    4.0.0
    com.vincent
    TParent
    0.0.1-SNAPSHOT
    pom
    
    
        UTF-8
        UTF-8
        1.7
    
    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.1.RELEASE
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Camden.SR1
                pom
                import
            
        
    
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
    
    
        TServer-Provider
        TServer-Consumer
        TServer-Consumer-Feign
        TServer-Consumer-MyFeign
        TServer-Consumer-Ribbon
        TServer-Discovery-Eureka
        TServer-Discovery-HAEureka
        TServer-TT
    

  • 2.创建Controller+Config+主Application(此时全部合并在一起了)
@Configuration
@SpringBootApplication
@Controller
public class HelloApplication {

    @ResponseBody
    @GetMapping(value = "sayHello")
    public String sayHello() {
        return "sayHello";
    }
    
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(HelloApplication.class);
        application.run(args);
    }
}

你可能感兴趣的:(001--SpringBoot创建最简单的项目)