3434import org .springframework .integration .mongodb .support .MongoHeaders ;
3535import org .springframework .integration .support .AbstractIntegrationMessageBuilder ;
3636import org .springframework .integration .transaction .IntegrationResourceHolder ;
37+ import org .springframework .lang .Nullable ;
3738import org .springframework .transaction .support .TransactionSynchronizationManager ;
3839import org .springframework .util .Assert ;
3940import org .springframework .util .CollectionUtils ;
4546 * a {@link org.springframework.messaging.Message} with a payload which is the result of
4647 * execution of a {@link Query}. When expectSingleResult is false (default), the MongoDb
4748 * {@link Query} is executed using {@link MongoOperations#find(Query, Class)} method which
48- * returns a {@link List}. The returned {@link List} will be used as the payoad of the
49+ * returns a {@link List}. The returned {@link List} will be used as the payload of the
4950 * {@link org.springframework.messaging.Message} returned by the {{@link #receive()}
5051 * method. An empty {@link List} is treated as null, thus resulting in no
5152 * {@link org.springframework.messaging.Message} returned by the {{@link #receive()}
6566 */
6667public class MongoDbMessageSource extends AbstractMessageSource <Object > {
6768
68- private final Expression queryExpression ;
69+ @ Nullable
70+ private final MongoDbFactory mongoDbFactory ;
6971
70- private volatile Expression collectionNameExpression = new LiteralExpression ( "data" ) ;
72+ private final Expression queryExpression ;
7173
72- private volatile StandardEvaluationContext evaluationContext ;
74+ private Expression collectionNameExpression = new LiteralExpression ( "data" ) ;
7375
74- private volatile MongoOperations mongoTemplate ;
76+ private StandardEvaluationContext evaluationContext ;
7577
76- private volatile MongoConverter mongoConverter ;
78+ private MongoOperations mongoTemplate ;
7779
78- private volatile MongoDbFactory mongoDbFactory ;
80+ private MongoConverter mongoConverter ;
7981
80- private volatile boolean initialized = false ;
82+ private Class <?> entityClass = DBObject . class ;
8183
82- private volatile Class <?> entityClass = DBObject . class ;
84+ private boolean expectSingleResult = false ;
8385
84- private volatile boolean expectSingleResult = false ;
86+ private volatile boolean initialized = false ;
8587
8688 /**
8789 * Creates an instance with the provided {@link MongoDbFactory} and SpEL expression
88- * which should resolve to a MongoDb 'query' string
89- * (see https://www.mongodb.org/display/DOCS/Querying).
90+ * which should resolve to a MongoDb 'query' string (see https://www.mongodb.org/display/DOCS/Querying).
9091 * The 'queryExpression' will be evaluated on every call to the {@link #receive()} method.
9192 * @param mongoDbFactory The mongodb factory.
9293 * @param queryExpression The query expression.
9394 */
9495 public MongoDbMessageSource (MongoDbFactory mongoDbFactory , Expression queryExpression ) {
9596 Assert .notNull (mongoDbFactory , "'mongoDbFactory' must not be null" );
9697 Assert .notNull (queryExpression , "'queryExpression' must not be null" );
97-
9898 this .mongoDbFactory = mongoDbFactory ;
9999 this .queryExpression = queryExpression ;
100100 }
101101
102102 /**
103103 * Creates an instance with the provided {@link MongoOperations} and SpEL expression
104- * which should resolve to a Mongo 'query' string
105- * (see https://www.mongodb.org/display/DOCS/Querying).
104+ * which should resolve to a Mongo 'query' string (see https://www.mongodb.org/display/DOCS/Querying).
106105 * It assumes that the {@link MongoOperations} is fully initialized and ready to be used.
107106 * The 'queryExpression' will be evaluated on every call to the {@link #receive()} method.
108107 * @param mongoTemplate The mongo template.
@@ -111,15 +110,14 @@ public MongoDbMessageSource(MongoDbFactory mongoDbFactory, Expression queryExpre
111110 public MongoDbMessageSource (MongoOperations mongoTemplate , Expression queryExpression ) {
112111 Assert .notNull (mongoTemplate , "'mongoTemplate' must not be null" );
113112 Assert .notNull (queryExpression , "'queryExpression' must not be null" );
114-
113+ this . mongoDbFactory = null ;
115114 this .mongoTemplate = mongoTemplate ;
116115 this .queryExpression = queryExpression ;
117116 }
118117
119118 /**
120119 * Allows you to set the type of the entityClass that will be passed to the
121- * {@link MongoTemplate#find(Query, Class)} or {@link MongoTemplate#findOne(Query, Class)}
122- * method.
120+ * {@link MongoTemplate#find(Query, Class)} or {@link MongoTemplate#findOne(Query, Class)} method.
123121 * Default is {@link DBObject}.
124122 * @param entityClass The entity class.
125123 */
@@ -135,7 +133,7 @@ public void setEntityClass(Class<?> entityClass) {
135133 * {@link #receive()} will use {@link MongoTemplate#findOne(Query, Class)},
136134 * and the payload of the returned {@link org.springframework.messaging.Message}
137135 * will be the returned target Object of type
138- * identified by {{ @link #entityClass} instead of a List.
136+ * identified by {@link #entityClass} instead of a List.
139137 * @param expectSingleResult true if a single result is expected.
140138 */
141139 public void setExpectSingleResult (boolean expectSingleResult ) {
@@ -188,8 +186,8 @@ protected void onInit() {
188186 /**
189187 * Will execute a {@link Query} returning its results as the Message payload.
190188 * The payload can be either {@link List} of elements of objects of type
191- * identified by {{ @link #entityClass}, or a single element of type identified by { {@link #entityClass}
192- * based on the value of {{ @link #expectSingleResult} attribute which defaults to 'false' resulting
189+ * identified by {@link #entityClass}, or a single element of type identified by {@link #entityClass}
190+ * based on the value of {@link #expectSingleResult} attribute which defaults to 'false' resulting
193191 * {@link org.springframework.messaging.Message} with payload of type
194192 * {@link List}. The collection name used in the
195193 * query will be provided in the {@link MongoHeaders#COLLECTION_NAME} header.
@@ -218,12 +216,10 @@ else if (value instanceof Query) {
218216
219217 Object result = null ;
220218 if (this .expectSingleResult ) {
221- result = this .mongoTemplate .
222- findOne (query , this .entityClass , collectionName );
219+ result = this .mongoTemplate .findOne (query , this .entityClass , collectionName );
223220 }
224221 else {
225- List <?> results = this .mongoTemplate .
226- find (query , this .entityClass , collectionName );
222+ List <?> results = this .mongoTemplate .find (query , this .entityClass , collectionName );
227223 if (!CollectionUtils .isEmpty (results )) {
228224 result = results ;
229225 }
0 commit comments