Spring Boot配置 基本pom ,编写启动类,spring 注入 使用。
cmd 端 spring boot的启动方式 应该不会涉及到线程安全问题?
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>org.springframework.samples</groupId>
<artifactId>runner</artifactId>
<version>1.0</version>
<properties>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Application
/**
*
*/
package com.liang;
import java.sql.Connection;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
/**
* @author robin
*
*/
@SpringBootApplication
@ComponentScan("com.liang")
public class Application implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(Application.class);
private static String init;
@Autowired
private ScopeBean scopeBean;
//java -jar myproject.jar --spring.config.location=D:\workspace\runner\target\dbconnection.properties
@Bean
@Primary
public DataSource dataSource() {
log.error("-----------------dataSource---------start");
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(init);
dataSource.setUrl(init);
dataSource.setUsername(init);
dataSource.setPassword(init);
log.error("-----------------dataSource---------end");
return dataSource;
}
// @Bean
// @ConfigurationProperties(prefix="datasource.secondary")
// public DataSource secondaryDataSource() {
// return DataSourceBuilder.create().build();
// }
//PropertyPlaceholderConfigurer
//java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
// @Autowired
// HelloBean helloBean;
// @Autowired
// OtherProperty other;
// @Autowired
// DataSource dataSource;
@Override
public void run(String... args) throws Exception {
log.error("-----------------run---------start");
scopeBean.setArg1(init);
//helloBean.getWorld();
// String ls = other.getName();
System.out.println("#########");
// /Connection connection = dataSource.getConnection();
System.out.println(scopeBean.getDataSource());
// log.info("----------------------------------------->" + );
System.out.println("#########");
try{
Thread.currentThread().sleep(10000);
}catch(InterruptedException ie){
ie.printStackTrace();
}
log.error("-----------------run---------end");
System.out.println(scopeBean.getArg1());
}
/**
* @param args
*/
public static void main(String[] args) {
log.error("-----------------main---------start");
init = args[0];
System.out.println("----------init--------" + init);
SpringApplication.run(Application.class, args);
log.error("-----------------main---------end");
}
}
ScopeBean
package com.liang;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
public class ScopeBean {
private String arg1;
@Autowired
DataSource dataSource;
public String getDataSource(){
System.out.println("---------------getDataSource-------");
return dataSource.toString();
}
public String getArg1() {
return arg1;
}
public void setArg1(String arg1) {
this.arg1 = arg1;
}
}