From fec5bae808a6f0d853ddfcf8ccfc7b5b0a712899 Mon Sep 17 00:00:00 2001 From: "J. Brisbin" Date: Tue, 5 Apr 2011 14:56:30 -0500 Subject: [PATCH] Added MappingContextAware interface and BeanPostProcessor for same --- .../mapping/context/MappingContextAware.java | 35 ++++++++ .../MappingContextAwareBeanPostProcessor.java | 81 +++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContextAware.java create mode 100644 spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContextAwareBeanPostProcessor.java diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContextAware.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContextAware.java new file mode 100644 index 000000000..0513804e1 --- /dev/null +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContextAware.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2011 by the original author(s). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.mapping.context; + +import org.springframework.data.mapping.model.MappingContext; + +/** + * An interface to make beans aware of the active MappingContext in the current ApplicationContext. + * + * @author Jon Brisbin + */ +public interface MappingContextAware { + + /** + * The active MappingContext for the environment. + * + * @param mappingContext + */ + void setMappingContext(MappingContext mappingContext); + +} diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContextAwareBeanPostProcessor.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContextAwareBeanPostProcessor.java new file mode 100644 index 000000000..b36a2978f --- /dev/null +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContextAwareBeanPostProcessor.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2011 by the original author(s). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.mapping.context; + +import java.util.Map; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.data.mapping.model.MappingContext; + +/** + * BeanPostProcessor to make Spring beans aware of the current MappingContext. If a MappingContext exists with the + * default bean name ("mappingContext"), then that bean is used. If there is no MappingContext registered under the + * default bean name, then the first MappingContext it finds is the one it chooses. + * + * @author Jon Brisbin + */ +public class MappingContextAwareBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { + + private ApplicationContext applicationContext; + private String mappingContextBeanName = "mappingContext"; + private MappingContext mappingContext; + + public MappingContextAwareBeanPostProcessor() { + } + + public MappingContextAwareBeanPostProcessor(MappingContext mappingContext) { + this.mappingContext = mappingContext; + } + + public String getMappingContextBeanName() { + return mappingContextBeanName; + } + + public void setMappingContextBeanName(String mappingContextBeanName) { + this.mappingContextBeanName = mappingContextBeanName; + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + return bean; + } + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if (bean instanceof MappingContextAware) { + if (null == mappingContext) { + Map mappingContexts = applicationContext.getBeansOfType(MappingContext.class); + if (mappingContexts.containsKey(mappingContextBeanName)) { + this.mappingContext = mappingContexts.get(mappingContextBeanName); + } else { + String firstBean = mappingContexts.keySet().iterator().next(); + this.mappingContext = applicationContext.getBean(firstBean, MappingContext.class); + } + } + ((MappingContextAware) bean).setMappingContext(mappingContext); + } + return bean; + } +}