Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8745121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:57:17+00:00 2026-06-13T11:57:17+00:00

When we annotate a class as @Autowired , does it HAVE to be a

  • 0

When we annotate a class as @Autowired, does it HAVE to be a interface or can it be a class?

All the examples of using Spring I have seen, use an interface and then implement it on a class. The interface type is then used to call a function on the Concrete class. Can we not just simply add @Autowired to a concrete class rather than an interface.

I know of the Programme to a interface analogy in JAVA, but if u are not depending on Polymorphism, then why to write an interface?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:57:18+00:00Added an answer on June 13, 2026 at 11:57 am

    No, you don’t have to use interfaces, this is completely fine as far as Spring is concerned:

    @Service
    public class FooService {
        @Autowired
        private FooDao fooDao;
    }
    

    or you can even go for construction injection:

    @Service
    public class FooService {
    
        private final FooDao fooDao;
    
        public FooService(FooDao fooDao) {
            this.fooDao = fooDao;
        }
    }
    

    Often interfaces are anachronic practice repeated by every subsequent generation. Don’t use them if they are not required. And they aren’t required if they will always have just one implementation or if you want to mock such a class (modern mocking frameworks mock classes without any problem).

    There is also nothing wrong in injecting concrete classes, like FooDao in example above. It has some technical implications wrt. proxying, but nothing that can’t be comprehended.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using spring 3 mvc/security frameworks. I have created a Controller class that
I'm using Spring 3.1.2 with Hibernate 4 . I have a DAO implementation class
If I annotate a method of a class with an attribute, I can then
According to the class ElementType in the API, you can annotate local variables and
If I have a Spring Controller with two SEPARATE methods, one annotated by: @ExceptionHandler(Exception.class)
I have a Spring MVC Controller in a very XML-slimmed application, we use a
In my Spring context file I have something like this: <bean id=userCheck class=a.b.c.UserExistsCheck/> <aop:config>
I'd like to be able to annotate a public class/interface in Java as @NonPublic
When I annotate a Java class with @WebService and use it with JAX-WS RI
Situation : I have I class with property annotated with @Autowired: public class MyClass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.