2017-11-20 3 views
0

私の学生のエンティティクラスのMongoDB -Considerあなたの設定で 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' タイプのBeanを定義する

package com.example.entity; 

import java.io.Serializable; 

import javax.persistence.Id; 

import org.springframework.data.mongodb.core.mapping.Document; 

@Document(collection = "student") 
public class StudentMongo implements Serializable { 

    private static final long serialVersionUID = 8764013757545132519L; 

    @Id 
    private Long id; 

    private String name; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

} 

マイリポジトリ

package com.example.repository; 

import javax.annotation.Resource; 

import org.springframework.data.mongodb.core.MongoOperations; 
import org.springframework.data.mongodb.repository.query.MongoEntityInformation; 
import org.springframework.data.mongodb.repository.support.SimpleMongoRepository; 
import org.springframework.stereotype.Repository; 

import com.example.entity.StudentMongo; 

@Repository 
public class StudentMongoRepository extends SimpleMongoRepository<StudentMongo, Long> { 

    @Resource 
    MongoOperations mongoOperations; 


    public StudentMongoRepository(MongoEntityInformation<StudentMongo, Long> metadata, MongoOperations mongoOperations) { 
     super(metadata, mongoOperations); 
    } 

} 

マイコンフィギュレーション・クラス

@Configuration 
@EnableMongoRepositories(basePackageClasses = {com.example.repository.StudentMongoRepository.class}) 
public class MongoConfiguration { 

} 

春ブートアプリケーション

私は、次のアプリケーションに

2017-11-20 09:04:48.937 ERROR 23220 --- [   main] o.s.b.d.LoggingFailureAnalysisReporter : 

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

Description: 

Parameter 0 of constructor in com.example.repository.StudentMongoRepository required a bean of type 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' that could not be found. 


Action: 

Consider defining a bean of type 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' in your configuration. 

を取得していたアプリケーションを起動しようとするタイプのBeanを定義する検討する「org.springframework.data.mongodb.repository.query.MongoEntityInformation」あなたの設定で 作成する方法EntityInformation BeanがSpringフレームワークで述べたように アプリケーションの実行中に上記の問題が発生する。エンティティ情報を渡す方法

は、すべてのSimpleMongorepository

+0

あなたmongoOperationsクラスに@Serviceアノテーションを追加します。 – Habil

+0

@HabilなぜmongoOperationsで** @サービス**? –

答えて

0

ファーストを使用する方法を私に勧め私はではなく、実装クラスを使用するインターフェイスを使用しないでくださいと言いたいと思います。

インターフェイスを宣言してMongoRepository<T,ID>から拡張すると、スプリングはすぐに実装を提供します。

@Repository 
public interface StudentMongoRepository extends MongoRepository<T,ID>{ 

@Autowired 
private MongoOperations mongoOperations; 

これは動作するはずです。

0

完全なサンプル。

public interface UserService extends MongoRepository<User, String>{ 

User findByName(String name); 

}

@Service 
public abstract class UserServiceImpl implements UserService { 

@Autowired 
private UserService userRepository; 

public User findByName(String name) { 
    return userRepository.findByName(name); 
} 

}

@Autowired 
UserService userService; 

@RequestMapping(value = "/user/", method = RequestMethod.GET) 
public ResponseEntity<List<User>> listAllUsers() { 
    List<User> users = userService.findAll(); 
    if (users.isEmpty()) { 
     return new ResponseEntity<>(HttpStatus.NO_CONTENT); 
    } 
    return new ResponseEntity<>(users, HttpStatus.OK); 
} 

public class User { 

@Id 
private String id; 

}

関連する問題