Çözüldü Java Springboot autowired işareti çalışmıyor

Bu konu çözüldü olarak işaretlenmiştir. Çözülmediğini düşünüyorsanız konuyu rapor edebilirsiniz.

706111

Hectopat
Katılım
28 Ağustos 2023
Mesajlar
6.020
Makaleler
1
Çözümler
29
Arkadaşlar merhaba. Kısaca durumu şöyle anlatayım. Service sınıfında, repository interface instance edip, @autowired annotasyonu ile Spring'i otomatik bağlamasını istedim.
Belki repository interface'sini bir sınıf implemente edip kullanabilirdim, denemedim ama @autowired daha kullanışlı gibi duruyor.

Uygulamanın verdiği hata bu;
java.lang.NullPointerException: Cannot invoke "com.tensa.proje.repository.TestModelRepository.save(Object)" because "this.testModelRepository" is null

Ama veritabanında veriler olmadan tabloları oluşturuyor.

Kodlarım budur.

Model Sınıfları;
Java:
package com.tensa.proje.model;

import java.util.List;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TestModel {
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
 
    private String name;
 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "testModel")
    private List<TestModel2> testModel2s;

}
Java:
package com.tensa.proje.model;

import java.util.List;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TestModel2 {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
 
    private String name;
 
    @ManyToOne
    @JoinColumn(name = "testModelID")
    private TestModel testModel;
}

Repository interfaceleri;
Java:
package com.tensa.proje.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.tensa.proje.model.TestModel;

@Repository
public interface TestModelRepository extends JpaRepository<TestModel, Integer> {

}
Java:
package com.tensa.proje.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.tensa.proje.model.TestModel2;

@Repository
public interface TestModel2Repository extends JpaRepository<TestModel2, Integer> {

}

Service sınıfı
Java:
package com.tensa.proje.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tensa.proje.model.TestModel;
import com.tensa.proje.repository.TestModelRepository;

@Service
public class TestModelService {

    @Autowired
    TestModelRepository testModelRepository;
 
 
    public void save(TestModel testModel) {
    
        testModelRepository.save(testModel);
    }
}

Main sınıfı
Java:
package com.tensa.proje;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.tensa.proje.model.TestModel;
import com.tensa.proje.model.TestModel2;
import com.tensa.proje.service.TestModelService;

@SpringBootApplication
public class ProjeApplication implements CommandLineRunner {

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

    @Override
    public void run(String... args) throws Exception {
    
        TestModel2 testModel21 = new TestModel2();
    
        testModel21.setName("1. Test Model 2");
    
        TestModel2 testModel22 = new TestModel2();
    
        testModel22.setName("2. Test Model 2");
    
        List<TestModel2> list = new ArrayList<TestModel2>();
    
        list.add(testModel22);
        list.add(testModel21);
    
    
        TestModel testModel = new TestModel();
    
        testModel.setName("Test Model");
    
        testModel.setTestModel2s(list);
    
        TestModelService modelService = new TestModelService();
    
        modelService.save(testModel);
    
    }

}
 
Son düzenleyen: Moderatör:
Çözüm
NullPointerException hatası almanızın nedeni, TestModelService sınıfını manuel olarak oluşturmanız ve bu nedenle Spring'in Dependency Injection kullanamamasıdır. TestModelService'i Spring konteyneri tarafından yönetilen bir bileşen olarak kullanmanız gerekiyor. CommandLineRunner içinde TestModelService'i Spring tarafından yönetilen bir bean olarak kullanmalısınız.
NullPointerException hatası almanızın nedeni, TestModelService sınıfını manuel olarak oluşturmanız ve bu nedenle Spring'in Dependency Injection kullanamamasıdır. TestModelService'i Spring konteyneri tarafından yönetilen bir bileşen olarak kullanmanız gerekiyor. CommandLineRunner içinde TestModelService'i Spring tarafından yönetilen bir bean olarak kullanmalısınız.
 
Çözüm
NullPointerException hatası almanızın nedeni, TestModelService sınıfını manuel olarak oluşturmanız ve bu nedenle Spring'in Dependency Injection kullanamamasıdır. TestModelService'i Spring konteyneri tarafından yönetilen bir bileşen olarak kullanmanız gerekiyor. CommandLineRunner içinde TestModelService'i Spring tarafından yönetilen bir bean olarak kullanmalısınız.
Hocam dediğinizi anladım biraz ama örnek kod atabilir misiniz?
Not: Hocam teşekkürler halletim. Component ile işaretlenen ayrı bir sınıf yazıp, Sprign uygulamasını çalıştırdım ve çalıştı.

Teşekkür ettim. Çok önemli bir bilgi.
 
Son düzenleme:
Hocam dediğinizi anladım biraz ama örnek kod atabilir misiniz?
Not: Hocam teşekkürler halletim. Component ile işaretlenen ayrı bir sınıf yazıp, Sprign uygulamasını çalıştırdım ve çalıştı.

Teşekkür ettim. Çok önemli bir bilgi.
Java:
package com.tensa.proje;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.tensa.proje.model.TestModel;
import com.tensa.proje.model.TestModel2;
import com.tensa.proje.service.TestModelService;

@SpringBootApplication
public class ProjeApplication implements CommandLineRunner {

    @Autowired
    private TestModelService modelService;

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

    @Override
    public void run(String... args) throws Exception {
    
        TestModel2 testModel21 = new TestModel2();
        testModel21.setName("1. Test Model 2");

        TestModel2 testModel22 = new TestModel2();
        testModel22.setName("2. Test Model 2");

        List<TestModel2> list = new ArrayList<>();
        list.add(testModel22);
        list.add(testModel21);

        TestModel testModel = new TestModel();
        testModel.setName("Test Model");
        testModel.setTestModel2s(list);

        modelService.save(testModel);
    }
}
 

Technopat Haberler

Yeni konular

Geri
Yukarı