maven 搭建spring boot web服务程序

一、环境说明:

JDK1.8

Eclipse Neon.1a Release (4.6.1)

Maven 3.3.3

二、工程搭建

1,eclipse中创建maven工程。

按下next后出现下面的页面,勾选“create a simple project(skip archetype selection)”

下一步出现下面的画面,如下输入。

因为要用spring boot,所以Parent project处如红框所示输入。

按下Finish,完成工程创建,创建好的工程目录如下:

自动生成的pom.xml如下:

三、编写代码

1,,配置POM.XML文件,加入spring-boot-starter-ws,spring-boot-starter-jdbc等依赖。

<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>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
  </parent>
  <groupId>com.main</groupId>
  <artifactId>SpringBootServiceDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Demo</name>
  <description>Demo for spring boot service.</description>
  
  <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-ws</artifactId>
		</dependency>

		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<scope>runtime</scope>
		</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-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

2,编写application.properties文件

3,编写DatasourceConfig.java文件

package com.config;


import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
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.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class DatasourceConfig {

	    @Bean(name = "postgrelDataSource")
	    @Qualifier("postgrelDataSource")
	    @Primary
	    @ConfigurationProperties(prefix="spring.datasource.postgrel")
	    public DataSource postgrelDataSource() {
	    	return DataSourceBuilder.create().build();
	    }

	    
	    @Bean(name = "redshiftDataSource")
	    @Qualifier("redshiftDataSource")
	    @ConfigurationProperties(prefix="spring.datasource.redshift")
	    public DataSource redshiftDataSource() {
	    	return DataSourceBuilder.create().build();
	    }
	    
	    @Bean(name = "db2DataSource")
	    @Qualifier("db2DataSource")
	    @ConfigurationProperties(prefix="spring.datasource.db2")
	    public DataSource db2DataSource() {
	    	return DataSourceBuilder.create().build();
	    }

	    @Bean(name = "postgrelJdbcTemplate")
	    public JdbcTemplate postgrelJdbcTemplate(
	            @Qualifier("postgrelDataSource") DataSource dataSource) {
	        return new JdbcTemplate(dataSource);
	    }

	    @Bean(name = "redshiftJdbcTemplate")
	    public JdbcTemplate secondaryJdbcTemplate(
	            @Qualifier("redshiftDataSource") DataSource dataSource) {
	        return new JdbcTemplate(dataSource);
	    }
	    
	    @Bean(name = "db2JdbcTemplate")
	    public JdbcTemplate db2JdbcTemplate(
	            @Qualifier("db2DataSource") DataSource dataSource) {
	        return new JdbcTemplate(dataSource);
	    }

}

4,编写入口Application.java类

package com;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Jsr330ScopeMetadataResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;


@Configuration
//TODO データソース動的切替え(scopeResolver = Jsr330ScopeMetadataResolver.class)
@ComponentScan(scopeResolver = Jsr330ScopeMetadataResolver.class)
@EnableAutoConfiguration
@EnableTransactionManagement
@SpringBootApplication
public class Application extends SpringBootServletInitializer{ 

    @Bean(name = "postgrelManager")
    public PlatformTransactionManager postgrelManager(@Qualifier("postgrelDataSource")DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "redshiftManager")
    public PlatformTransactionManager redshiftManager(@Qualifier("redshiftDataSource")DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    
    @Bean(name = "db2Manager")
    public PlatformTransactionManager db2Manager(@Qualifier("db2DataSource")DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    
    /**
     * Servletコンテナ起動時の設定クラス認識。
     *
     * <PRE>
     * Servletコンテナで起動したときにどのクラスが設定クラスなのか認識させます。
     * </PRE>
     *
     * @param  SpringApplicationBuilder
     * @return SpringApplicationBuilder
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
    
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}


5,编写测试类LoginController.java

package com.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/userLogin")
public class LoginController {
	
	@RequestMapping("/login")
	public String login(HttpServletRequest request, HttpServletResponse response, @RequestParam("uId") String uId) {
		String strUId=uId;
		
		return "Hello " + strUId;
	}
	
}


四、测试

启动程序

待程序启动成功,浏览器输入以下地址:

http://localhost:8080/userLogin/login?uId=tommy

页面成功显示结果:


至此结束。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值