작성
·
283
0
package hello.hellospring.controller;
import hello.hellospring.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class MemberController {
private MemberService memberService; // 필드주입 - 별로 안좋음 바꿀 수 없기 때문
// @Autowired
// public void setMemberService(MemberService memberService) {
// this.memberService = memberService;
// }
@Autowired // controller와 service 연결 시켜줌 / 생성자 주입- 가장 좋음음
public MemberController(MemberService memberService) {
this.memberService = memberService;
}
}
package hello.hellospring.service;
package hello.hellospring.service;
import hello.hellospring.domain.Member;
import hello.hellospring.repository.MemberRepository;
import hello.hellospring.repository.MemoryMemberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service //Service에 MemberService를 등록 /스프링 빈 등록 / Component로 해도됨
public class MemberService {
private MemberRepository memberRepository;
// @Autowired
// public void setMemberRepository(MemberRepository memberRepository) {
// this.memberRepository = memberRepository; //setter 주입
// }
public MemberService(MemberRepository memberRepository) { //외부에서 넣어지게 (직접 new로 생성하는게 x)
this.memberRepository = memberRepository; //dependance ijection
}
/**
* 회원가입
*/
public Long join(Member member) {
validateDuplicateMember(member); //중복회원 검증
memberRepository.save(member);
return member.getId();
}
private void validateDuplicateMember(Member member) {
memberRepository.findByName(member.getName()) // optional로 한 이유는 null 이 아니면을 생략하기 위해
.ifPresent(m -> {
throw new IllegalStateException("이미 존재하는 회원입니다.");
});
}
/**
* 전체 회원조회
*/
public List<Member> findMembers() {
return memberRepository.findAll();
}
public Optional<Member> findOne(Long memberId){
return memberRepository.findByID((memberId));
}
}
MemberService.java
답변 1
0
안녕하세요. 허은택 sam님, 공식 서포터즈 OMG입니다.
아래의 링크 참고해주세요.
안드로이드 스튜디오이지만 인텔리제이에서도 확인 가능합니다.
[Android Studio] error: illegal character: '\ufeff' : 네이버 블로그 (naver.com)
감사합니다.