0

Can abstract class be autowired in another class?

Suppose we have a bean

     <bean id = "Utils" class = "com.org.appl.Utils" abstract = "true">
          <property name = "javaUtils" ref = "javaUtils"/>
        </bean>

can we use this

bean like:

      Public class Calculation


   {
    @Autowired
    private Utils utils;
    .......................
    }
  • There is a difference between an abstract Java class and an abstract Spring bean. Is your class which you referenced in the bean also abstract, or only the bean definition itself? – dunni Jan 26 '17 at 14:11
  • Possible duplicate of [What is meant by abstract="true" in spring?](http://stackoverflow.com/questions/9397532/what-is-meant-by-abstract-true-in-spring) – dunni Jan 26 '17 at 14:11
  • Utils have some properties defined and only abstract=true is set in bean definition.So this bean cannot be autowired in any concrete class? – Priyanka L Verma Jan 27 '17 at 06:55

1 Answers1

1

NO

abstract=true is used in spring-xml files to create a group of properties, it is not the same as an abstract class.

Abstract classes can't be @Autowired because they can't be created, this is how Java work.

There are cases where Spring will take an abstract class and create an implementation at runtime, by using CGLIB to emit bytecode, for instance when using lookup method injection

Klaus Groenbaek
  • 4,820
  • 2
  • 15
  • 30
  • Utils have some properties defined and only abstract=true is set in bean definition.So this bean cannot be autowired in any concrete class? – Priyanka L Verma Jan 27 '17 at 06:55