Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing beans in xml config created via Annotations

Tags:

spring

This must be possible but I can't work out how or see it in the docs.

I need to reference a bean which has been created via an annotation @Service and context:component-scan, within a spring xml config file.

How is this achieved?

Cheers

like image 825
Ben Davies Avatar asked Dec 15 '09 18:12

Ben Davies


People also ask

How do I enable annotations in beans XML?

You can wire beans using XML or annotations. In case you use annotations, you need to activate annotations and you have to add <context:annotation-config /> in applicationContext. xml. This will simplify the structure of the tag from applicationContext.

How do I reference a bean of another XML file in Spring?

Just import the xml defining the bean with <import resource="otherXml. xml"> and you will be able to use the bean definition.

What is @bean annotation used for?

Spring @Bean Annotation is applied on a method to specify that it returns a bean to be managed by Spring context. Spring Bean annotation is usually declared in Configuration classes methods. In this case, bean methods may reference other @Bean methods in the same class by calling them directly.


1 Answers

  1. use @Service("myService")
  2. in the xml, use <property name="myProperty" ref="myService"/>
like image 158
Bozho Avatar answered Nov 15 '22 08:11

Bozho