Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotation for xml-parent - Solution [SPR-6343] #11009

Closed
spring-projects-issues opened this issue Nov 12, 2009 · 1 comment
Closed

Annotation for xml-parent - Solution [SPR-6343] #11009

spring-projects-issues opened this issue Nov 12, 2009 · 1 comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply type: task A general task

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Nov 12, 2009

Michael Hauser opened SPR-6343 and commented

I have a solution for this problem, see 3 Code snippets below:

1)--- The annotation @Interface
CildOf.java

package ??????? ; // fill in whatever suits you

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.stereotype.Component;

/**

  • Annotation for Bean definition Inheritance - must be processed by a
  • BeanFactoryPostProcessor prior to Instantiation of the Beans<br>
  • Indicates that this bean is ChildOf the named parent Bean and inherits all
  • (set) properties of the parent Class
  • @author HAUSER
  • @since 2009-05-12

*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ChildOf {

/**
 * The value may indicate a suggestion for a logical component name,
 * to be turned into a Spring bean in case of an autodetected component.
 * @return the suggested component name, if any
 */
String value() default "";

/**
 * The name of the parent bean - must NOT be null
 * 
 * @return the parent beanname
 */
String parent() default "";

}

2)--- The BeanFactoryPostProcessor to digest this
BeanFactoryPostProcessor.java

package ??????? ; // fill in whatever suits you

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.StringUtils;

/**

  • BeanFactoryPostProcessor for Inheritance of Bean Properties via Annotations
  • {@link at.gv.brz.spring.beans.ChildOf @ChildOf}
  • @author HAUSER
  • @since 2009-05-12

*/
public class ChildOfBeanFactoryPostProcessor implements
BeanFactoryPostProcessor, PriorityOrdered {

protected final Log logger = LogFactory.getLog(getClass());

private int order = Ordered.LOWEST_PRECEDENCE;  // default: same as non-Ordered

public void setOrder(int order) {
  this.order = order;
}

public int getOrder() {
  return this.order;
}

/**
 * Modify the application context's internal bean factory after its standard
 * initialization. All bean definitions will have been loaded, but no beans
 * will have been instantiated yet. This allows for overriding or adding
 * properties even to eager-initializing beans.
 * 
 * @param beanFactory
 *            the bean factory used by the application context
 * @throws org.springframework.beans.BeansException
 *             in case of errors
 */
@SuppressWarnings("unchecked")
public void postProcessBeanFactory(
		ConfigurableListableBeanFactory beanFactory) throws BeansException {

	String[] beanNames = beanFactory.getBeanDefinitionNames();
	for (int i = 0; i < beanNames.length; i++) {
		String beanName = beanNames[i];
		BeanDefinition bd = beanFactory.getBeanDefinition(beanName);
		String className = bd.getBeanClassName();
		// some classnames are nulls, why ?
		if (!StringUtils.hasText(className)) {
			continue;
		}
		try {
			Class clazz = Class.forName(className);
			ChildOf childOf = AnnotationUtils.findAnnotation(clazz,
					ChildOf.class);
			if (childOf != null) {
				String parentName = childOf.parent();
				if (!StringUtils.hasText(parentName)) {
					throw new FatalBeanException(
							"ChildOf Annotation of bean [" + beanName
									+ "] has no parent Value");
				}
				if (logger.isDebugEnabled()) {
					logger.debug("Found parentName [" + parentName
							+ "] for bean [" + beanName + "]");
				}
				// is there already a different parent ?
				String oldParentName = bd.getParentName();
				if (StringUtils.hasText(oldParentName)
						&& !parentName.equals(oldParentName)) {
					logger.warn("bean [" + beanName
							+ "] has already parent [" + oldParentName
							+ "] set - new annotated parent[" + parentName
							+ "] will be ignored");
				}

				// and set the parentName
				bd.setParentName(parentName);
			}

		} catch (ClassNotFoundException e) {
			logger.error("Bean [" + beanName + "] has invalid ClassName["
					+ bd.getBeanClassName()
					+ "] - Exception will be ignored");
		}
	}

}

}

  1. register the BFPostprocessor somewhere in Your applicationConfig.xml like:

<bean id="childOfAnnotationsProcessor" class="??????.ChildOfBeanFactoryPostProcessor" />
4) and final annotate any @Component, @Controller , ... etc like:
@ChildOf(parent = "baseController")
@Controller
public class MyDerivedController extends BaseController {


Affects: 2.5.6

Reference URL: http://jira.springframework.org/browse/SPR-5580

This issue is a sub-task of #10251

1 votes, 0 watchers

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

This issue has been resolved through a selective bulk update, as part of a larger effort to better manage unresolved issues. To qualify for the update, the issue was either created before Spring 3.0 or affects a version older than Spring 3.0 and is not a bug.

There is a good chance the request was made obsolete, or at least partly outdated, by changes in later versions of Spring including deprecations. It is also possible it didn't get enough traction or we didn't have enough time to address it. One way or another, we didn't get to it.

If you believe the issue, or some aspects of it, are still relevant and worth pursuing at present you may re-open this issue or create a new one with a more up-to-date description.

We thank you for your contributions and encourage you to become familiar with the current process of managing Spring Framework JIRA issues that has been in use for over a year.

@spring-projects-issues spring-projects-issues added status: declined A suggestion or change that we don't feel we should currently apply in: core Issues in core modules (aop, beans, core, context, expression) type: task A general task labels Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply type: task A general task
Projects
None yet
Development

No branches or pull requests

1 participant