org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为' UserServices'被定义为

时间:2017-07-04 04:20:15

标签: java spring hibernate model-view-controller

我该如何修复错误?可以告诉我吗? 我有登录过程..当我点击按钮登录不工作,我收到消息" org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为' UserServices'已定义"

//class for user dao
    public interface UserDao {

    User loginUser(String email);

    void simpanUser(User user);

    User cariUser(String email); 
}


//class for user dao impl 
@Repository public class UserDaoImpl implements UserDao {

    @Autowired
    private SessionFactory sessionFactory;

    @SuppressWarnings("unchecked")
    @Override
    public User loginUser(String email) {
        List<User> users = sessionFactory
                .getCurrentSession()
                .createQuery("select u from User u left join fetch u.userRoles pd where u.email = :email")
                .setParameter("email", email)
                .list();

        if (users.size() > 0) {
            return users.get(0);
        }
        return null;
    }

    @Override
    public void simpanUser(User user) {
        sessionFactory.getCurrentSession().save(user);
    }

    @Override
    public User cariUser(String email) {
        return sessionFactory.getCurrentSession().get(User.class, email);
    }

}

//class for user service
public interface UserService {

    User loginUser(String email, String password);

    void simpanUser(User user);

    User cariUser(String email);

}

//class for user service impl
@Service("UserServices")
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserDao userDao;

    @Override
    public User loginUser(String email, String password) {

        User user = userDao.loginUser(email);

        if (user != null) {
            if (BCrypt.checkpw(password, user.getPassword())) {
                return user;
            } else {
                return null;
            }
        }
        return null;
    }

    @Transactional
    @Override
    public void simpanUser(User user) {
        String hashed = BCrypt.hashpw(user.getPassword(), BCrypt.gensalt(12));

        LOGGER.debug("hasil generate bcrypt {}", hashed);
        UserRole userRole = new UserRole();
        userRole.setUser(user);

        Set<UserRole> userRoles = new HashSet<>();
        userRoles.add(userRole);
        user.setUserRoles(userRoles);
        user.setPassword(hashed);

        userDao.simpanUser(user);
    }

    @Override
    public User cariUser(String email) {
        User user = userDao.cariUser(email);
        if (user != null) {
            return user;
        }
        return null;
    }

}

//class for main class

public class App {
    private static ApplicationContext applicationContext;
    public static void main(String[] args) {
    applicationContext = new ClassPathXmlApplicationContext("classpath:spring-configuration.xml");

    /.codeee

    }
 public static UserService userService() {
    return (UserService) applicationContext.getBean("UserServices");
    }
  

弹簧-configuration.xml文件

<context:annotation-config />
    <tx:annotation-driven transaction-manager="transactionManager" />
    <context:component-scan base-package="com.belajar.springHibernate" />
    <context:property-placeholder location="classpath:jdbc.properties" />
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driver}"
    p:url="${jdbc.url}"
    p:username="${jdbc.username}"
    p:password="${jdbc.password}"
    />

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
    p:dataSource-ref="dataSource"
    p:configLocation="classpath:hibernate.cfg.xml"
    />

    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"
    p:sessionFactory-ref="sessionFactory"
    />

0 个答案:

没有答案