配置Spring一般包括3中方式
- 基于XML的配置文件
- 基于注解的配置
- 基于Java的配置
基于XML的配置
這個(gè)很常見枚尼,比如:
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld">
<property name = "message" value = "Hello World!"/>
</bean>
</beans>
這種配置可以通過容器來加載,也可以通過FileSystemXmlApplicationContext砂吞、ClassPathXmlApplicationContext 等來加載署恍,如:
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.getBean("helloWorld");
使用注解
配置了<context:annotation-config />開啟注解
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!-- bean definitions go here -->
</beans>
注解 | 說明 |
---|---|
@Required | 用于bean屬性的setter方法 |
@Autowired | 可以應(yīng)用于bean屬性setter方法、非setter方法蜻直、構(gòu)造函數(shù)和屬性 |
@Qualifier | @Qualifier注解和@Autowired一起可以通過指定連接哪個(gè)bean來消除混淆盯质。 |
JSR-250 Annotations | Spring支持基于JSR-250的注解,包括@Resource概而、@PostConstruct和@PreDestroy注解呼巷。 |
基于java的方式
@Configuration & @Bean
使用@Configuration注解一個(gè)類表明該類可以被Spring IoC容器用作bean定義的來源。該@Bean注解告訴Spring與@Bean注釋的方法將返回應(yīng)注冊(cè)為Spring應(yīng)用程序上下文的bean的對(duì)象赎瑰。最簡(jiǎn)單的@Configuration類如下:
package com.tutorialspoint;
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld(){
return new HelloWorld();
}
}
以上代碼將等同于以下XML配置 -
<beans>
<bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld" />
</beans>
這里王悍,方法名用@Bean作為bean ID進(jìn)行注釋,并創(chuàng)建并返回實(shí)際的bean餐曼。你的配置類可以擁有多個(gè)@Bean的聲明压储。一旦你的配置類被定義鲜漩,你可以使用AnnotationConfigApplicationContext加載并提供給Spring容器,如下所示:
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfig.class);
HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
helloWorld.setMessage("Hello World!");
helloWorld.getMessage();
}
可以按如下方式加載各種配置類:
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class, OtherConfig.class);
ctx.register(AdditionalConfig.class);
ctx.refresh();
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}
@Import
@Import注解允許從另一個(gè)配置類加載@Bean定義
如配置類A
@Configuration
public class ConfigA {
@Bean
public A a() {
return new A();
}
}
可以在另一個(gè)Bean聲明中導(dǎo)入上面的Bean聲明集惋,如下所示
@Configuration
@Import(ConfigA.class)
public class ConfigB {
@Bean
public B a() {
return new A();
}
}
現(xiàn)在孕似,不需要在實(shí)例化上下文時(shí)指定ConfigA.class和ConfigB.class,只需要提供ConfigB刮刑,如下所示
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigB.class);
// now both beans A and B will be available...
A a = ctx.getBean(A.class);
B b = ctx.getBean(B.class);
}
生命周期回調(diào)
@Bean注解支持指定任意初始化和銷毀回調(diào)方法喉祭,就像Spring XML的bean元素的init-method和destroy-method屬性一樣
public class Foo {
public void init() {
// initialization logic
}
public void cleanup() {
// destruction logic
}
}
@Configuration
public class AppConfig {
@Bean(initMethod = "init", destroyMethod = "cleanup" )
public Foo foo() {
return new Foo();
}
}
指定Bean范圍
默認(rèn)范圍是單例,但您可以使用@Scope注釋覆蓋它雷绢,如下所示
@Configuration
public class AppConfig {
@Bean
@Scope("prototype")
public Foo foo() {
return new Foo();
}
}