下面列出了怎么用org.springframework.beans.factory.config.BeanReference的API类实例代码及写法,或者点击链接到github查看源代码。
public AdvisorComponentDefinition(
String advisorBeanName, BeanDefinition advisorDefinition, @Nullable BeanDefinition pointcutDefinition) {
Assert.notNull(advisorBeanName, "'advisorBeanName' must not be null");
Assert.notNull(advisorDefinition, "'advisorDefinition' must not be null");
this.advisorBeanName = advisorBeanName;
this.advisorDefinition = advisorDefinition;
MutablePropertyValues pvs = advisorDefinition.getPropertyValues();
BeanReference adviceReference = (BeanReference) pvs.get("adviceBeanName");
Assert.state(adviceReference != null, "Missing 'adviceBeanName' property");
if (pointcutDefinition != null) {
this.beanReferences = new BeanReference[] {adviceReference};
this.beanDefinitions = new BeanDefinition[] {advisorDefinition, pointcutDefinition};
this.description = buildDescription(adviceReference, pointcutDefinition);
}
else {
BeanReference pointcutReference = (BeanReference) pvs.get("pointcut");
Assert.state(pointcutReference != null, "Missing 'pointcut' property");
this.beanReferences = new BeanReference[] {adviceReference, pointcutReference};
this.beanDefinitions = new BeanDefinition[] {advisorDefinition};
this.description = buildDescription(adviceReference, pointcutReference);
}
}
/**
* Create a new BeanComponentDefinition for the given bean.
* @param beanDefinitionHolder the BeanDefinitionHolder encapsulating
* the bean definition as well as the name of the bean
*/
public BeanComponentDefinition(BeanDefinitionHolder beanDefinitionHolder) {
super(beanDefinitionHolder);
List<BeanDefinition> innerBeans = new ArrayList<>();
List<BeanReference> references = new ArrayList<>();
PropertyValues propertyValues = beanDefinitionHolder.getBeanDefinition().getPropertyValues();
for (PropertyValue propertyValue : propertyValues.getPropertyValues()) {
Object value = propertyValue.getValue();
if (value instanceof BeanDefinitionHolder) {
innerBeans.add(((BeanDefinitionHolder) value).getBeanDefinition());
}
else if (value instanceof BeanDefinition) {
innerBeans.add((BeanDefinition) value);
}
else if (value instanceof BeanReference) {
references.add((BeanReference) value);
}
}
this.innerBeanDefinitions = innerBeans.toArray(new BeanDefinition[0]);
this.beanReferences = references.toArray(new BeanReference[0]);
}
public AdvisorComponentDefinition(
String advisorBeanName, BeanDefinition advisorDefinition, @Nullable BeanDefinition pointcutDefinition) {
Assert.notNull(advisorBeanName, "'advisorBeanName' must not be null");
Assert.notNull(advisorDefinition, "'advisorDefinition' must not be null");
this.advisorBeanName = advisorBeanName;
this.advisorDefinition = advisorDefinition;
MutablePropertyValues pvs = advisorDefinition.getPropertyValues();
BeanReference adviceReference = (BeanReference) pvs.get("adviceBeanName");
Assert.state(adviceReference != null, "Missing 'adviceBeanName' property");
if (pointcutDefinition != null) {
this.beanReferences = new BeanReference[] {adviceReference};
this.beanDefinitions = new BeanDefinition[] {advisorDefinition, pointcutDefinition};
this.description = buildDescription(adviceReference, pointcutDefinition);
}
else {
BeanReference pointcutReference = (BeanReference) pvs.get("pointcut");
Assert.state(pointcutReference != null, "Missing 'pointcut' property");
this.beanReferences = new BeanReference[] {adviceReference, pointcutReference};
this.beanDefinitions = new BeanDefinition[] {advisorDefinition};
this.description = buildDescription(adviceReference, pointcutReference);
}
}
/**
* Create a new BeanComponentDefinition for the given bean.
* @param beanDefinitionHolder the BeanDefinitionHolder encapsulating
* the bean definition as well as the name of the bean
*/
public BeanComponentDefinition(BeanDefinitionHolder beanDefinitionHolder) {
super(beanDefinitionHolder);
List<BeanDefinition> innerBeans = new ArrayList<>();
List<BeanReference> references = new ArrayList<>();
PropertyValues propertyValues = beanDefinitionHolder.getBeanDefinition().getPropertyValues();
for (PropertyValue propertyValue : propertyValues.getPropertyValues()) {
Object value = propertyValue.getValue();
if (value instanceof BeanDefinitionHolder) {
innerBeans.add(((BeanDefinitionHolder) value).getBeanDefinition());
}
else if (value instanceof BeanDefinition) {
innerBeans.add((BeanDefinition) value);
}
else if (value instanceof BeanReference) {
references.add((BeanReference) value);
}
}
this.innerBeanDefinitions = innerBeans.toArray(new BeanDefinition[0]);
this.beanReferences = references.toArray(new BeanReference[0]);
}
private void findInnerBeanDefinitionsAndBeanReferences(BeanDefinition beanDefinition) {
List<BeanDefinition> innerBeans = new ArrayList<BeanDefinition>();
List<BeanReference> references = new ArrayList<BeanReference>();
PropertyValues propertyValues = beanDefinition.getPropertyValues();
for (PropertyValue propertyValue : propertyValues.getPropertyValues()) {
Object value = propertyValue.getValue();
if (value instanceof BeanDefinitionHolder) {
innerBeans.add(((BeanDefinitionHolder) value).getBeanDefinition());
}
else if (value instanceof BeanDefinition) {
innerBeans.add((BeanDefinition) value);
}
else if (value instanceof BeanReference) {
references.add((BeanReference) value);
}
}
this.innerBeanDefinitions = innerBeans.toArray(new BeanDefinition[innerBeans.size()]);
this.beanReferences = references.toArray(new BeanReference[references.size()]);
}
private void unwrapDefinitions(BeanDefinition advisorDefinition, BeanDefinition pointcutDefinition) {
MutablePropertyValues pvs = advisorDefinition.getPropertyValues();
BeanReference adviceReference = (BeanReference) pvs.getPropertyValue("adviceBeanName").getValue();
if (pointcutDefinition != null) {
this.beanReferences = new BeanReference[] {adviceReference};
this.beanDefinitions = new BeanDefinition[] {advisorDefinition, pointcutDefinition};
this.description = buildDescription(adviceReference, pointcutDefinition);
}
else {
BeanReference pointcutReference = (BeanReference) pvs.getPropertyValue("pointcut").getValue();
this.beanReferences = new BeanReference[] {adviceReference, pointcutReference};
this.beanDefinitions = new BeanDefinition[] {advisorDefinition};
this.description = buildDescription(adviceReference, pointcutReference);
}
}
private void findInnerBeanDefinitionsAndBeanReferences(BeanDefinition beanDefinition) {
List<BeanDefinition> innerBeans = new ArrayList<BeanDefinition>();
List<BeanReference> references = new ArrayList<BeanReference>();
PropertyValues propertyValues = beanDefinition.getPropertyValues();
for (int i = 0; i < propertyValues.getPropertyValues().length; i++) {
PropertyValue propertyValue = propertyValues.getPropertyValues()[i];
Object value = propertyValue.getValue();
if (value instanceof BeanDefinitionHolder) {
innerBeans.add(((BeanDefinitionHolder) value).getBeanDefinition());
}
else if (value instanceof BeanDefinition) {
innerBeans.add((BeanDefinition) value);
}
else if (value instanceof BeanReference) {
references.add((BeanReference) value);
}
}
this.innerBeanDefinitions = innerBeans.toArray(new BeanDefinition[innerBeans.size()]);
this.beanReferences = references.toArray(new BeanReference[references.size()]);
}
private void unwrapDefinitions(BeanDefinition advisorDefinition, BeanDefinition pointcutDefinition) {
MutablePropertyValues pvs = advisorDefinition.getPropertyValues();
BeanReference adviceReference = (BeanReference) pvs.getPropertyValue("adviceBeanName").getValue();
if (pointcutDefinition != null) {
this.beanReferences = new BeanReference[] {adviceReference};
this.beanDefinitions = new BeanDefinition[] {advisorDefinition, pointcutDefinition};
this.description = buildDescription(adviceReference, pointcutDefinition);
}
else {
BeanReference pointcutReference = (BeanReference) pvs.getPropertyValue("pointcut").getValue();
this.beanReferences = new BeanReference[] {adviceReference, pointcutReference};
this.beanDefinitions = new BeanDefinition[] {advisorDefinition};
this.description = buildDescription(adviceReference, pointcutReference);
}
}
private void findInnerBeanDefinitionsAndBeanReferences(BeanDefinition beanDefinition) {
List<BeanDefinition> innerBeans = new ArrayList<BeanDefinition>();
List<BeanReference> references = new ArrayList<BeanReference>();
PropertyValues propertyValues = beanDefinition.getPropertyValues();
for (int i = 0; i < propertyValues.getPropertyValues().length; i++) {
PropertyValue propertyValue = propertyValues.getPropertyValues()[i];
Object value = propertyValue.getValue();
if (value instanceof BeanDefinitionHolder) {
innerBeans.add(((BeanDefinitionHolder) value).getBeanDefinition());
}
else if (value instanceof BeanDefinition) {
innerBeans.add((BeanDefinition) value);
}
else if (value instanceof BeanReference) {
references.add((BeanReference) value);
}
}
this.innerBeanDefinitions = innerBeans.toArray(new BeanDefinition[innerBeans.size()]);
this.beanReferences = references.toArray(new BeanReference[references.size()]);
}
private List<BeanReference> registerPlugins(final String id, final Client client) {
final Stream<Optional<String>> plugins = Stream.of(
registerChaosPlugin(id, client),
registerMicrometerPlugin(id, client),
registerRequestCompressionPlugin(id, client),
registerLogbookPlugin(id, client),
registerOpenTracingPlugin(id, client),
registerCircuitBreakerFailsafePlugin(id, client),
registerRetryPolicyFailsafePlugin(id, client),
registerAuthorizationPlugin(id, client),
registerBackupRequestFailsafePlugin(id, client),
registerTimeoutFailsafePlugin(id, client),
registerOriginalStackTracePlugin(id, client),
registerCustomPlugin(id));
return plugins
.filter(Optional::isPresent)
.map(Optional::get)
.map(Registry::ref)
.collect(toCollection(Registry::list));
}
private AspectComponentDefinition createAspectComponentDefinition(
Element aspectElement, String aspectId, List<BeanDefinition> beanDefs,
List<BeanReference> beanRefs, ParserContext parserContext) {
BeanDefinition[] beanDefArray = beanDefs.toArray(new BeanDefinition[0]);
BeanReference[] beanRefArray = beanRefs.toArray(new BeanReference[0]);
Object source = parserContext.extractSource(aspectElement);
return new AspectComponentDefinition(aspectId, beanDefArray, beanRefArray, source);
}
public AspectComponentDefinition(String aspectName, @Nullable BeanDefinition[] beanDefinitions,
@Nullable BeanReference[] beanReferences, @Nullable Object source) {
super(aspectName, source);
this.beanDefinitions = (beanDefinitions != null ? beanDefinitions : new BeanDefinition[0]);
this.beanReferences = (beanReferences != null ? beanReferences : new BeanReference[0]);
}
private AspectComponentDefinition createAspectComponentDefinition(
Element aspectElement, String aspectId, List<BeanDefinition> beanDefs,
List<BeanReference> beanRefs, ParserContext parserContext) {
BeanDefinition[] beanDefArray = beanDefs.toArray(new BeanDefinition[0]);
BeanReference[] beanRefArray = beanRefs.toArray(new BeanReference[0]);
Object source = parserContext.extractSource(aspectElement);
return new AspectComponentDefinition(aspectId, beanDefArray, beanRefArray, source);
}
public AspectComponentDefinition(String aspectName, @Nullable BeanDefinition[] beanDefinitions,
@Nullable BeanReference[] beanReferences, @Nullable Object source) {
super(aspectName, source);
this.beanDefinitions = (beanDefinitions != null ? beanDefinitions : new BeanDefinition[0]);
this.beanReferences = (beanReferences != null ? beanReferences : new BeanReference[0]);
}
private AspectComponentDefinition createAspectComponentDefinition(
Element aspectElement, String aspectId, List<BeanDefinition> beanDefs,
List<BeanReference> beanRefs, ParserContext parserContext) {
BeanDefinition[] beanDefArray = beanDefs.toArray(new BeanDefinition[beanDefs.size()]);
BeanReference[] beanRefArray = beanRefs.toArray(new BeanReference[beanRefs.size()]);
Object source = parserContext.extractSource(aspectElement);
return new AspectComponentDefinition(aspectId, beanDefArray, beanRefArray, source);
}
public AspectComponentDefinition(
String aspectName, BeanDefinition[] beanDefinitions, BeanReference[] beanReferences, Object source) {
super(aspectName, source);
this.beanDefinitions = (beanDefinitions != null ? beanDefinitions : new BeanDefinition[0]);
this.beanReferences = (beanReferences != null ? beanReferences : new BeanReference[0]);
}
private AspectComponentDefinition createAspectComponentDefinition(
Element aspectElement, String aspectId, List<BeanDefinition> beanDefs,
List<BeanReference> beanRefs, ParserContext parserContext) {
BeanDefinition[] beanDefArray = beanDefs.toArray(new BeanDefinition[beanDefs.size()]);
BeanReference[] beanRefArray = beanRefs.toArray(new BeanReference[beanRefs.size()]);
Object source = parserContext.extractSource(aspectElement);
return new AspectComponentDefinition(aspectId, beanDefArray, beanRefArray, source);
}
public AspectComponentDefinition(
String aspectName, BeanDefinition[] beanDefinitions, BeanReference[] beanReferences, Object source) {
super(aspectName, source);
this.beanDefinitions = (beanDefinitions != null ? beanDefinitions : new BeanDefinition[0]);
this.beanReferences = (beanReferences != null ? beanReferences : new BeanReference[0]);
}
private Optional<BeanReference> findCacheStorageReference(final String id, final Client client) {
if (client.getCaching().getEnabled()) {
return registry.findRef(id, HttpCacheStorage.class);
} else {
return Optional.empty();
}
}
@Test
void shouldFindPrimaryBeanDefinitionIfAvailable() {
final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(PrimaryTracerConfiguration.class);
context.refresh();
final ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
final DefaultRiptideConfigurer configurer = new DefaultRiptideConfigurer(beanFactory, null);
final BeanReference bd = configurer.getBeanRef(Tracer.class, "tracer");
assertThat(bd.getBeanName()).isEqualTo("primaryTracer");
}
@Test
void shouldBeanDefinitionIfSingleBeanRegisteredForType() {
final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(SingleTracerConfiguration.class);
context.refresh();
final ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
final DefaultRiptideConfigurer configurer = new DefaultRiptideConfigurer(beanFactory, null);
final BeanReference bd = configurer.getBeanRef(Tracer.class, "tracer");
assertThat(bd.getBeanName()).isEqualTo("opentracingTracer");
}
@Test
void shouldFindBeanDefinitionByNameIfNoPrimaryBeanAvailable() {
final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(DoubleTracerConfiguration.class);
context.refresh();
final ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
final DefaultRiptideConfigurer configurer = new DefaultRiptideConfigurer(beanFactory, null);
final BeanReference bd = configurer.getBeanRef(Tracer.class, "tracer");
assertThat(bd.getBeanName()).isEqualTo("tracer");
}
@Override
public void postProcessBeanDefinitionRegistry(
BeanDefinitionRegistry registry) throws BeansException {
if (this.mongoTemplate == null) {
if (this.repoId != null) {
// Fetch the MongoTemplate Bean Id
//
BeanDefinition repo = registry.getBeanDefinition(this.repoId);
this.templateId = ((BeanReference)repo.getPropertyValues().get("mongoOperations")).getBeanName();
}
// Check to make sure we have a reference to the MongoTemplate
//
if (this.templateId == null) {
throw new RuntimeException("Unable to obtain a reference to a MongoTemplate");
}
}
// Add in CascadeSupport
//
BeanDefinition mongoCascadeSupportBean = BeanDefinitionBuilder
.genericBeanDefinition(MongoCascadeSupport.class)
.getBeanDefinition();
ConstructorArgumentValues args = mongoCascadeSupportBean.getConstructorArgumentValues();
args.addIndexedArgumentValue(0, this);
registry.registerBeanDefinition(Long.toString((new Random()).nextLong()), mongoCascadeSupportBean);
}
private void parseAspect(Element aspectElement, ParserContext parserContext) {
String aspectId = aspectElement.getAttribute(ID);
String aspectName = aspectElement.getAttribute(REF);
try {
this.parseState.push(new AspectEntry(aspectId, aspectName));
List<BeanDefinition> beanDefinitions = new ArrayList<>();
List<BeanReference> beanReferences = new ArrayList<>();
List<Element> declareParents = DomUtils.getChildElementsByTagName(aspectElement, DECLARE_PARENTS);
for (int i = METHOD_INDEX; i < declareParents.size(); i++) {
Element declareParentsElement = declareParents.get(i);
beanDefinitions.add(parseDeclareParents(declareParentsElement, parserContext));
}
// We have to parse "advice" and all the advice kinds in one loop, to get the
// ordering semantics right.
NodeList nodeList = aspectElement.getChildNodes();
boolean adviceFoundAlready = false;
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (isAdviceNode(node, parserContext)) {
if (!adviceFoundAlready) {
adviceFoundAlready = true;
if (!StringUtils.hasText(aspectName)) {
parserContext.getReaderContext().error(
"<aspect> tag needs aspect bean reference via 'ref' attribute when declaring advices.",
aspectElement, this.parseState.snapshot());
return;
}
beanReferences.add(new RuntimeBeanReference(aspectName));
}
AbstractBeanDefinition advisorDefinition = parseAdvice(
aspectName, i, aspectElement, (Element) node, parserContext, beanDefinitions, beanReferences);
beanDefinitions.add(advisorDefinition);
}
}
AspectComponentDefinition aspectComponentDefinition = createAspectComponentDefinition(
aspectElement, aspectId, beanDefinitions, beanReferences, parserContext);
parserContext.pushContainingComponent(aspectComponentDefinition);
List<Element> pointcuts = DomUtils.getChildElementsByTagName(aspectElement, POINTCUT);
for (Element pointcutElement : pointcuts) {
parsePointcut(pointcutElement, parserContext);
}
parserContext.popAndRegisterContainingComponent();
}
finally {
this.parseState.pop();
}
}
/**
* Parses one of '{@code before}', '{@code after}', '{@code after-returning}',
* '{@code after-throwing}' or '{@code around}' and registers the resulting
* BeanDefinition with the supplied BeanDefinitionRegistry.
* @return the generated advice RootBeanDefinition
*/
private AbstractBeanDefinition parseAdvice(
String aspectName, int order, Element aspectElement, Element adviceElement, ParserContext parserContext,
List<BeanDefinition> beanDefinitions, List<BeanReference> beanReferences) {
try {
this.parseState.push(new AdviceEntry(parserContext.getDelegate().getLocalName(adviceElement)));
// create the method factory bean
RootBeanDefinition methodDefinition = new RootBeanDefinition(MethodLocatingFactoryBean.class);
methodDefinition.getPropertyValues().add("targetBeanName", aspectName);
methodDefinition.getPropertyValues().add("methodName", adviceElement.getAttribute("method"));
methodDefinition.setSynthetic(true);
// create instance factory definition
RootBeanDefinition aspectFactoryDef =
new RootBeanDefinition(SimpleBeanFactoryAwareAspectInstanceFactory.class);
aspectFactoryDef.getPropertyValues().add("aspectBeanName", aspectName);
aspectFactoryDef.setSynthetic(true);
// register the pointcut
AbstractBeanDefinition adviceDef = createAdviceDefinition(
adviceElement, parserContext, aspectName, order, methodDefinition, aspectFactoryDef,
beanDefinitions, beanReferences);
// configure the advisor
RootBeanDefinition advisorDefinition = new RootBeanDefinition(AspectJPointcutAdvisor.class);
advisorDefinition.setSource(parserContext.extractSource(adviceElement));
advisorDefinition.getConstructorArgumentValues().addGenericArgumentValue(adviceDef);
if (aspectElement.hasAttribute(ORDER_PROPERTY)) {
advisorDefinition.getPropertyValues().add(
ORDER_PROPERTY, aspectElement.getAttribute(ORDER_PROPERTY));
}
// register the final advisor
parserContext.getReaderContext().registerWithGeneratedName(advisorDefinition);
return advisorDefinition;
}
finally {
this.parseState.pop();
}
}
/**
* Creates the RootBeanDefinition for a POJO advice bean. Also causes pointcut
* parsing to occur so that the pointcut may be associate with the advice bean.
* This same pointcut is also configured as the pointcut for the enclosing
* Advisor definition using the supplied MutablePropertyValues.
*/
private AbstractBeanDefinition createAdviceDefinition(
Element adviceElement, ParserContext parserContext, String aspectName, int order,
RootBeanDefinition methodDef, RootBeanDefinition aspectFactoryDef,
List<BeanDefinition> beanDefinitions, List<BeanReference> beanReferences) {
RootBeanDefinition adviceDefinition = new RootBeanDefinition(getAdviceClass(adviceElement, parserContext));
adviceDefinition.setSource(parserContext.extractSource(adviceElement));
adviceDefinition.getPropertyValues().add(ASPECT_NAME_PROPERTY, aspectName);
adviceDefinition.getPropertyValues().add(DECLARATION_ORDER_PROPERTY, order);
if (adviceElement.hasAttribute(RETURNING)) {
adviceDefinition.getPropertyValues().add(
RETURNING_PROPERTY, adviceElement.getAttribute(RETURNING));
}
if (adviceElement.hasAttribute(THROWING)) {
adviceDefinition.getPropertyValues().add(
THROWING_PROPERTY, adviceElement.getAttribute(THROWING));
}
if (adviceElement.hasAttribute(ARG_NAMES)) {
adviceDefinition.getPropertyValues().add(
ARG_NAMES_PROPERTY, adviceElement.getAttribute(ARG_NAMES));
}
ConstructorArgumentValues cav = adviceDefinition.getConstructorArgumentValues();
cav.addIndexedArgumentValue(METHOD_INDEX, methodDef);
Object pointcut = parsePointcutProperty(adviceElement, parserContext);
if (pointcut instanceof BeanDefinition) {
cav.addIndexedArgumentValue(POINTCUT_INDEX, pointcut);
beanDefinitions.add((BeanDefinition) pointcut);
}
else if (pointcut instanceof String) {
RuntimeBeanReference pointcutRef = new RuntimeBeanReference((String) pointcut);
cav.addIndexedArgumentValue(POINTCUT_INDEX, pointcutRef);
beanReferences.add(pointcutRef);
}
cav.addIndexedArgumentValue(ASPECT_INSTANCE_FACTORY_INDEX, aspectFactoryDef);
return adviceDefinition;
}
private String buildDescription(BeanReference adviceReference, BeanDefinition pointcutDefinition) {
return "Advisor <advice(ref)='" +
adviceReference.getBeanName() + "', pointcut(expression)=[" +
pointcutDefinition.getPropertyValues().get("expression") + "]>";
}
private String buildDescription(BeanReference adviceReference, BeanReference pointcutReference) {
return "Advisor <advice(ref)='" +
adviceReference.getBeanName() + "', pointcut(ref)='" +
pointcutReference.getBeanName() + "'>";
}
@Override
public BeanReference[] getBeanReferences() {
return this.beanReferences;
}
@Override
public BeanReference[] getBeanReferences() {
return this.beanReferences;
}