Spring Tutorial 19 - The Autowired Annotation

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ส.ค. 2024
  • In this tutorial, we'll use the Autowired annotation to wire up dependencies. We'll learn how to add dependencies by type and name. We'll also use Qualifiers to narrow down dependency contenders.

ความคิดเห็น • 111

  • @nehaagrawal1730
    @nehaagrawal1730 11 ปีที่แล้ว +3

    Thanks Koushik!! I have watched your 20 videos till now and I am so impressed with you. May God bless you!!

  • @Neran280
    @Neran280 ปีที่แล้ว +1

    if you still get the NoUniqueBeanDefinitionException then add to your xml config.

  • @shuyiyang157
    @shuyiyang157 9 ปีที่แล้ว +29

    I use following xml configuration code and it works.








    • @ZeeshanMRaje
      @ZeeshanMRaje 6 ปีที่แล้ว

      thanx sir, I am 13'th person who appreciate your help

  • @logeshbalaji6264
    @logeshbalaji6264 หลายเดือนก่อน

    Very easy to understand, Thank you very much Koushik🙂

  • @kanchankulkarni6937
    @kanchankulkarni6937 12 ปีที่แล้ว +1

    All the spring tutorials I am following & its very useful for new learners like me.Thanks a lot Kaushik for creating & uploading such a clean & easy to understand videos. :)

  • @Chmilou1
    @Chmilou1 10 ปีที่แล้ว +8

    You can use aliases. Add this line to the spring.xml file:

  • @CryptoJaat
    @CryptoJaat 11 ปีที่แล้ว +3

    @koushik, I really appreciate the hard work and effort you have put into creating these videos. You are surely getting my blessings and thanks and I am sure from so many other fortunate as well.
    One more request from your, I am sure very busy, schedule, can you also provide tutorials of Spring MVC ?
    Thanks again.
    AJ

  • @aashishganesh
    @aashishganesh 8 ปีที่แล้ว +23

    Please make you videos on new version of spring

  • @linchenSeth
    @linchenSeth 5 ปีที่แล้ว +1

    First point: add in your xml; second point: just link Bean name instead of field will work

  • @willhdq
    @willhdq 7 ปีที่แล้ว +1

    For people with error: (expected single matching bean but found 3 (or 4): ... ), update your pom.xml with this:
    org.springframework
    spring
    2.5.6
    for:
    org.springframework
    spring-core
    4.1.4.RELEASE
    org.springframework
    spring-context
    4.1.4.RELEASE
    notice the
    (spring-core in )
    and
    (spring-context in )
    (stackoverflow.com/questions/23813369/spring-java-error-namespace-element-annotation-config-on-jdk-1-5-and-high/29647933#29647933)
    (stackoverflow.com/questions/20894695/spring-configuration-xml-schema-with-or-without-version)
    (mvnrepository.com/artifact/org.springframework/spring-orm/4.1.4.RELEASE)

    • @bpnaidu4u
      @bpnaidu4u 2 ปีที่แล้ว

      I too face same issue, am using spring-context-5.2.9.jar version, spring-core-5.2.9.jar version. if you know any solution , please share

    • @bpnaidu4u
      @bpnaidu4u 2 ปีที่แล้ว

      I have added this line in spring.xml under bean tag . Now it is working as expected. And removed bean tag of class org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor

  • @chhayasharma7399
    @chhayasharma7399 4 ปีที่แล้ว +2

    sound of this expensive bike or car always catches my attention :D

  • @James5Sapphire
    @James5Sapphire 5 ปีที่แล้ว +1

    If you are getting the IllegalArgumentException after adding the then upgrade your version of spring to the latest one / one which is compatible with your JDK

  • @LeBadman
    @LeBadman 12 ปีที่แล้ว

    If you are having problems with the annotations config element, you must make sure that you provide the right spring-context strings in your schemaLocation part of the bean defintions tag.

  • @vamshikrishna7292
    @vamshikrishna7292 9 ปีที่แล้ว +10

    XML Namespace might have changed for spring 4.1.5. This is the latest one

    • @Brax1982
      @Brax1982 9 ปีที่แล้ว

      Vamshi Krishna Specifically without the doctype that Koushik uses, I would like to add. Apparently no longer necessary.

    • @MissPiggyM976
      @MissPiggyM976 8 ปีที่แล้ว

      Thanks Vamshi Krishna !

    • @vipul1246
      @vipul1246 5 ปีที่แล้ว

      thanks
      it worked

  • @kumudamv
    @kumudamv 7 ปีที่แล้ว

    Awesome:)Well presented/summarized from spring io docs:)I 'm kind of lazy who don't read the whole documentation.But you made it so simple and tried max to cover from docs

  • @BanuKones
    @BanuKones 9 ปีที่แล้ว +6

    Hi Koushik, Thank you for these great tutorials. I have notice some thing when I follow your tutorial. It looks like the AutowiredAnnotationBeanPostProcessor is actually matching the parameter name instead of the member variable name.
    changing the member variable to a different name actually runs without any problem. even changing the setter method name runs without any problem. Only when I change the parameter name it actually throws the exception.
    to illustrate.
    in xml file



    in Code Cicle.java
    changing the member variable name from circle to circle1 did not break the program.
    I went ahead and change the setter as well
    public void setCentre1(Point centre) // not "1" prefixed But still the program runs
    only when I change the parameter name it failed
    public void setCentre(Point centre1) // look at the parameter name is centre1, this time I got the BeanCreationException.
    I was expecting the exception in the previous cases (when member variable name didn't match or the setter Method name didn't match).
    I am using spring Version 4.1.3

    • @Brax1982
      @Brax1982 9 ปีที่แล้ว

      Banugoban Kones Yes, since 2.5 aka ever since annotations were supported: docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/annotation/Autowired.html
      "Config methods[..]each of those arguments will be autowired with a matching bean in the Spring container. Bean property setter methods are effectively just a special case of such a general config method."
      This will easily lead to confusion if you try to autowire and do a rename refactoring on the member variable in the process. That will (by default in Eclipse) not rename the arguments of the setters. It's best practice to have those match with the member variables. Therefore this problem can be avoided by adhering to (and making your IDE adhere to) the practice.

    • @davidguiney1746
      @davidguiney1746 8 ปีที่แล้ว

      This makes sense as the member variable in the class is private.

    • @kzanonymscatalyst2196
      @kzanonymscatalyst2196 6 ปีที่แล้ว +1

      Add this to resolve placeholders with $ interpretations.

    • @adilmuthukoya2272
      @adilmuthukoya2272 5 ปีที่แล้ว

      You can give the @Autowired annotation on setter method or the member variable directly , What Kaushik has mentioned applicable to member variable , if you are giving the annotation to setter function then the setter function parameter is cross checked not the variable name

  • @rjiang21
    @rjiang21 10 ปีที่แล้ว +3

    I get this error: Could not autowire method: public void com.mypackage.Circle.setCenter(com.techsharp.Point); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.mypackage.Point] is defined: expected single matching bean but found 3: pointA,pointB,pointC. I have to set Primary bean in the xml in order to make to work... I still want to know why qualifier is not working....

  • @114huseyn
    @114huseyn 7 ปีที่แล้ว +8

    hi guys, i was getting the same erros about @qualifier but when i defined PostProcessors to it worked.

    • @zebashabnam4962
      @zebashabnam4962 5 ปีที่แล้ว

      Thanks a lot it worked for me as well

    • @zebashabnam4962
      @zebashabnam4962 5 ปีที่แล้ว

      But i am yet to understand the issue why it was unable to run the same without adding it although in the tutorials they haven't added anything like this!

    • @demigod6190
      @demigod6190 3 ปีที่แล้ว

      This is work for me.

    • @despotjc3144
      @despotjc3144 3 ปีที่แล้ว

      @@demigod6190 work for me to ffff something weird stuff istn it

  • @elfarsante
    @elfarsante 9 ปีที่แล้ว

    Great tutorials! thank you very much!

  • @manasa6687
    @manasa6687 8 ปีที่แล้ว +4

    Hello all, I was getting the error - the @Qualifier annotation is disallowed in this location. I was unable to locate the fix to this error after intensive search.
    I was using all .jar files below 3.0 version. After changing the jar files to 3.0 and above version, the error got fixed. Hope this would be helpful to anyone out there facing this issue.

    • @ShivaKumar-ns2oc
      @ShivaKumar-ns2oc 8 ปีที่แล้ว

      +Manasa Mattur We really appreciate your help. Thank you.

    • @James5Sapphire
      @James5Sapphire 5 ปีที่แล้ว

      It worked

  • @chaoschao9432
    @chaoschao9432 6 ปีที่แล้ว

    autowire firstly find the type, then find the first matched bean to wire.

  • @RahulRaina04
    @RahulRaina04 8 ปีที่แล้ว

    Would any version of Spring not work with Eclipse Indigo? As while setting up the Xml namespace for @Required annotation, i got an error and am unable to resolve it. Help me urgently.

  • @1989pabloaimar
    @1989pabloaimar 8 ปีที่แล้ว +2

    Hi everyone,
    I have a problem. Everytime I paste to my xml file, the java.lang.IllegalArgumentException is thrown. My code doesn't throw an exception without but then I can't use annotations.

    • @James5Sapphire
      @James5Sapphire 5 ปีที่แล้ว

      Same problem here ..

    • @rogernkosi8420
      @rogernkosi8420 5 ปีที่แล้ว

      @@James5Sapphire update JDK and use most recent spring version

  • @zaycef03
    @zaycef03 7 ปีที่แล้ว

    Okey, but if I have specified more than just one variable in class and some of them are the same type and some of them not, will the autowiring work or it would be to complex to it ?

  • @Santoshchaa
    @Santoshchaa 7 ปีที่แล้ว

    is autowire annotation work when we are initializing the IOC container with Beanfacory interface?. like
    BeanFactory apc=new XmlBeanFactory(new ClassPathResource("Spring-context.xml"));
    in my case it only working with applicationcontaxt implementation.

  • @francescopiazza4882
    @francescopiazza4882 8 ปีที่แล้ว

    Thanks a lot Koushik!

  • @raman-jn6yt
    @raman-jn6yt 6 ปีที่แล้ว

    How to find namespaces if you haven't added JARS manually but using maven?

  • @remusik04
    @remusik04 12 ปีที่แล้ว +4

    Hi,
    I discovered that you can also use an alias instead of changing the name of the bean when using @Autowired . So instead of changing the name of the bean from "pointA" to "center" so that Autowired finds the name, you can also leave the bean name set to "pointA" and also create an alias.
    Thanks again for the very good tutorials. Can you also provide tutorials of Spring MVC ?

  • @user-cc7dk2ci2e
    @user-cc7dk2ci2e 7 ปีที่แล้ว

    Hello I get a TypeMismatchException setting the centre of the Circle if I keep
    Instead of
    Before @Autowire the property file was working fine.

  • @davidguiney1746
    @davidguiney1746 8 ปีที่แล้ว

    Does @Autowired compare the name of the bean in the xml to the member variable itself, or the name of the argument in the setter method?

    • @davidguiney1746
      @davidguiney1746 8 ปีที่แล้ว

      Ok, sorry, that q is already answered below by Banugoban Kones. This makes sense as the member variable is private.

  • @despotjc3144
    @despotjc3144 3 ปีที่แล้ว

    if you getting error like this :
    Error creating bean with name 'pointA' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'x'; nested exception is java.lang.NumberFormatException: For input string: "${pointA.pointX}"
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pointA' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is ...bla bla
    than add the this cod lines

  • @VIRAJBHOSLE
    @VIRAJBHOSLE 5 ปีที่แล้ว

    It works fine without the tag in xml

  • @parth191079
    @parth191079 10 ปีที่แล้ว

    Can someone tell me that what is the need for @Qualifier annotation? Why Spring cannot use some annotation to just link the property ("circle" in this case) with the bean defined in the properties file ("pointA" in this case)? I found this unessential an just an overhead until there is some valid use of @Qualifier annotation.

    • @jaykhimani
      @jaykhimani 10 ปีที่แล้ว +1

      Qualifier is useful when you have multiple implementation of same interface and you are injecting the interface in different service/component and each of those service/component requires different implementation. For example you are generating HTML and XML representation of an object model say - Person, then you define an interface Representation with method write(Person person) and then HtmlRepresentation implementation of Representation will generate HTMl of Person and XmlRepresentation implementation of Representation will generate XML for Person. So keeping interface (write(Person person)) same you can have different representation based on context for example in this case HTML in case of web and XML in case of API. If you have only one implementation of interface, Qualifier is not required. Hope this helps

    • @parth191079
      @parth191079 10 ปีที่แล้ว

      Jay, short and sweet explanation! You have been always my inspiration for J2EE. Looks like you have worked a lot in Spring. What all modules have you worked on? I have started learning.

    • @jaykhimani
      @jaykhimani 10 ปีที่แล้ว

      Glad that explanation helped you. Yes I've been working on Spring for quite a sometime now. Worked with many core, web, ws, data, security etc. Spring is great framework developed by developers for developers.

  • @LateThanNever7
    @LateThanNever7 7 ปีที่แล้ว

    No need to add the bean for Qualifier annotation ? In the video it worked without adding the bean for Qualifier !!

  • @dp_prajapati
    @dp_prajapati 11 ปีที่แล้ว

    Nicely explained..

  • @snehi23
    @snehi23 11 ปีที่แล้ว

    Hi Koushik, Thanks for video uploads !!!
    I was trying to use @Autowired with @Qualifier on setter "public void setCentre(Point centre)" method but its giving error of "The annotation @Qualifier is disallowed for this location". I tried googling to find answer and stumble upon a link saying " @Qualifier is only allowed on fields and attributes not methods (you need to put it on the method attribute)." Does this implementation spring version specific? or Did i miss anything?

  • @teonapastravanu936
    @teonapastravanu936 12 ปีที่แล้ว

    or he could use syso + ctrl + space for System.out.println();, but I guess he wants to show us how things get done step by step. This a very good tutorial ...

  • @amanvikram04
    @amanvikram04 7 ปีที่แล้ว

    why did you removed the xml doctype tag?

  • @amanvikram04
    @amanvikram04 7 ปีที่แล้ว

    why cant you import the packages before ?

  • @deshpandeble
    @deshpandeble 11 ปีที่แล้ว

    Koushik, can we have multiple qualifiers to single bean?

  • @vamshi498
    @vamshi498 11 ปีที่แล้ว

    To Comment
    ctrl+shift+/
    To Uncomment
    ctrl+shift+\
    To Format the code
    ctrl+shift+F
    To import the related dependencies
    ctrl+shift+O

  • @alphaflame7
    @alphaflame7 9 ปีที่แล้ว

    In the Circle class, when I add @Autowired and @Qualifier("circleRelated"). Eclipse shows an error saying:" The annotation @Qualifier is disallowed for this location". This happens even when I have the imports such as "...annotation.Autowired and ...annotation.Qualifier. Does anyone else no how to resolve this?

    • @alphaflame7
      @alphaflame7 9 ปีที่แล้ว

      This is the error I get: The annotation @Qualifier is disallowed for this location
      Near this snippet of code:
      @Autowired
      @Qualifier("circleRelated")
      public void setCenter(Point center) {
      this.center = center;
      }

    • @alphaflame7
      @alphaflame7 9 ปีที่แล้ว

      These are my import in the circle class:
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Qualifier;

    • @alphaflame7
      @alphaflame7 9 ปีที่แล้ว

    • @xybnedasdd2930
      @xybnedasdd2930 9 ปีที่แล้ว +2

      Michael Jones
      Try to copy from here:
      www.tutorialspoint.com/spring/spring_annotation_based_configuration.htm

  • @roshansalvi81
    @roshansalvi81 13 ปีที่แล้ว

    Awesome.. I am also looking out for some web application example using JTA MAVEN

  • @kavyasrinivas6962
    @kavyasrinivas6962 6 ปีที่แล้ว +1

    Can you explain it without XML file

  • @kanchanup
    @kanchanup 12 ปีที่แล้ว

    Hey Koushik, first of all, thank you very much for posting these videos, really useful! My question is, are those post processor bean declarations not required in Spring 3.1? I am using Spring 3.1, and my code works (@Required and @Autowired) without those bean post processor declarations!

    • @amolsahane3381
      @amolsahane3381 2 ปีที่แล้ว +1

      there might be a chance you have defined a tag in your spring xml file. If you have declared this tag you don't need to declare post processors explicitly.

  • @ankitrathore3946
    @ankitrathore3946 9 ปีที่แล้ว +1

    can a bean have multiple qualifier

    • @Brax1982
      @Brax1982 9 ปีที่แล้ว

      Ankit Rathore No. The last qualifier overwrites all the others. But you can give a bean as many aliases (see earlier video) as you want and those actually match with the Qualifier annotation, as well (as per Spring version 4.2.0), so that's essentially the same as multiple qualifiers.

    • @ankitrathore3946
      @ankitrathore3946 9 ปีที่แล้ว

      thanks..

  • @sridug84
    @sridug84 11 ปีที่แล้ว

    i tried to use qualified annotation. But it is not detecting. I'm still getting Error creating bean with name 'circle': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.sri.javabrains.Circle.setCenter(org.sri.javabrains.Point); nested exception.
    Can u plz help me to solve this problem.
    Thanks a lot for the vedios

  • @alekszinikandar
    @alekszinikandar 7 ปีที่แล้ว

    Where and how can I find current xml namespaces?

    • @alekszinikandar
      @alekszinikandar 7 ปีที่แล้ว

      Found them... these worked for me:

  • @tiagopereira6292
    @tiagopereira6292 7 ปีที่แล้ว +2

    In some cases...

  • @LeBadman
    @LeBadman 12 ปีที่แล้ว

    In my case it's ctrl+shift+c

  • @LeBadman
    @LeBadman 12 ปีที่แล้ว

    ctrl shift - collapse everything

  • @parthsalat3915
    @parthsalat3915 2 ปีที่แล้ว +1

    Koush

  • @BenderFesk
    @BenderFesk 11 ปีที่แล้ว

    V8 automatic @ 00:29

  • @SoeaOu
    @SoeaOu 12 ปีที่แล้ว

    ctrl+shift+/ - conversion to comment

  • @aashishganesh
    @aashishganesh 8 ปีที่แล้ว

    Please

  • @npggreat
    @npggreat 11 ปีที่แล้ว

    This also works
    Comment/Uncomment = Ctrl+Shift+C

  • @kalyank3825
    @kalyank3825 7 ปีที่แล้ว

    Very slow tutorial and dragged topics