新建一个maven spring boot项目中遇到的问题

2024-06-22 13:58

本文主要是介绍新建一个maven spring boot项目中遇到的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.问题:xxx.jar中没有主清单属性 

命令:make debug
结果:
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5033 -Dgrpc.port=6033 -Dserver.port=8033 -jar target/demo-1.0-SNAPSHOT.jar
Listening for transport dt_socket at address: 5033
target/demo-1.0-SNAPSHOT.jar中没有主清单属性
解决:查找资料发现MAVEN插件打包生成的jar包中的META-INF/MANIFEST.MF文件,没有设置主函数信息。配置pom.xml即可;
<plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
(如果直接配置则会报错→问题2)
现在配置为:


2.问题:<plugin>不能识别

命令:make install
结果:
mvn clean install -DskipTests
[INFO] Scanning for projects...
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[ERROR] Malformed POM /Users/********/Documents/workspace/pom.xml: Unrecognised tag: 'plugin' (position: START_TAG seen ...</repositories>\n\n    <plugin>... @38:13)  @ /Users/********/Documents/workspace/pom.xml, line 38, column 13
@
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]   
[ERROR]   The project com.mobike:demo:1.0-SNAPSHOT (/Users/********/Documents/workspace/pom.xml) has 1 error
[ERROR]     Malformed POM /Users/********/Documents/workspace/pom.xml: Unrecognised tag: 'plugin' (position: START_TAG seen ...</repositories>\n\n    <plugin>... @38:13)  @ /Users/********/Documents/workspace/pom.xml, line 38, column 13 -> [Help 2]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/ModelParseException

原因:未加上<build>

3.问题:target/xxx.jar找不到

命令:make debug
结果:

原因:检查target目录下的jar包名,发现与Makefile中写的不符合。将debug和run里面的名字改成对的即可。

4.问题:Consider defining a bean of type 'service.IUserInfoService' in your configuration.
命令:make debug
结果:
解决方案:加入config,并在UserController.java中加入注解 @SpringBootApplication( scanBasePackages = { "service" , "dao" , "config })
解决过程:
在工程中加入config文件,配置如下:
package config;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
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;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Repository;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;import javax.sql.DataSource;
import java.io.IOException;import java.sql.SQLException;import java.util.ArrayList;
import java.util.Arrays;import java.util.List;
@Slf4j
@Configuration
@MapperScan(basePackages = {"dao",},    annotationClass = Repository.class,    sqlSessionFactoryRef = SysUserAuthDaoConfig.SQL_SESSION_FACTORY_NAME)
public class SysUserAuthDaoConfig {public static final String SQL_SESSION_FACTORY_NAME = "opsSqlSessionFactory";@Value("${ops.database.username}")private String username;@Value("${ops.database.password}")private String password;@Value("${ops.database.url}")private String url;@Value("classpath:mybatis.userinfo/*.xml")private String mapperLocation;private DruidDataSource dataSource;private DataSourceTransactionManager transactionManager;private SqlSessionFactory sqlSessionFactory;@Autowired    private ResourcePatternResolver resourceResolver;public String getUsername() {return username;    }public void setUsername(String username) {this.username = username;    }public String getPassword() {return password;    }public void setPassword(String password) {this.password = password;    }public String getUrl() {return url;    }public void setUrl(String url) {this.url = url;    }public String getMapperLocation() {return mapperLocation;    }public void setMapperLocation(String mapperLocation) {this.mapperLocation = mapperLocation;    }public String[] getMapperLocations() {String[] mapperLocations = new String[1];        mapperLocations[0] = getMapperLocation();        return mapperLocations;    }@PostConstruct    public void init() {try {log.info("Init datasource: url: {}", url);            dataSource = new DruidDataSource();            dataSource.setDriverClassName("com.mysql.jdbc.Driver");            dataSource.setUrl(url);            dataSource.setUsername(username);            dataSource.setPassword(password);            dataSource.setTestWhileIdle(true);            dataSource.setTestOnReturn(false);            dataSource.init();transactionManager = new DataSourceTransactionManager();            transactionManager.setDataSource(dataSource);            log.info("Init done");        } catch (Throwable t) {log.error("Init error", t);        }}@PreDestroy    public void destroy() {try {log.info("Close {}", url);            dataSource.close();            log.info("Close {} done", url);        } catch (Throwable t) {log.error("Destroy error", t);        }}@Bean(name = SQL_SESSION_FACTORY_NAME)public SqlSessionFactory sqlSessionFactoryBean() throws Exception {if (sqlSessionFactory == null) {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();            org.apache.ibatis.session.Configurationconfig = new org.apache.ibatis.session.Configuration();            config.setMapUnderscoreToCamelCase(true);            sqlSessionFactoryBean.setConfiguration(config);            sqlSessionFactoryBean.setDataSource(dataSource);            List<Resource> resources = new ArrayList<>();            if (this.getMapperLocations() != null) {for (String mapperLocation : this.getMapperLocations()) {try {Resource[] mappers = resourceResolver.getResources(mapperLocation);                        resources.addAll(Arrays.asList(mappers));                    } catch (IOException e) {log.error("IOException", e);                        return null;                    }}}Resource[] arr = resources.toArray(new Resource[resources.size()]);            sqlSessionFactoryBean.setMapperLocations(arr);            sqlSessionFactory = sqlSessionFactoryBean.getObject();        }return sqlSessionFactory;    }@Bean("sysUserAuthJdbcTemplate")public JdbcTemplate jdbcTemplate() {return new JdbcTemplate(this.dataSource);    }@Bean("sysUserAuthDataSource")public DataSource getDatabase() throws SQLException {return dataSource;    }@Bean("sysUserAuthTransactionManager")public DataSourceTransactionManager transactionManager() {return transactionManager;    }}



一开始只加入了service,先扫service包,发现error变成了'dao.UserInfoMapper'
Description:
Field userInfoMapper in service.UserInfoServiceimpl required a bean of type 'dao.UserInfoMapper' that could not be found.
Action:
Consider defining a bean of type 'dao.UserInfoMapper' in your configuration.

可以看出实际上是因为Mapper没有被扫到,所以增加dao和config的扫描。
改成 @SpringBootApplication( scanBasePackages = { "service" , "dao" , "config })把包都扫到之后,编译通过。




这篇关于新建一个maven spring boot项目中遇到的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1084481

相关文章

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

JAVA中安装多个JDK的方法

《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

Java中Integer128陷阱

《Java中Integer128陷阱》本文主要介绍了Java中Integer与int的区别及装箱拆箱机制,重点指出-128至127范围内的Integer值会复用缓存对象,导致==比较结果为true,下... 目录一、Integer和int的联系1.1 Integer和int的区别1.2 Integer和in

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

nginx 负载均衡配置及如何解决重复登录问题

《nginx负载均衡配置及如何解决重复登录问题》文章详解Nginx源码安装与Docker部署,介绍四层/七层代理区别及负载均衡策略,通过ip_hash解决重复登录问题,对nginx负载均衡配置及如何... 目录一:源码安装:1.配置编译参数2.编译3.编译安装 二,四层代理和七层代理区别1.二者混合使用举例

JSONArray在Java中的应用操作实例

《JSONArray在Java中的应用操作实例》JSONArray是org.json库用于处理JSON数组的类,可将Java对象(Map/List)转换为JSON格式,提供增删改查等操作,适用于前后端... 目录1. jsONArray定义与功能1.1 JSONArray概念阐释1.1.1 什么是JSONA