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>com.jessexu</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
         <version>1.4.1.RELEASE</version>
    </dependency>
</dependencies>
   <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.4.1.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

build中的插件是用于打成可执行的fat-jar包。

新建一个hello包:

@Controller
@EnableAutoConfiguration
@ComponentScan(basePackages={"service"})
public class SampleController {
    @Autowired
    private  UserService   userService;
    
    @RequestMapping("/user")
    @ResponseBody
    String home() {
        return userService.getName();
    }
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}

再新建一个service包,新建UserService:

package service;

import org.springframework.stereotype.Service;

@Service
public class UserService {
    
    
    
    public  String    getName(){
        
        
        return   "hello  world";
    }

}

运行: localhost:8080/user

注解 @EnableAutoConfiguration:这是一个自动配置的注解,用于自动配置一些默认配置,比如web.xml中的配置。数据库的配置等等。详细说明可以看这里:
http://docs.spring.io/spring-...


happyfish
2.4k 声望66 粉丝

[链接]