Results 1 to 7 of 7

Thread: @Autowired

  1. #1
    Join Date
    Dec 2010
    Posts
    10

    Default @Autowired

    Hi all, I have problem with @Autowired.

    I have config.xml with this beans

    PHP Code:
    <bean id="HeroDaoImpl" class="com.epos.kernel.spring.model.HeroDaoImpl">  </bean>
        <
    bean id="Hero" class="com.epos.kernel.spring.model.Hero">  </bean
    and class

    PHP Code:
    public class MainHero {


        @
    Autowired
        
    @Qualifier("HeroDaoImpl")
        private static 
    HeroDaoImpl heroDaoImpl;
        @
    Autowired
        
    @Qualifier("Hero")
        private static 
    Hero objHero;

        public static 
    void main(String[] args) {

            
    ApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");
            
    SessionFactory sessionFactory = (SessionFactorycontext.getBean("sessionFactory");


            
    objHero.setName("1231");
                    

        }

    in IDEA I see link near private static HeroDaoImpl heroDaoImpl; and private static Hero objHero; after click on this link I move into config.xml bean. But if I run my application I have got this exception

    PHP Code:
    Exception in thread "main" java.lang.NullPointerException
        at com
    .epos.testspring.MainHero.main(MainHero.java:30)
        
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        
    at java.lang.reflect.Method.invoke(Method.java:597)
        
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110
    exception on this link objHero.setName("1231");
    why ? I have method etName, and I add @Autowired

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    You cannot inject statics with @Autowired next to that spring only injects beans it knowns about. Your AppMain class isn't a spring managed bean so it is never going to be injected...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Dec 2010
    Posts
    10

    Default

    ОК. thanks I have changed my class, look it

    PHP Code:
    package com.epos.testspring;

    import com.epos.kernel.spring.model.Hero;
    import com.epos.kernel.spring.model.HeroDaoImpl;
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class 
    MainHero {


        @
    Autowired
        
    @Qualifier("HeroDaoImpl")
        private 
    HeroDaoImpl heroDaoImpl;
        @
    Autowired
        
    @Qualifier("Hero")
        private 
    Hero objHero;

        public static 
    void main(String[] args) {

            
    MainHero start = new MainHero();
            
    start.testBeans();

        }


        public 
    Hero testBeans(){

            
    objHero.setName("1231");

            return 
    objHero;

        }


    because error don`t change ((

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    As stated spring doesn't know about the bean... You are using your own instance... I suggest a basic spring tutorial on dependency injection.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Dec 2010
    Posts
    10

    Default

    can you show me example?

  6. #6
    Join Date
    Dec 2010
    Posts
    10

    Default

    Ок, I have changed my class, this work well


    PHP Code:
    public class AutowiredTest {
        @
    Autowired
        
    @Qualifier("HeroDaoImpl")
        private 
    HeroDaoImpl heroDaoImpl;

        @
    Autowired
        
    @Qualifier("Hero")
        private 
    Hero objHero;

        public 
    Hero testBeans(){

            
    ApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");
            
    SessionFactory sessionFactory = (SessionFactorycontext.getBean("sessionFactory");
            
            
    XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("db-config.xml"));

           
            
    objHero =(Herocontext.getBean("Hero");
            
    objHero.setChoice("Hello");
            
    System.out.println(objHero.getChoice());

            
                          

            return 
    objHero;
        }        


    but if I add thisline
    PHP Code:
     heroDaoImpl = (HeroDaoImplcontext.getBean("HeroDaoImpl"); 
    want to get contest, I have got this exaption
    PHP Code:

    Exception in thread 
    "main" java.lang.ClassCastException$Proxy14 cannot be cast to com.epos.kernel.spring.model.HeroDaoImpl
    Hello
        at com
    .epos.testspring.AutowiredTest.testBeans(AutowiredTest.java:34)
        
    at com.epos.testspring.MainHero.main(MainHero.java:12)
        
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        
    at java.lang.reflect.Method.invoke(Method.java:597)
        
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110
    Who can help me?

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    As I stated earlier I suggest a tutorial (and/or the reference guide)... I suggest especially a tutorial about classes and programming to interfaces (currently you use interfaces, but program to concrete classes).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •