深圳网站设计比较好的公司有哪些,手机网站制作器,记事本网页制作教程,企业网站 域名注册很多时候我们需要在项目里面嵌入其他项目或者被其他项目嵌入#xff0c;如我们开发一个开源项目b#xff0c;用户需要在自己的项目a嵌入b项目#xff0c;使用b项目的功能#xff0c;而且要实现a项目工作最小化#xff0c;最好实现引入即用。
1.定义b项目的自定义配置 …
很多时候我们需要在项目里面嵌入其他项目或者被其他项目嵌入如我们开发一个开源项目b用户需要在自己的项目a嵌入b项目使用b项目的功能而且要实现a项目工作最小化最好实现引入即用。
1.定义b项目的自定义配置 (1).将你的application.properties或yml配置文件改个名如我改成了application-project.properties这样做是为了b项目被a项目嵌入引用的时候配置不会被覆盖和冲突。 (2).既然改了名就要让springboot知道你有这份配置可以用Configuration和PropertySource注解。 (3).将一些常用配置改成自定义名称配置避免a项目引用时冲突覆盖并且在配置文件中加载到springboot中如我这里的数据库配置把前缀改成了自定义。 2.配置自动注册类确保你的b项目的配置能在a项目中自动配置 (1).引入spring boot自动配置包 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-autoconfigure/artifactId/dependency (2). 定义自动配置类创建自动配置类编写一个或多个配置类来定义项目 A 提供的服务、控制器以及 FreeMarker 配置等让a项目在引用b项目的时候会自动加载这些配置,如我这里自定义了一些需要自动配置的组件。
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;Configuration
Import(DataSourceConfig.class)
ConditionalOnProperty(prefix project.datasource, name enabled, havingValue true, matchIfMissing true)
EnableConfigurationProperties(ProjectProperties.class)
MapperScan(basePackages com.missyoubug.easyjtest.dao)
public class ProjectAutoConfiguration {Bean(name bprojectSqlSessionFactory)public SqlSessionFactoryBean bprojectSqlSessionFactory(Qualifier(projectDataSource) DataSource dataSource) throws Exception {SqlSessionFactoryBean sessionFactory new SqlSessionFactoryBean();sessionFactory.setDataSource(dataSource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(classpath*:mapper/*.xml));return sessionFactory;}Beanpublic ContentNegotiatingViewResolver contentNegotiatingViewResolver(FreeMarkerViewResolver freeMarkerViewResolver) {ContentNegotiatingViewResolver resolver new ContentNegotiatingViewResolver();ListViewResolver viewResolvers new ArrayList();viewResolvers.add(freeMarkerViewResolver);resolver.setViewResolvers(viewResolvers);return resolver;}Beanpublic FreeMarkerViewResolver freeMarkerViewResolver() {FreeMarkerViewResolver resolver new FreeMarkerViewResolver();resolver.setPrefix();resolver.setSuffix(.ftl);resolver.setContentType(text/html;charsetUTF-8);resolver.setOrder(0); // 设置最高优先级return resolver;}Beanpublic FreeMarkerConfigurer freemarkerConfig() {FreeMarkerConfigurer configurer new FreeMarkerConfigurer();configurer.setTemplateLoaderPath(classpath:/templates/);return configurer;}
}
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;Component
ConfigurationProperties(prefix project.datasource)
public class ProjectProperties {private String url;private String driverClassName;private String username;private String password;// Getters and Setters
}(3).让Spring boot加载我们的自定义自动配置创建 spring.factories 文件在 src/main/resources/META-INF/ 目录下创建 spring.factories 文件并在其中声明自动配置类。 org.springframework.boot.autoconfigure.EnableAutoConfiguration\
com.missyoubug.easyjtest.config.ProjectAutoConfiguration
3.配置maven打包 (1).使用maven-compiler-plugin和maven-resources-plugin插件处理java源代码和静态资源打包注意不要用spring-boot-maven-plugin打包。 (2).在resource详细列明你需要打进去哪些资源这里踩坑很久开始以为只要有directorysrc/main/resources/directory就会把resources下的资源全打进去然而并不是这样需要详细列明。 (3).运行mvn clean install命令将b项目打成jar到本地maven仓库或者是远程仓库。 4.b项目类托管a项目让springboot扫描到b项目的资源文件
1 写一个配置类把b项目所有非静态的类都进行spring容器托管否则a项目不知道要接管b项目的这些类 2让spring知道你托管的这些类在spring.factortes中配置上面的bean托管类这样b项目被a项目嵌入引用时就会自动加载托管这些类。 5.a项目引用及使用被嵌入的b项目 (1).a项目maven引入b项目的maven坐标如我这里 dependencygroupIdcom.missyouBUG/groupIdartifactIdEasy-JTest/artifactIdversion0.0.1-SNAPSHOT/version/dependency (2如此上a项目就能完全嵌入b项目使用b项目的资源及功能如还有问题可以打出a项目详细spring加载过程日志看看有什么没有加载的在解决。
logging.level.org.springframework.boot.autoconfigureDEBUG
logging.level.org.springframework.jdbcDEBUGlogging.level.org.springframework.webDEBUG
logging.level.org.springframework.boot.autoconfigure.web.servletDEBUG
#com替换成自己的路径
logging.level.com.missyoubug.easyjtest.configDEBUG
这两天在搞一个帮助自测的开源项目踩了这里的坑所以记录下也希望各位开发者多发扬开源及写原创博客的优良传统这样自己遇到问题也最大可能的有前人写下的经验大家都才能更快的解决不要抄过去抄过来的保持严谨的开发态度。
对照参考开源Java快速自测工具可以调用系统内任意一个方法-CSDN博客