搭建spring项目遇到的问题

搭建spring项目遇到的问题总结

spring项目工程结构目录如图:
在这里插入图片描述

测试类:

public class UserServiceTest extends AbstractTransactionalTestNGSpringContextTests {
private UserService userService;
@Autowired
public UserService setUserService() {
	return userService;
}
@Test
public void testHasMatchUser() {
	boolean b1 = userService.hasMatchUser("admin", "123456");
	boolean b2 = userService.hasMatchUser("admin", "1111");
	assertTrue(b1);
	assertTrue(!b2);
}

遇到 问题:
在这里插入图片描述
解决方式:
spring中有完整的test类,但是必须在测试类启动时,加载配置文件。如下:需要将以下代码放到测试类前面

@ContextConfiguration("classpath*:/smart-context.xml")

加载spring文件如下:smart-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-4.0.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

<!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
<context:component-scan base-package="com.smart.dao"/>
<context:component-scan base-package="com.smart.service"/>

<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
	destroy-method="close" 
	p:driverClassName="com.mysql.jdbc.Driver"
	p:url="jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8"
	p:username="root"
	p:password="123456" />

<!-- 配置Jdbc模板  -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
	p:dataSource-ref="dataSource" />
</beans>

遇到问题:
在这里插入图片描述
解决方式:serverTimezone=GMT%2B8

	jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8

启动spring项目报错:
在这里插入图片描述
异常描述:NoClassDefFoundError发生在JVM在动态运行时,根据你提供的类名,在classpath中找到对应的类进行加载,但当它找不到这个类时,就发生了java.lang.NoClassDefFoundError的错误

解决方式:同一端口号被占用 将80080修改为8080

        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>6.1.25</version>
            <configuration>
                <connectors>
                    <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                        <port>80080</port>
                        <maxIdleTime>60000</maxIdleTime>
                    </connector>
                </connectors>
                <!--配置web应用的上下文-->
                <contextPath>/bbs</contextPath>
                <!--在间隔时间内检查web应用是否有变化
                 0 表示禁用热部署 任何一个大于0 的数字都将表示启用-->
                <scanIntervalSeconds>0</scanIntervalSeconds>
            </configuration>
        </plugin>

** 发生可能的原因如下:

1.对应的Class在java的classpath中不可用
2.你可能用jar命令运行你的程序,但类并没有在jar文件的manifest文件中的classpath属性中定义
3.可能程序的启动脚本覆盖了原来的classpath环境变量
4.因为NoClassDefFoundError是java.lang.LinkageError的一个子类,所以可能由于程序依赖的原生的类库不可用而导致
5.检查日志文件中是否有java.lang.ExceptionInInitializerError这样的错误,NoClassDefFoundError有可能是由于静态初始化失败导致的
6.如果你工作在J2EE的环境,有多个不同的类加载器,也可能导致NoClassDefFoundError

NoClassDefFoundError解决示例
1.当发生由于缺少jar文件,或者jar文件没有添加到classpath,或者jar的文件名发生变更会导致java.lang.NoClassDefFoundError的错误。
2.当类不在classpath中时,这种情况很难确切的知道,但如果在程序中打印出
System.getproperty(“java.classpath”),可以得到程序实际运行的classpath
3.运行时明确指定你认为程序能正常运行的 -classpath 参数,如果增加之后程序能正常运行,说明原来程序的classpath被其他人覆盖了。
4.NoClassDefFoundError也可能由于类的静态初始化模块错误导致,当你的类执行一些静态初始化模块操作,如果初始化模块抛出异常,哪些依赖这个类的其他类会抛出NoClassDefFoundError的错误。如果你查看程序日志,会发现一些java.lang.ExceptionInInitializerError的错误日志,ExceptionInInitializerError的错误会导致java.lang.NoClassDefFoundError: Could not initialize class

猜你喜欢

转载自blog.csdn.net/weixin_39248420/article/details/88539376