Kimisme
2016-11-01 14:07
采纳率: 100%
浏览 4.9k
已采纳

spring 加载applicationContext.xml文件报错

项目结构如下图
图片说明
控制台输出的错误如下

 信息: Refreshing org.springframework.context.support.GenericApplicationContext@7a61053: startup date [Tue Nov 01 21:56:43 CST 2016]; root of context hierarchy
十一月 01, 2016 9:56:43 下午 org.springframework.context.support.GenericApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginLogDao' defined in file [D:\CodeLocation\Java\My_Learn\spring_chapter2\bin\com\baobaotao\dao\LoginLogDao.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
十一月 01, 2016 9:56:43 下午 org.springframework.test.context.TestContextManager prepareTestInstance
严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@8554f0c] to prepare test instance [com.baobaotao.service.TestUserService@75813820]
java.lang.IllegalStateException: Failed to load ApplicationContext

applicationContext.xml代码如下

 <?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

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

    <!-- 定义一个使用DBCP实现的数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" 
        p:driverClassName="com.mysql.jdbc.Driver"
        p:url="jdbc:mysql://localhost:3306/sampledb" 
        p:username="root"
        p:password="123456" />

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

    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="dataSource" />

    <!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 -->
        <aop:config proxy-target-class="true">
        <aop:pointcut id="serviceMethod"
            expression=" execution(* com.baobaotao.service..*(..))" />
        <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
    </aop:config>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>

</beans>

搞了好久一直没解决,如果还需要哪些代码段的话, 跟我说下,谢谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • A130_0703 2016-11-01 14:51
    已采纳

    缺少相应的包:java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException

    点赞 打赏 评论
  • ZealotTL 2016-11-01 14:21

    Error creating bean with name 'loginLogDao' ,贴loginLogDao文件

    点赞 打赏 评论
  • Kimisme 2016-11-01 14:34

    loginLogDao.java代码如下

     package com.baobaotao.dao;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Repository;
    
    import com.baobaotao.domain.LoginLog;
    
    @Repository
    public class LoginLogDao {
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
        public void insertLoginLog(LoginLog loginLog) {
            String sqlStr = "INSERT INTO t_login_log(user_id,ip,login_datetime) "
                    + "VALUES(?,?,?)";
            Object[] args = { loginLog.getUserId(), loginLog.getIp(),
                              loginLog.getLoginDate() };
            jdbcTemplate.update(sqlStr, args);
        }
    }
    

    UserService.java代码如下

     package com.baobaotao.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import com.baobaotao.dao.LoginLogDao;
    import com.baobaotao.dao.UserDao;
    import com.baobaotao.domain.LoginLog;
    import com.baobaotao.domain.User;
    
    //将UserService标注为一个服务层的Bean
    @Service
    public class UserService {
        @Autowired
        private UserDao userDao;
        @Autowired
        private LoginLogDao loginLogDao;
    
        public boolean hasMatchUser(String username,String password){
            int matchCount = userDao.getMatchCount(username, password);
            return matchCount>0;
        }
        public User findUserByUserName(String username){
            return userDao.findUserByUserName(username);
        }
        public void loginSuccess(User user){
            user.setCredits(5+user.getCredits());
            LoginLog loginLog=new LoginLog();
            loginLog.setUserId(user.getUserId());
            loginLog.setIp(user.getLastIp());
            loginLog.setLoginDate(user.getLastVisit());
            userDao.updateLoginInfo(user);
            loginLogDao.insertLoginLog(loginLog);
        }
    }
    
    
    点赞 打赏 评论
  • Kimisme 2016-11-02 00:54

    昨天后来没解决,就关机了。然后早上把电脑重新开机,还是昨天的程序,竟然可以运行了。郁闷了。不过还是谢谢大家

    点赞 打赏 评论

相关推荐 更多相似问题