질문탭으로 가야하는데 고민으로 작성했네요... 수정으로는 이동이 안되는데 이동 가능하다면 부탁드립니다.ㅠㅠ
===========================================
AOP 수강 중 에러가 발생했는데 어디를 건드려야할지 잘 모르겠어서 문의 남깁니다.
우선 강의와 다른 환경으로 실습해보고 있어 조금씩 코드가 다릅니다.
먼저 에러 메세지는 아래 2개입니다.
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'memberController' defined in file [D:\dev\java\hello-spring\out\production\classes\hello\hellospring\controller\MemberController.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'hello.hellospring.service.MemberService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'hello.hellospring.service.MemberService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2번 메세지를 보게 되면 MemberService타입의 Qualifying 한 bean이 존재하지 않아 wiring 할수 없다는 에러인데. 스프링을 처음 해보는 입장에서는 그대로 따라 쳤는데 되지 않아 어떻게 더 수정해야할지 고민입니다.
관련된 코드 블럭을 첨부 드립니다.
SpringConfig.Java
package hello.hellospring;
import hello.hellospring.repository.*;
import hello.hellospring.service.MemberService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpringConfig {
// private EntityManager em;
private final MemberRepository memberRepository;
public SpringConfig(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}
// public SpringConfig(EntityManager em) {
// this.em = em;
// }
@Bean
public MemberService memberService(){
return new MemberService(memberRepository);
}
// @Bean
// public MemberRepository memberRepository(){
//// return new MemoryMemberRepository();
//// return new JdbcMemberRepository(dataSource);
//// return new JdbcTemplateMemberRepository(dataSource);
//// return new JpaMemberRepository(em);
// }
}
package hello.hellospring.service;
import hello.hellospring.domain.Member;
import hello.hellospring.repository.MemberRepository;
import hello.hellospring.repository.JdbcMemberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Component
public class MemberService {
private final MemberRepository memberRepository;
@Autowired
public MemberService(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}
/*
* 회원가입
*/
public Long join(Member member){
//같은 이름이 있느 중복 회원X
validatedDuplicateMember(member);
memberRepository.save(member);
return member.getId();
}
private void validatedDuplicateMember(Member member) {
memberRepository.findByName(member.getName())
.ifPresent(m -> {
throw new IllegalStateException("이미 존재하는 회원입니다.");
});
}
public List<Member> findMembers(){
return memberRepository.findAll();
}
public Optional<Member> findOne(Long memberId){
return memberRepository.findById(memberId);
}
}
spring.datasource.url=jdbc:h2:tcp://localhost/~/test
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.datasource.username=sa
gradle
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.1'
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'hello'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '21'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'
}
tasks.named('test') {
useJUnitPlatform()
}
package hello.hellospring.controller;
import hello.hellospring.domain.Member;
import hello.hellospring.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
@Controller
public class MemberController {
private final MemberService memberService;
@Autowired
public MemberController(MemberService memberService) {
this.memberService = memberService;
}
@GetMapping("/members/new")
public String createForm() {
return "members/createMemberForm";
}
@PostMapping("/members/new")
public String create(MemberForm form){
Member member = new Member();
member.setName(form.getName());
memberService.join(member);
return "redirect:/";
}
@GetMapping("members")
public String list(Model model){
List<Member> members = memberService.findMembers();
model.addAttribute("members", members);
return "members/memberList";
}
}
감사합니다.
안녕하세요. 김지수님, 공식 서포터즈 OMG입니다.
강의에서는 아래와 같이
@Service
어노테이션을 선언했다가 지우고 SpringConfig에 Service 빈을 등록합니다.MemberService의
@Component
를 지우고 확인해주세요 😀감사합니다.
답글