mybatis-plus和spring-boot的bean加载问题

发布于 2022-09-12 04:46:48 字数 13570 浏览 9 评论 0

我取消了spring-boot的自动配置,改为手动配置
ImportAutoConfiguration

package com.esunward.plisp;
import com.esunward.framework.utils.SpringContextUtil;
import org.springframework.context.annotation.Import;
/**
 * 手动导入配置类,提到启动速度
 */
@Import({
  org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration.class,
  org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class,
  com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration.class,
  SpringContextUtil.class,
  org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration.class,
  org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration.class,
  org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration.class,
  org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration.class,
  org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration.class,
  org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration.class,
  org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.class,
  de.codecentric.boot.admin.client.config.SpringBootAdminClientAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.audit.AuditAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.audit.AuditEventsEndpointAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.beans.BeansEndpointAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.system.DiskSpaceHealthIndicatorAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.mongo.MongoHealthIndicatorAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.redis.RedisHealthIndicatorAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration.class,
  org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.elasticsearch.ElasticsearchHealthIndicatorAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.health.HealthEndpointAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.condition.ConditionsReportEndpointAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.context.ShutdownEndpointAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.context.properties.ConfigurationPropertiesReportEndpointAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration.class,
  org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.endpoint.jmx.JmxEndpointAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration.class,
  org.springframework.cloud.autoconfigure.LifecycleMvcEndpointAutoConfiguration.class,
  org.springframework.cloud.autoconfigure.WritableEnvironmentEndpointAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.env.EnvironmentEndpointAutoConfiguration.class,
  org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.info.InfoContributorAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.info.InfoEndpointAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.jolokia.JolokiaEndpointAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.logging.LogFileWebEndpointAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.logging.LoggersEndpointAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.management.HeapDumpWebEndpointAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.management.ThreadDumpEndpointAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.metrics.MetricsEndpointAutoConfiguration.class,
  org.springframework.cloud.autoconfigure.RefreshAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.metrics.jdbc.DataSourcePoolMetricsAutoConfiguration.class,
  org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration.class,
  org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration.class,
  org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.metrics.web.client.RestTemplateMetricsAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.metrics.web.servlet.WebMvcMetricsAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.metrics.web.tomcat.TomcatMetricsAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.scheduling.ScheduledTasksEndpointAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.trace.http.HttpTraceAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.trace.http.HttpTraceEndpointAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.web.mappings.MappingsEndpointAutoConfiguration.class,
  org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration.class,
  org.springframework.boot.autoconfigure.aop.AopAutoConfiguration.class,
  org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration.class,
  org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration.class,
  org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration.class,
  org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration.class,
  org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration.class,
  org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration.class,
  org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration.class,
  org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration.class,
  org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration.class,
  org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration.class,
  org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration.class,
  org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration.class,
  org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration.class,
  org.springframework.cloud.client.CommonsClientAutoConfiguration.class,
  org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientAutoConfiguration.class,
  org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration.class,
  org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration.class,
  org.springframework.cloud.client.loadbalancer.AsyncLoadBalancerAutoConfiguration.class,
  org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration.class,
  org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration.class,
  org.springframework.cloud.commons.util.UtilAutoConfiguration.class,
  org.springframework.cloud.netflix.hystrix.HystrixAutoConfiguration.class,
  org.springframework.cloud.netflix.ribbon.eureka.RibbonEurekaAutoConfiguration.class,
  org.springframework.cloud.openfeign.ribbon.FeignRibbonClientAutoConfiguration.class,
  org.springframework.cloud.openfeign.FeignAutoConfiguration.class,
  org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration.class,
})
public class ImportAutoConfiguration {
}

启动类配置代码,主要是导入ImportAutoConfiguration

package com.esunward.plisp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import com.esunward.framework.utils.IPUtils;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;

@SpringBootConfiguration
@Import(ImportAutoConfiguration.class)
@ComponentScan(basePackages = {"com.esunward.framework.configuration", "com.esunward.plisp"},
  excludeFilters = {
  @ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
  @ComponentScan.Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class)})
public class PlispSystemApplication {
  private static final String projectName = "plisp-service-system";
  private static ApplicationContext ctx;

  public static void main(String[] args) {
    long start = System.currentTimeMillis();
    ctx = SpringApplication.run(PlispSystemApplication.class, args);
    // 获取tomcat server
    TomcatServletWebServerFactory tomcatServletWebServerFactory = (TomcatServletWebServerFactory) ctx
      .getBean("tomcatServletWebServerFactory");
    // 获取启动地址和端口
    int port = tomcatServletWebServerFactory.getPort();
    String contextPath = tomcatServletWebServerFactory.getContextPath();
    String thisUrl = IPUtils.getThisUrl(port, contextPath);
    long end = System.currentTimeMillis();
    // 输出地址和端口
    System.out.println(projectName + "完成启动共使用了:" + (end - start) + "ms");
    System.out.println(thisUrl);
  }
}

配置文件如下

spring.profiles.active=litong
#spring.profiles.active=dev
server.port=8762
eureka.instance.preferIpAddress=true

spring.main.banner-mode=off
spring.application.name=plisp-service-system

logging.level.com.esunward=debug


mybatis-plus.mapper-locations=classpath*:mapper/*.xml
mybatis-plus.type-aliases-package=com.esunward
mybatis-plus.global-config.refresh=true
mybatis-plus.global-config.dbconfig.columnLike=true
mybatis-plus.global-config.dbconfig.logic-delete-value=1
mybatis-plus.global-config.dbconfig.logic-not-delete-value=0


spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.initialSize=1
spring.datasource.minIdle=1
spring.datasource.maxActive=200
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=select 1
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,wall,log4j
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
spring.datasource.logSlowSql=true
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.urlPattern=/datasource-druid/*
spring.datasource.druid.stat-view-servlet.loginUsername
spring.datasource.druid.stat-view-servlet.loginPassword

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
spring.boot.admin.client.instance.prefer-ip=true

但是启动过程中却出现了下面的错误

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'areaServiceProvider': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'areaService': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.esunward.plisp.system.mapper.AreaMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

AreaMapper代码,添加了@Mapper注解

package com.esunward.plisp.system.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import com.esunward.framework.common.CommonBaseMapper;
import com.esunward.framework.model.DataMap;
import com.esunward.plisp.system.model.Area;

@Mapper
public interface AreaMapper extends CommonBaseMapper<Area> {

}

问题是原因我清楚
myatis-plus还没有配置成功,但是@Mapper注解被其他注解处理器扫描到要放到bean容器中,
如何配置可以确保是在mybatis-plus配置成功后再配置mapper接口

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪漫人生路 2022-09-19 04:46:48

@Order注解可能有用

爱人如己 2022-09-19 04:46:48

有代码没,打个包,这个有点意思,可以看看

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文