@Autowired如何与JavaConfig一起工作

@Autowired如何与JavaConfig一起工作,java,spring,Java,Spring,我正在学习Spring注释,我想知道每个注释如何与xml配置类似。 无论如何,我想使用Spring而不使用任何xml文件。所以我开始使用JavaConfig 为了告诉Spring我们正在使用JavaConfig,我们正在使用@Configuration 现在我正在研究@Bean和@Autowired是如何工作的 @Bean用于定义与xml配置类似的Bean <bean></bean> 现在我想在上面的示例中使用@Autowired: packag

我正在学习Spring注释,我想知道每个注释如何与xml配置类似。 无论如何,我想使用Spring而不使用任何xml文件。所以我开始使用JavaConfig

为了告诉Spring我们正在使用JavaConfig,我们正在使用@Configuration

现在我正在研究@Bean和@Autowired是如何工作的

@Bean用于定义与xml配置类似的Bean

<bean></bean>
现在我想在上面的示例中使用@Autowired:

            package com.example.service;

            public class Greeting {
               public Greeting(){
                   System.out.println("In Greeting Constructor");
               }

               public String getMessage(){
                   return "Hello Greeting Message";
               }
            }


            package com.example.config;

            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.context.annotation.Bean;
            import org.springframework.context.annotation.Configuration;

            import com.example.service.Greeting;

            @Configuration
            public class AnotherConfig {

                @Autowired
                Greeting greeting;

                @Bean
                public Greeting getGreet(){
                    return this.greeting;
                }
            }       


            package com.example;

            import org.springframework.context.annotation.AnnotationConfigApplicationContext;
            import com.example.config.AnotherConfig;
            import com.example.service.Greeting;

            public class HelloApp {
                public static void main(String[] args) {
                    AnnotationConfigApplicationContext context1 = new AnnotationConfigApplicationContext(AnotherConfig.class);
                    Greeting obj3 = (Greeting)context1.getBean("getGreet");
                    System.out.println("obj3 ("+obj3+")");
                    System.out.println(obj3.getMessage());
                }
            }

            Error: Not working!!!

            Oct 15, 2016 2:49:04 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
            INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@439f5b3d: startup date [Sat Oct 15 02:49:04 SGT 2016]; root of context hierarchy
            Oct 15, 2016 2:49:05 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
            INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7d417077: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,anotherConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,getGreet]; root of factory hierarchy
            Exception in thread "main" obj3 (null)
            java.lang.NullPointerException
                at com.example.HelloApp.main(HelloApp.java:12)
我在谷歌上搜索过,在其中一个例子中,他们提到@AnnotationDrivenConfig是@Autowired工作所必需的,() 我使用的是Spring3.2,但是@AnnotationDrivenConfig没有解析,而且似乎不可用

因此,我尝试了ComponentScan的其他选项:

            package com.example.service;

            import org.springframework.stereotype.Component;

            @Component
            public class Greeting {
               public Greeting(){
                   System.out.println("In Greeting Constructor");
               }

               public String getMessage(){
                   return "Hello Greeting Message";
               }
            }


            package com.example.config;

            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.context.annotation.Bean;
            import org.springframework.context.annotation.ComponentScan;
            import org.springframework.context.annotation.Configuration;

            import com.example.service.Greeting;

            @Configuration
            @ComponentScan("com.example.service")
            public class AnotherConfig {

                @Autowired  
                @Bean
                public Greeting getGreet(Greeting greeting){
                    return greeting;
                }
            }   


            package com.example;

            import org.springframework.context.annotation.AnnotationConfigApplicationContext;
            import com.example.config.AnotherConfig;
            import com.example.service.Greeting;

            public class HelloApp {
                public static void main(String[] args) {
                    AnnotationConfigApplicationContext context1 = new AnnotationConfigApplicationContext(AnotherConfig.class);
                    Greeting obj3 = (Greeting)context1.getBean("getGreet");
                    System.out.println("obj3 ("+obj3+")");
                    System.out.println(obj3.getMessage());
                }
            }

            Output: Working with construtor injection

            In Greeting Constructor
            obj3 (com.example.service.Greeting@7a9273a8)
            Hello Greeting Message
但为什么不使用属性设置器处理同样的事情呢

            package com.example.config;

            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.context.annotation.Bean;
            import org.springframework.context.annotation.ComponentScan;
            import org.springframework.context.annotation.Configuration;

            import com.example.service.Greeting;

            @Configuration
            @ComponentScan("com.example.service")
            public class AnotherConfig {

                @Autowired  
                Greeting greeting;

                @Bean
                public Greeting getGreet(){
                    return this.greeting;
                }
            }   

            Error:
            Oct 15, 2016 2:54:41 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
            INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@439f5b3d: startup date [Sat Oct 15 02:54:41 SGT 2016]; root of context hierarchy
            Oct 15, 2016 2:54:42 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
            INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1134affc: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,anotherConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,greeting,getGreet]; root of factory hierarchy
            In Greeting Constructor
            obj3 (null)
            Exception in thread "main" java.lang.NullPointerException
                at com.example.HelloApp.main(HelloApp.java:12)

我会直接跳到带弹簧靴的现代Spring(4.3)。还请注意,不鼓励在字段上使用
@Autowired
,因为这会隐藏依赖项并导致微妙的错误;最好使用构造函数。从Spring4.3开始,如果bean类只有一个构造函数,那么您甚至不需要
@Autowired
。最后,
@Component
/
@Service
类在扫描时会自动注册为Bean,因此,如果您正在对它们进行组件扫描,则不需要使用
@Bean
方法。了解如何使用Spring引导还有一个注意事项——您不需要使用
@Configuration
来告诉Spring您正在使用注释,这就是Spring在使用注释后所寻找的,基本上相当于XML文件。感谢您的快速回复。例如,类似spring的现代SpringBoot应用程序的问题是使用SpringApplication注释..它在后台做什么..它是配置、EnableAutoConfiguration和ComponentScan注释的组合。当我开始研究每个注释及其作用时,我以Autowired结束,无法理解…这么多注释做着类似的事情,但在某些方面有所不同..比如注释配置和组件扫描..我会直接跳到使用Spring Boot的现代Spring(4.3)。还请注意,不鼓励在字段上使用
@Autowired
,因为这会隐藏依赖项并导致微妙的错误;最好使用构造函数。从Spring4.3开始,如果bean类只有一个构造函数,那么您甚至不需要
@Autowired
。最后,
@Component
/
@Service
类在扫描时会自动注册为Bean,因此,如果您正在对它们进行组件扫描,则不需要使用
@Bean
方法。了解如何使用Spring引导还有一个注意事项——您不需要使用
@Configuration
来告诉Spring您正在使用注释,这就是Spring在使用注释后所寻找的,基本上相当于XML文件。感谢您的快速回复。例如,类似spring的现代SpringBoot应用程序的问题是使用SpringApplication注释..它在后台做什么..它是配置、EnableAutoConfiguration和ComponentScan注释的组合。当我开始挖掘每个注释及其作用时,我以Autowired结束,无法理解…这么多注释做相似的事情,但在某些方面有所不同..比如注释配置和组件扫描。。
            package com.example.config;

            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.context.annotation.Bean;
            import org.springframework.context.annotation.ComponentScan;
            import org.springframework.context.annotation.Configuration;

            import com.example.service.Greeting;

            @Configuration
            @ComponentScan("com.example.service")
            public class AnotherConfig {

                @Autowired  
                Greeting greeting;

                @Bean
                public Greeting getGreet(){
                    return this.greeting;
                }
            }   

            Error:
            Oct 15, 2016 2:54:41 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
            INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@439f5b3d: startup date [Sat Oct 15 02:54:41 SGT 2016]; root of context hierarchy
            Oct 15, 2016 2:54:42 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
            INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1134affc: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,anotherConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,greeting,getGreet]; root of factory hierarchy
            In Greeting Constructor
            obj3 (null)
            Exception in thread "main" java.lang.NullPointerException
                at com.example.HelloApp.main(HelloApp.java:12)