我正在学习Spring Annotation,我想知道每个注释如何与xml配置相似。 无论如何,我想在没有任何xml文件的情况下使用Spring。所以我开始使用JavaConfig。
为了告诉Spring,我们正在使用JavaConfig,我们正在使用@Configuration。
现在我正在研究@Bean和@Autowired是如何工作的。
@Bean用于定义与xml配置类似的bean
<bean></bean>
下面是我的示例代码,它可以很好地测试@Configuration和@Bean:
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.example.service.Greeting;
@Configuration
public class AnotherConfig {
@Bean
public Greeting getGreet(){
return new 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:
In Greeting Constructor
obj3 (com.example.service.Greeting@3043fe0e)
Hello Greeting Message
现在我想在上面的例子中使用@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工作所必需的,(http://www.basilv.com/psd/blog/2009/java-based-configuration-of-spring-dependency-injection) 我使用的是Spring 3.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)