table of Contents
I. Background
In springboot project, we will inevitably introduce other jar package, if the jar package but there is a simple toolkit'd do well to install directly dependent on local and then add a reference on the line. So if the jar package when there is a need to start the project also need to be scanned to spring bean container approach just would not do anything, so I gave you talk about the following three ways at the start of the bean jar package scanning project
- Project Address https://github.com/ZiXinZhu/runnar_load
Second, prepare for work
Create a project consumer (behind several projects we will use this project to do the test)
1) Parent pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
2) child pom ... xml
<dependency>
<groupId>com.zzx</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.zzx</groupId>
<artifactId>spring-boot-start-zzx-datasource</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.zzx</groupId>
<artifactId>provider_scan</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
3)application.properties
server.port=8083
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3307/hibernate?characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=123456
zzx.url=jdbc:mysql://localhost:3307/hibernate?characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull&autoReconnect=true
zzx.username=root
zzx.password=123456
4) Start class
import com.zzx.provider.annotation.EnableProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableProvider
@ComponentScan({"com.zzx.provider_scan","com.zzx.consumer"})
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
5) Service Layer
import com.zzx.common.server.UserServer;
import com.zzx.provider.server.EnablePrint;
import com.zzx.provider_scan.po.BoyPO;
import com.zzx.provider_scan.service.BoyServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TestEnablePrintServer {
@Autowired(required = false)
EnablePrint enablePrint;
@Autowired(required = false)
UserServer userServer;
@Autowired
BoyServer boyServer;
public void enablePrintTest() {
enablePrint.toPrint();
}
public void printDataSource(){
userServer.printDataSource();
}
public List<BoyPO> getAllBoy(){
return boyServer.getAllBoy();
}
}
6) Control Layer
import com.zzx.consumer.server.TestEnablePrintServer;
import com.zzx.provider_scan.po.BoyPO;
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;
import java.util.List;
@RestController
@RequestMapping("/test")
public class TestEnablePrintController {
@Autowired
private TestEnablePrintServer server;
@GetMapping("/enable")
public void enablePrintTest(){
server.enablePrintTest();
}
@GetMapping("/start")
public void printDataSource(){
server.printDataSource();
}
@GetMapping("/scan")
public List<BoyPO> getAllBoy(){
return server.getAllBoy();
}
}
Third, the three ways to start the project at the time of scanning bean jar package
1. @ ComponentScan scanning
Provider_scan project space reasons we can not show out here
- Implementation
Add @ComponentScan ({ "com.zzx.provider_scan", "com.zzx.consumer"}) at the start of the class consumer
- advantage
We only need to provider_scan project to install locally and then adding a dependency in the consumer, and then add annotations in the master class will be able to scan at startup project to the project
- Shortcoming
A low self-word loaded to force large cattle will not be used this way, and when scanning with this annotation their projects will not be scanned, so take your own path packet writing projects come in so the above two scanning path.
2. Customize the way @Enable ****
- Implementation
1) the first to write a configuration class and then add annotations to scan all of the current path of the project in the configuration class above
@ComponentScan("com.zzx.provider")
@Component
public class EnableConfig {
}
2) Write a comment enable **** then came in above configuration class @import
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EnableConfig.class)
public @interface EnableProvider {
}
3) We need to install the project to use local, followed by Consumer rely on the project, added last @EnableProvider annotation on the class can start to scan the package at the start of the project
- advantage
With annotated scanned jar package, which package you need to scan a package which is not likely to cause conflict and omissions
- Shortcoming
Compared to last approach, the process is slightly complicated
3. Customize xxx-boot-start dependent manner
- Implementation
create spring-boot-satart-zzx- datasource project as a jar dependencies
1) We need to write a configuration class Properties.jar, configuration information
@Data
@ConfigurationProperties(prefix = "zzx")
public class Properties {
private String url;
private String password;
private String username;
}
2) Create a bean and add @ComponentScan assembly configuration information on the class notes to scan the entire project
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(Properties.class)
@ComponentScan("com.zzx.common")
@Slf4j
public class DataSourceConfiguration {
@Autowired
Properties properties;
@Autowired
DataSourceProperties dataSourceProperties;
@Bean
public void assembleBean() {
log.info("密码:"+properties.getPassword());
dataSourceProperties.setUrl(properties.getUrl());
dataSourceProperties.setPassword(properties.getPassword());
dataSourceProperties.setUsername(properties.getUsername());
log.info("数据库配置完毕!");
}
}
3) Create a new file META-INF files scanned DataSourceConfiguration spring.factories configuration classes for the folder under the resources
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.zzx.common.config.DataSourceConfiguration
4) When using the first spring-boot-satart-zzx-datasource to install a local program, then add items dependent upon the starting edge of the bean jar package also scanned to project in the consumer project
<dependency>
<groupId>com.zzx</groupId>
<artifactId>spring-boot-start-zzx-datasource</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
5) Before starting the project do not forget to add a spring-boot-satart-zzx-datasource application.properties consumer packages required in the configuration of the project as follows:
zzx.url=jdbc:mysql://localhost:3307/hibernate?characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull&autoReconnect=true
zzx.username=root
zzx.password=123456
- advantage
Convenient, practical and most importantly, the loading force!
- Shortcoming
Novice may use up a bit more difficult