小编Ahm*_*shi的帖子

如何通过内置的JUnit测试自动连接服务

如何仅通过使用弹簧注释在测试类中自动装配服务

当我尝试以下错误时,在UserServiceImp类中使用@Service批注

2014-12-20 15:35:52错误TestContextManager:334-允许TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5af97850]准备测试实例[com.amsb.bariz.base.test]时捕获了异常。 UserTest @ 4520ebad] org.springframework.beans.factory.BeanCreationException:创建名称为'com.amsb.bariz.base.test.UserTest'的bean时出错:自动连接依赖项的注入失败;嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:public com.amsb.bariz.base.service.UserService com.amsb.bariz.base.test.UserTest.userService; 嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为[com.amsb.bariz.base.service.UserService]的合格Bean作为依赖项:期望至少有1个bean符合此依赖项的自动装配条件。依赖项注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

我的服务等级是

package com.amsb.bariz.base.service.imp;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.amsb.bariz.base.dao.UserDao;
import com.amsb.bariz.base.dao.UserRoleDao;
import com.amsb.bariz.base.entity.User;
import com.amsb.bariz.base.entity.UserRole;
import com.amsb.bariz.base.service.UserService;


@Service("userService")
public class UserServiceImp implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserRoleDao userRoleDao;

    public void register(User user) {

        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

        user.setPassword(passwordEncoder.encode(user.getPassword()));
        Calendar calendar = Calendar.getInstance();
        java.util.Date now = calendar.getTime();

        Date dateNow …
Run Code Online (Sandbox Code Playgroud)

java junit spring autowired spring-annotations

4
推荐指数
1
解决办法
7657
查看次数

标签 统计

autowired ×1

java ×1

junit ×1

spring ×1

spring-annotations ×1