springboot整合elaticjob报错

在这里插入图片描述
springboot整合elaticjob报错 Invocation of init method failed; nested exception is java.lang.NullPointerException

application.properties文件配置
elaticjob.zookeeper.server-lists=10.10.80.51:2181
elaticjob.zookeeper.namespace=my-project
server.port=8766
spring.application.name=scheduler-service

zookeeper注册中心

spring.elasticjob.serverList = 10.10.80.51:2181
spring.elasticjob.namespace = elastic-job-lite-springboot

stockJob.cron = 0/5 * * * * ?
stockJob.shardingTotalCount = 2
stockJob.shardingItemParameters = 0=Chengdu0,1=Chengdu1

stockSimpleJob.java文件
package com.example.elasticjob.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import org.springframework.stereotype.Component;

@Component
public class StockSimpleJob implements SimpleJob {

@Override
public void execute(ShardingContext shardingContext) {
    System.out.println(String.format("------Thread ID: %s, 任务总片数: %s, " +
                    "当前分片项: %s.当前参数: %s,"+
                    "当前任务名称: %s.当前任务参数: %s"
            ,
            Thread.currentThread().getId(),
            shardingContext.getShardingTotalCount(),
            shardingContext.getShardingItem(),
            shardingContext.getShardingParameter(),
            shardingContext.getJobName(),
            shardingContext.getJobParameter()

    ));

}

}
config配置类JobRegistryCenterConfig
package com.example.elasticjob.config;

import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnExpression("’${spring.elasticjob.serverList}’.length() > 0") //判断是否配置了zookeeper 地址
public class JobRegistryCenterConfig {

@Bean(initMethod = "init")
public ZookeeperRegistryCenter regCenter(@Value("${spring.elasticjob.serverList}") final String serverList, @Value("${spring.elasticjob.namespace}") final String namespace) {
    return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
}

}
SpringJobScheduler.java

package com.example.elasticjob.config;

import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.example.elasticjob.job.StockSimpleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
class StockJobConfig {
@Autowired
private JobRegistryCenterConfig jobRegistryCenterConfig;
@Autowired
private ZookeeperRegistryCenter regCenter;
public StockJobConfig() {
}
@Bean(initMethod = “init”)
public JobScheduler simpleJobScheduler(final StockSimpleJob simpleJob, @Value(" s t o c k J o b . c r o n " ) f i n a l S t r i n g c r o n , @ V a l u e ( " {stockJob.cron}") final String cron, @Value(" stockJob.cron")finalStringcron,@Value("{stockJob.shardingTotalCount}") final int shardingTotalCount,
@Value("${stockJob.shardingItemParameters}") final String shardingItemParameters) {
return new SpringJobScheduler(simpleJob, regCenter, simpleJobConfigBuilder(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters));
}
/**
*@Description 任务配置类
*/
private LiteJobConfiguration simpleJobConfigBuilder(final Class<? extends SimpleJob> jobClass,
final String cron,
final int shardingTotalCount,
final String shardingItemParameters){

    return LiteJobConfiguration
            .newBuilder(
                    new SimpleJobConfiguration(
                            JobCoreConfiguration.newBuilder(
                                    "my-jobName",cron,shardingTotalCount)
                                    .shardingItemParameters(shardingItemParameters).jobParameter("job-参数")
                                    .build()
                            ,jobClass.getCanonicalName()
                    )
            )
            .overwrite(true)
            .build();
}
/**
 * 动态添加
 * @param jobClass
 * @param cron
 * @param shardingTotalCount
 * @param shardingItemParameters
 */
public void addSimpleJobScheduler(final Class<? extends SimpleJob> jobClass,
                                  final String cron,
                                  final int shardingTotalCount,
                                  final String shardingItemParameters){
    JobCoreConfiguration coreConfig = JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).build();
    SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(coreConfig, jobClass.getCanonicalName());
    JobScheduler jobScheduler = new JobScheduler(regCenter, LiteJobConfiguration.newBuilder(simpleJobConfig).build());
    jobScheduler.init();
}

}

pom

<?xml version="1.0" encoding="UTF-8"?>


4.0.0

org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE


com.example.elasticjob
elasticjob
0.0.1-SNAPSHOT
elasticjob
Demo project for Spring Boot

<java.version>1.8</java.version>



org.springframework.boot
spring-boot-starter-web



com.github.kuhn-he
elastic-job-lite-spring-boot-starter
2.1.5



com.dangdang
elastic-job-lite-core
2.1.5


com.dangdang
elastic-job-lite-spring
2.1.5

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

麻烦大神帮我看下 到底是什么错误??!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 很抱歉,我是一名语言模型AI,无法查看您的具体报错信息。但是,一般来说,Spring Boot整合MyBatis报错可能是因为配置文件中的某些参数设置不正确,或者依赖库版本不兼容等原因。建议您检查配置文件和依赖库版本,或者提供具体的报错信息,以便更好地帮助您解决问题。 ### 回答2: SpringBoot是一个流行的轻量级Java Web框架,MyBatis是一个开源的持久层框架。将它们整合起来可以使开发变得更加便捷。然而在实际使用过程中可能会遇到一些报错,在整合过程中也会有一些细节需要注意。 首先确保在pom.xml中添加了所需的依赖,包括SpringBoot、MyBatis和MySQL驱动等。同时也需要添加对应的插件,如MyBatis Generator。 其次在application.properties文件中配置数据库信息和MyBatis的相关属性。比如: ``` #数据库配置 spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver #MyBatis相关属性 mybatis.mapper-locations=classpath*:mappers/*.xml mybatis.type-aliases-package=com.example.domain mybatis.configuration.map-underscore-to-camel-case=true ``` 其中mapper-locations指定了mapper.xml文件的路径,type-aliases-package指定了实体类的包名,configuration.map-underscore-to-camel-case指定是否开启驼峰命名转换。 接着在Mapper类和对应的mapper.xml文件中编写SQL语句和映射关系。注意命名规范要一致,并且在mapper.xml文件中指定命名空间。比如: ``` public interface UserMapper { @Select("SELECT * FROM user WHERE id = #{id}") User selectById(Integer id); } ``` ``` <mapper namespace="com.example.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.domain.User"> <id column="id" property="id" /> <result column="username" property="username" /> <result column="password" property="password" /> </resultMap> <select id="selectById" resultMap="BaseResultMap"> SELECT * FROM user WHERE id = #{id} </select> </mapper> ``` 最后在Controller中调用Mapper方法即可。如果在整合过程中遇到报错,可以根据报错信息定位到相应的位置进行调试。常见的报错原因包括依赖版本不兼容、配置信息错误、映射关系错误等。 综上所述,SpringBoot整合MyBatis需要注意依赖和配置以及Mapper和mapper.xml的编写,同时也需要注意错误定位和排查。 ### 回答3: Spring Boot 是一个开发微服务框架的利器,在开发中常常需要使用到持久层框架 Mybatis 来实现数据的 CRUD。但是,在 Spring Boot 整合 Mybatis 的过程中,可能会遇到报错情况。接下来,将针对 Spring Boot 整合 Mybatis 报错问题,进行详细的分析和解答。 常见报错及解决方法: 1. No qualifying bean of type ‘org.apache.ibatis.session.SqlSession’: 这个错误通常是因为没能正确配置 Mybatis 的 SqlSession 的 bean 。解决方法如下: - 在 application.properties 文件里配置 mybatis.configuration 的信息,如下所示 ``` mybatis.configuration.cache-enabled=true mybatis.configuration.map-underscore-to-camel-case=true ``` - 确认如下扫描路径 ``` @MapperScan(basePackages = "你的 mapper 目录") @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 2. Mapper 和 Service 的依赖注入失败: 在 Spring Boot 加载时,自动会读取 Mybatis 相关的 Java 文件,然后将其注册为 Mapper 和 Service。如果出现了该问题,很有可能是因为 Mybatis 的 scan 配置方式不正确,解决方法如下: - 确认如下扫描路径 ``` @MapperScan(basePackageClasses = {XXXMapper.class}) @SpringBootApplication public class Application { public static void main(String args[]) { SpringApplication.run(Application .class, args); } } ``` 3. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘XxxController’: Unsatisfied dependency expressed through field ‘XxxService’: 这个错误的原因是因为 XxxController 无法找到 XxxService 的 bean 。而出现这种情况的原因可以是 XxxController 和 XxxService 在不同的包下。解决方法如下: - 在XxxService 的实现类上添加 @Service 注解,告诉 Spring 这个类是 Service ``` @Service public class XxxServiceImpl implements XxxService { } ``` - 在 XxxController 类中添加 @RestController 和 @RequestMapping 注解,告诉 Spring 这是 Controller 类 ``` @RestController @RequestMapping("/api") public class XxxController { @Autowired private XxxService xxxService; } ``` 综上所述,Spring Boot 整合 Mybatis 报错问题通常是一些常见配置问题所致。只要在配置时注意细节,充分理解异常信息,问题往往可以轻松得到解决。通过学习和掌握这些技巧,我们可以快速地进行 Spring Boot 整合 Mybatis 的开发工作,并有效地提高自己的开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值