Backend Language/Spring boot(Java)

ERROR 9912 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter / CrudRepository์™€ @Autowired ์‚ฌ์šฉ ์‹œ Error ๋ฐœ์ƒ

chaerlo127 2021. 11. 8. 16:59
728x90

โ€ปํ•ด๊ฒฐ ๋ฐฉ๋ฒ•๋ถ€ํ„ฐ ๋ณด๊ณ  ์‹ถ์€ ์‚ฌ๋žŒ๋“ค์€ ๋ฐ”๋กœ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‚ด๋ ค๊ฐ€์„ธ์š”!!โ€ป

 

ํ•™๊ต ์ˆ˜์—…์—์„œ ์ง„ํ–‰ํ•˜๋Š” Spring boot ๊ณผ์ œ ์‹œํ–‰ ์‹œ, 

์ด์™€ ๊ฐ™์€ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค.

 

์ง„์งœ,, ์ด๊ฑฐ 3์‹œ๊ฐ„ ๋„˜๊ฒŒ ์˜ค๋ฅ˜ ํ•ด๊ฒฐํ•˜๋Š๋ผ ํ™”๋‚˜ ์ฃฝ๋Š”์ค„ ์ฝ”๋”ฉํ•˜๋ฉด์„œ ์š•์ด ์ œ์ผ ๋งŽ์ด ๋Š˜์—ˆ์Œ;;;

 

 

2021-11-08 16:02:17.715 ERROR 9912 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field cart in com.example.PetAppMidterm.Application required a bean of type 'com.example.PetAppMidterm.PetRepository' that could not be found.

        - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.example.PetAppMidterm.PetRepository' in your configuration.

 

@Autowried๋ฅผ ์ƒ์„ฑํ•œ method๋ฅผ ์ œ์™ธํ•˜๊ณ  ์‹œํ–‰์„ ํ•ด๋ณด๋‹ˆ, ๋ฌธ์ œ๊ฐ€ ์—†๋Š” ๊ฑธ ๋ด์„œ ์ด๋ถ€๋ถ„์ด ์ž˜๋ชป ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค.

๋˜ํ•œ, Error ๋ถ€๋ถ„์„ ํ™•์ธํ•ด๋ณด๋ฉด, bean์„ ํ™•์ธํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

๊ต์ˆ˜๋‹˜์ด ์•Œ๋ ค์ฃผ์‹œ๋Š” ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ๋”ฐ๋ผํ•˜๊ณ , ๊ฐœ๋…์„ ์•Œ์ง€ ๋ชปํ•˜๋‹ˆ, Error๊ฐ€ ๋‚˜๋„ ์˜ค๋žซ๋™์•ˆ ํ•ด๊ฒฐํ•˜๊ณ  ์žˆ๋‹ค.

 

 

Spring boot์˜ ์ดˆ์งœ์ธ ๋‚˜๋กœ์„œ๋Š” ์•„๋ฌด๊ฒƒ๋„ ๋ชจ๋ฅด๋Š” ๋ฌด์ง€์˜ ์ƒํƒœ์ธ ์ง€์‹์œผ๋กœ ๊ตฌ๊ธ€ ์„œ์น˜๋ฅผ ํ†ตํ•ด ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ•˜์˜€๋‹ค.

 

๋งŽ์€ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ด์„œ,

@repository๋ฅผ CrudRepository ์ƒ์„ฑํ•œ class์— ๋„ฃ๊ธฐ,

CrudRepository ์ƒ์„ฑํ•œ class์— @Component ๋„ฃ๊ธฐ,

@EnableJpaRepositories, @ComponentScan, @EntityScan

๋“ฑ์„ ํ•ด๋ณด์•˜์ง€๋งŒ, ๋ชจ๋‘ ๋‹ค ์‹คํŒจ!!

 

์ด ํ›„ ํ•˜๋‚˜์˜ site๋ฅผ ํ™•์ธํ•ด์„œ ํ•ด๊ฒฐ์„ ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. 

 

์ผ๋‹จ, ๋‚˜์˜ ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•ด๋ณด์ž.

 

//์ด ์ฝ”๋“œ๋Š” ํ•„์š”ํ•œ ์„ค๋ช…์„ ์ œ์™ธํ•˜๊ณ  ์ง€์›Œ๋†“์€ ์ฝ”๋“œ์ด๋‹ค.
package com.example.PetAppMidterm;


import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@DiscriminatorColumn(
    discriminatorType = DiscriminatorType.STRING,
    name = "pet_type",
    columnDefinition = "CHAR(5)"
)

public abstract class Pet { 

    @Id @GeneratedValue
    Long id;
    String name;
    
    // ๋‚˜๋จธ์ง€ ์ฝ”๋“œ ์ƒ๋žต
  
}

 

package com.example.PetAppMidterm;



import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;




@Entity
@DiscriminatorValue("cat")
public class Cat extends Pet implements Groomable, Runnable{

//์ฝ”๋“œ ์ƒ๋žต
}

----------------------------------------

package com.example.PetAppMidterm;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;


@Entity
@DiscriminatorValue("dog")
public class Dog extends Pet implements Runnable{

//์ฝ”๋“œ ์ƒ๋žต

}

 

package com.example.PetAppMidterm;


import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;


@Repository
public interface PetRepository extends CrudRepository<Pet, Long>{//Repository Pattern Interface
    
}

 

<ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•>

 

๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€, Table์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.

ํ•œ ๋ฒˆ๋„ run์„ ํ•œ ์ ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— table์ด ์ƒ์„ฑ์ด ๋˜์ง€ ์•Š์•˜๋˜ ๊ฒƒ์ด๋‹ค.

์ด์—, table์„ ์ƒ์„ฑํ•˜๋Š” @Table์„ Pet Class์— ๋„ฃ์–ด์•ผ ํ•œ๋‹ค.

 

package com.example.PetAppMidterm;


import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;


@Table
@Entity
@DiscriminatorColumn(
    discriminatorType = DiscriminatorType.STRING,
    name = "pet_type",
    columnDefinition = "CHAR(5)"
)

public abstract class Pet { 
   
    @Id @GeneratedValue
    Long id;
    String name;
    
    //๋‚˜๋จธ์ง€ ์ฝ”๋“œ ์ƒ๋žต
    
    }

 

@Entity ์œ„์— ์žˆ๋Š” @Table ์ถ”๊ฐ€

 

+++ ์ถ”๊ฐ€๋กœ ๊ต์ˆ˜๋‹˜์€ CrudRepository๋ฅผ extends ํ•  ๋•Œ, @Repository๋ฅผ ์ ์ง€ ์•Š์•˜์ง€๋งŒ

๋‚˜๋Š” @Repository์™€ @Table์„ ์ถ”๊ฐ€ํ•˜์—ฌ

์˜ค๋ฅ˜ ํ•ด๊ฒฐ

 

โ€ป ํ•œ๋ฒˆ๋งŒ ์„ค์ •ํ•ด์ฃผ๊ณ  ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” @Table coding X -> ํ•˜๋‚˜์˜ project์—์„œ

 

 

-์˜ค๋ฅ˜ ํ•ด๊ฒฐ ์ถœ์ฒ˜

 

https://vladmihalcea.com/the-best-way-to-map-the-discriminatorcolumn-with-jpa-and-hibernate/

728x90