话题一:创建一个最简单的SpringBoot工程
- 1.Pom.xml引入文件
<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>ZBoot-SimpleDemo</groupId>
<artifactId>ZBoot-SimpleDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 2.UserDao创建后没有加任何的标注
- 3.创建SpringBoot的配置文件:@Configuration+文件扫描地址+@Bean
@Configuration
@ComponentScan(basePackages = "com.vincent.boot")
public class MySpringConfig {
@Bean
// 通过该注解来表明是一个Bean对象,相当于xml中的<bean>
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<User> 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中的<bean>
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
<?xml version="1.0" encoding="UTF-8"?>
<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>com.vincent</groupId>
<artifactId>TParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<modules>
<module>TServer-Provider</module>
<module>TServer-Consumer</module>
<module>TServer-Consumer-Feign</module>
<module>TServer-Consumer-MyFeign</module>
<module>TServer-Consumer-Ribbon</module>
<module>TServer-Discovery-Eureka</module>
<module>TServer-Discovery-HAEureka</module>
<module>TServer-TT</module>
</modules>
</project>
- 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);
}
}