一.自定义工程
1.pom配置
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.learning</groupId>
<artifactId>ip-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2.自定义service
package com.learning.service;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
/**
* @Author wangyouhui
* @Description
**/
public class IpService {
// 当前的request对象的注入工作由当前starter的工程提供自动装配
@Autowired
private HttpServletRequest httpServerRequest;
public void print(){
String ip = httpServerRequest.getRemoteAddr();
System.out.println("---------------------------------------"+ip);
}
}
3.定义一个AutoConfiguration类
package com.learning.autoconfig;
import com.learning.service.IpService;
import org.springframework.context.annotation.Bean;
/**
* @Author wangyouhui
* @Description TODO
**/
public class IpAutoConfiguration {
@Bean
public IpService ipCountService(){
return new IpService();
}
}
4.spring.factories
1.结构图
2.spring.factories文件配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.learning.autoconfig.IpAutoConfiguration
二.使用工程
1.pom文件配置
<?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>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
</parent>
<groupId>com.learning</groupId>
<artifactId>springboot-learning</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--引入自定义的starter-->
<dependency>
<groupId>com.learning</groupId>
<artifactId>ip-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--打jar包使用-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.controller测试类
package com.learning.controller;
import com.learning.service.IpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author wangyouhui
* @Description 测试接口
**/
@RestController
@RequestMapping("example")
@Slf4j
public class ExampleController {
@Autowired
private IpService ipService;
@GetMapping("/test")
public String get(){
ipService.print();
return "test";
}
}
3.效果示例
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/92335.html