You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot MongoDB会员注册开发中Bean创建错误问题求助

Fixing Your Spring Boot + MongoDB Member Registration Error

Let's break down what's causing the error and how to fix it step by step.

Root Cause of the Error

The core issue is in your MemberSv repository interface: you added a custom membersave method, which Spring Data MongoDB tries to interpret as a query method. Since "membersave" doesn't follow Spring Data's query naming rules (like findBy or save), it throws an error looking for a non-existent membersave property on your MemberDTO. Even better—you don't need this method at all, because MongoRepository already provides a built-in save() method for persisting entities.

Step 1: Clean Up the Repository Interface

Remove the unnecessary membersave method from your repository. It inherits all required CRUD methods (including save()) directly from MongoRepository:

// @Repository is optional here—Spring Data auto-detects repositories
public interface MemberSv extends MongoRepository<MemberDTO, String> {
    // No custom methods needed; save() is already available
}

Step 2: Fix Controller Dependency Injection

Right now, your controller is trying to inject the repository directly, which isn't best practice. Controllers should depend on services, not repositories. Here's how to fix this:

Option A: Keep Your Existing Service Setup

Update your controller to inject the MemberSvImp service instead of the repository:

@Controller
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private MemberSvImp memberService; // Changed from MemberSv to MemberSvImp
    
    @RequestMapping(value = "/join", method = RequestMethod.POST)
    public String membersave(MemberDTO memberDTO) {
        memberService.membersave(memberDTO);
        return "join_form";
    }
}

Your MemberSvImp service is already correctly using the repository's built-in save() method, so no changes needed there:

@Service
@Transactional
public class MemberSvImp {
    @Autowired
    private MemberSv memberSv;
    
    public MemberDTO membersave(MemberDTO memberDTO) {
        return memberSv.save(memberDTO);
    }
}

For better code maintainability, create a service interface and have your implementation class implement it:

  1. Create a MemberService interface:
public interface MemberService {
    MemberDTO membersave(MemberDTO memberDTO);
}
  1. Update MemberSvImp to implement this interface:
@Service
@Transactional
public class MemberSvImp implements MemberService {
    @Autowired
    private MemberSv memberSv;
    
    @Override
    public MemberDTO membersave(MemberDTO memberDTO) {
        return memberSv.save(memberDTO);
    }
}
  1. Inject the interface into your controller:
@Controller
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    
    @RequestMapping(value = "/join", method = RequestMethod.POST)
    public String membersave(MemberDTO memberDTO) {
        memberService.membersave(memberDTO);
        return "join_form";
    }
}

Step 3: Fix Missing @Document Annotation

Your MemberDTO is missing the @Document annotation, which tells Spring Data MongoDB to map this class to a collection in the database. Add it to your DTO:

import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.CreatedDate;

@Document(collection = "members") // Maps to a MongoDB collection named "members"
public class MemberDTO {
    @Id
    private String memberId;
    private String memberPwd;
    private String memberName;
    private String memberEName;
    private String memberCompany;
    private String memberPhone;
    private String memberZipCode;
    private String memberAddress;
    private String memberAddressDetail;
    private String memberAuth;
    @CreatedDate
    private Date createdDate;
}

Step 4: Enable Auditing for @CreatedDate

The @CreatedDate annotation won't auto-populate unless you enable MongoDB auditing in your main application class. Add @EnableMongoAuditing:

@EnableMongoAuditing // Required for @CreatedDate to work
@EnableMongoRepositories(basePackageClasses = com.example.accountproject.models.interfaces.MemberSv.class)
@SpringBootApplication
public class AccountProjectApplication {
    public static void main(String[] args) {
        SpringApplication.run(AccountProjectApplication.class, args);
    }
}

Final Notes

After making all these changes, your application should start without errors, and the member registration functionality will work as expected. The key fixes were removing the invalid repository method, ensuring proper layer dependencies, adding the missing @Document annotation, and enabling auditing for timestamp fields.

内容的提问来源于stack exchange,提问作者DB K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 19:52:32