- 推荐使用 Java-based configuration ,也可以使用xml
- we generally recommend that your primary source be a single
@Configuration
class. Usually the class that defines themain
method is a good candidate as the primary@Configuration
- Importing Additional Configuration Classes
You need not put all your @Configuration into a single class.
The @Import annotation can be used to import additional configuration classes.
Alternatively, you can use @ComponentScan to automatically pick up
all Spring components, including @Configuration classes.- 示例
Car.java
package javabeat.net.basic;
public interface Car {
public void print();
} Toyota.java
package javabeat.net.basic;
import org.springframework.stereotype.Component;
@Component
public class Toyota implements Car{
public void print(){
System.out.println("I am Toyota");
}
} Volkswagen.java
package javabeat.net.basic;
import org.springframework.stereotype.Component;
@Component
public class Volkswagen implements Car{
public void print(){
System.out.println("I am Volkswagen");
}
} JavaConfigA.java
package javabeat.net.basic; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JavaConfigA {
@Bean(name="volkswagen")
public Car getVolkswagen(){
return new Volkswagen();
}
} JavaConfigB.java
package javabeat.net.basic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JavaConfigB {
@Bean(name="toyota")
public Car getToyota(){
return new Toyota();
}
} ParentConfig.java
package javabeat.net.basic;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({JavaConfigA.class,JavaConfigB.class})
public class ParentConfig {
//Any other bean definitions
} ContextLoader.java
package javabeat.net.basic;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class ContextLoader {
public static void main (String args[]){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ParentConfig.class);
Car car = (Toyota)context.getBean("toyota");
car.print();
car = (Volkswagen)context.getBean("volkswagen");
car.print();
context.close();
}
}- If you run the above example,the printed output will be,
- Importing XML Configuration
If you absolutely must use XML based configuration,
we recommend that you still start with a @Configuration class.
You can then use an @ImportResource annotation to load XML configuration files.
相关文章
- 08-12Spring boot 官网学习笔记 - Using Spring Boot without the Parent POM,但是还要使用Parent POM提供的便利
- 08-12Spring Boot的学习之路(02):和你一起阅读Spring Boot官网
- 08-12Spring boot 官网学习笔记 - 开发第一个Spring boot web应用程序(使用mvn执行、使用jar执行)
- 08-12Spring boot 官网学习笔记 - Spring Boot CLI 入门案例
- 08-12Spring boot 官网学习笔记 - Auto-configuration(@SpringBootApplication、@EnableAutoConfiguration、@Configuration)
- 08-12Spring boot 官网学习笔记 - Spring DevTools 介绍
- 08-12Spring boot 官网学习笔记 - Configuration Class(@import)
- 08-12Spring boot 官网学习笔记 - logging
- 08-12Spring boot 官网学习笔记 - Spring Boot 属性配置和使用(转)-application.properties