前提:
我们在进行项目开发的过程中,经常能看到有一些重复的代码,这些代码可能是一些重复的配置啥的,所以为了方便使用者,我们经常需要将一些通用的配置配置成一个starter,这样用户只需要引入之后,就不需要再关心怎么去配置了,十分方便。
步骤一:
新建一个项目,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>
<groupId>com.bililee.starter</groupId>
<artifactId>firststarter</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>2.1.7.RELEASE</version>
<!--2.1.7.RELEASE-->
</parent>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.bililee.firststarter.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
main方法:
@SpringBootApplication
@ComponentScan("com.bililee.firststarter.**")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
注入下bean:
@Configuration
public class ThreadPoolConfiguration {
@Bean
@ConditionalOnClass(ThreadPoolExecutor.class)
public ThreadPoolExecutor MyThreadPool(){
System.out.println(123);
return new ThreadPoolExecutor(10, 10, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100));
}
}
在resource 下新建一个 META-INF 文件夹,并且新建一个 spring.factories:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.bililee.firststarter.config.ThreadPoolConfiguration
步骤二:
打包,这里是将jar包放到自己的本地仓库上
mvn install:install-file -DgroupId=com.bililee.starter -DartifactId=firststarter -Dversion=1.0.0 -Dpackaging=jar -Dfile=./target/firststarter-1.0.0.jar
步骤三:
在其他项目中引入下这个包,注入下,看下对应的bean是否注入成功,如果注入成功了的话,就说明已经成功了。(注意下面已经是一个新的项目的代码了哦)
<dependency>
<groupId>com.bililee.starter</groupId>
<artifactId>firststarter</artifactId>
<version>1.0.0</version>
</dependency>
然后注入下,在构造方法中调用:
@Service
public class TestServiceImpl implements TestService {
private final ThreadPoolConfiguration threadPoolConfiguration;
@Autowired
public TestServiceImpl(ThreadPoolConfiguration threadPoolConfiguration) {
this.threadPoolConfiguration = threadPoolConfiguration;
int size = this.threadPoolConfiguration.MyThreadPool().getCorePoolSize();
System.out.println("size222:" + size);
}
@Master
@Override
public void sayTest() {
System.out.println("sayTest");
}
}
然后看输出:
image.png
说明成功了
网友评论