1

При @Autowired репозитория UserRepository в классе UserServiceImpl получаю NPE. Все, что было на форуме перечитал. Ничего не помогло. Код:

User.java

package com.springmail.model;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "demo_users")
public class User implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@Column(name = "chat_id")
private long chatId;

protected User(){
}

public User(long chatId) {
    this.chatId = chatId;
}

@Override
public String toString() {
    return super.toString();
}

public long getId() {
    return id;
}

public long getChatId() {
    return chatId;
}
}

UserRepository.java

package com.springmail.dao;

import com.springmail.model.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends CrudRepository<User, Long> {}

Application.java

package com.springmail;

import com.springmail.controller.TelegramBot;
import com.springmail.dao.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@EnableJpaRepositories(basePackages = {"com.springmail.dao"})
@ComponentScan(basePackages = {"com.springmail"})
@EntityScan
@EnableTransactionManagement
public class Application implements CommandLineRunner{

@Autowired
UserRepository repository;

//Проверил первый ответ.
@Autowired
UserServiceImpl service;


public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

@Override
public void run(String... strings) throws Exception {
service.createUser(new User(123456789));

}
}

UserServiceImpl.java

package com.springmail.service;

import com.springmail.model.User;
import com.springmail.dao.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImp{

@Autowired
UserRepository repository;  //null

public void createUser(User newUser) {
    repository.save(newUser);  //NPE
}

public void deleteUser(User user) {
    repository.delete(user);
}

public void findUser(User user) {
    repository.findOne(user.getId());
}

public Iterable<User> findAllUsers() {
    return repository.findAll();
}

public void deleteAll() {
    repository.deleteAll();
}

}

1 ответ 1

0

Всё из-за того, что ты создаёшь UserServiceImp руками через new оператор. Тебе надо его достать просто из spring контейнера(через Autowired например)

1
  • Да. Вы правы) Спасибо)
    – Naurion
    1 мар 2018 в 13:53

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.