전체 글
-
[Spring] 빈이 충돌나는 경우JAVA/Spring(Springboot) 2021. 7. 21. 20:28
스프링 컨테이너에서 빈이 충돌나는 경우는 크게 두가지가 있다 첫번째로 자동 vs 자동 인 경우 두번째로 자동 vs 수동 인 경우 자동 vs 자동인 경우는 컴파일 과정에서 에러가 난다 에러 또한 친절하게 어느 클래스에서 중복이 되어 충돌이 나는지 적혀있다 두번째로 자동 vs 수동인 경우는 수동으로 등록하는 빈으로 overriding 되어 등록된다 Overriding bean definition for bean 'memoryMemberRepository' with a different definition 위와 같이 특정 빈이 overriding 된 것을 확인할 수 있다 하지만 스프링 부트에서는 overriding 이 적용이 안 되고 오류가 난다!
-
[Spring] Bean을 생성하는 방법JAVA/Spring(Springboot) 2021. 7. 18. 15:00
Bean을 생성하는 방법은 크게 두가지가 있다 첫번째는 AppConfig.class 를 사용하여 Factory Bean을 통해서 생성하는 방식이 있다 AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class); 두번째는 XML을 사용하여 직접적으로 빈을 생성하는 방식이 있다 ApplicationContext ac = new GenericXmlApplicationContext("appConfig.xml");
-
[Spring] Bean 객체 가져오기JAVA/Spring(Springboot) 2021. 7. 17. 16:09
Bean 객체를 가져올때 두가지 방법이 있다 1. Bean 타입으로 가져오기 2. Bean 이름으로 가져오기 우선 ApplicationContext 객체를 생성하자 ApplicationContext ac = AnnotationConfigApplicationContext(AppConfig.class); 위에서 사용하는 AppConfig 파일은 @Bean을 등록한 Configuration 파일이다 @Configuration public class AppConfig { @Bean public MemberService memberService() { return new MemberServiceImpl(memberRepository()); } @Bean public MemberRepository memberRep..
-
스프링 컨테이너에 등록된 모든 빈 조회JAVA/Spring(Springboot) 2021. 7. 17. 15:10
스프링 컨테이너에 등록된 모든 빈 및 사용자가 등록한 빈을 조회할 수 있다 우선 스프링 컨테이너를 생성해보자 AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class); 스프링 컨테이너에 등록된 모든 빈 출력 스프링 컨테이너에 등록된 모든 빈을 출력해보겠다 AppConfig에서 등록된 빈뿐만 아니라 스프링에서 기본으로 저장되어 있는 빈 객체도 있다 @Test @DisplayName("모든 빈 출력하기") void findAllBean() { String[] beanDefinitionNames = ac.getBeanDefinitionNames(); for(name : beanDefinitionN..
-
스프링 부트 사용해서 스프링 프로젝트 생성JAVA/Spring(Springboot) 2021. 7. 13. 20:35
스프링 부트를 사용해서 스프링 프로젝트를 간편하게 생성할 수 있다 우선 스프링 부트 스타터 페이지로 가자 https://start.spring.io 스프링 부트 스타터 페이지에서 Project : Gradle Language : Java Spring Boot : SNAPSHOT, M2로 표기된 건 정식 Release 버전이 아니기 때문에 이를 제외한 가장 최신 버전 선택 Group, Artifact 기입 Package name : Jar Java version : 본인 PC Java 버전에 맞게 선택 => GENERATE! 생성했다면 압축을 푼 후 InteliJ에서 New Project > 압축폴더에서 build.gradle 선택을 한다 그리고 InteliJ에서 File > Settings > Gradl..
-
[JPA] JPA 사용하기JAVA/Spring(Springboot) 2021. 7. 11. 16:45
JPA를 사용하면 DB쿼리를 작성하지 않아도 DB작업이 가능하다 JPA를 사용하기 위한 작업을 알아보자 1. build.gradle의 dependencies에 아래 코드를 추가한다 dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' } 2. application.properties에 아래 코드를 추가한다 // jpa로 작성된 sql을 보여준다 spring.jpa.show-sql=true // DB에 table이 이미 생성되어 있기 때문에 ddl-auto는 꺼준다 spring.jpa.hibernate.ddl-auto=none 3. Repository에서 사용하면 된다 package hello.hellosp..