spring 裝配bean有三種基本方式:
- xml配置方式
- java config方式
- 自動裝配方式
1. xml配置方式
示例bean:
public class UserInfo {
private String username;
public UserInfo() {}
public UserInfo(List<String> ns) {
//假裝有這個list
}
public UserInfo(String name) {
this.username = name;
}
public void setUsername(String name) {
this.username = username;
}
}
application.xml文件:
<?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.xsd">
<bean id="userInfo" class="cn.wyn.ssm.pojo.UserInfo"/>
</beans>
一些bean會依賴其他bean,spring 會根據(jù)上下文裝入所需依賴豆胸。注入依賴的方式主要有構(gòu)造器注入和屬性注入兩種悔据。
1.1 構(gòu)造器注入依賴
<?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.xsd">
<bean id="userInfo" class="cn.wyn.ssm.pojo.UserInfo">
<!--構(gòu)造器注入name屬性的值為Lily-->
<constructor-arg name="name" value="Lily"/>
</bean>
</beans>
也可以在constructor-arg 元素中以ref屬性注入其他配置的bean。
<?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.xsd">
<bean id="str" class="java.lang.String">
<!--構(gòu)造器注入值為Lily-->
<constructor-arg value="Lily"/>
</bean>
<bean id="userInfo" class="cn.wyn.ssm.pojo.UserInfo">
<!--構(gòu)造器注入的依賴引用自id 為str的bean-->
<constructor-arg name="name" ref="str"/>
</bean>
</beans>
如果裝入的依賴是集合List類型(其他map類型類似):
<bean class="cn.wyn.ssm.pojo.UserInfo">
<constructor-arg name="ns">
<list>
<ref bean="beanId1"/>
<ref bean="beanId2"/>
</list>
</constructor-arg>
</bean>
constructor-arg元素的name屬性的值對應(yīng)的是構(gòu)造函數(shù)的參數(shù)名伟叛。
1.2 屬性setter方法注入
<?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.xsd">
<bean id="userInfo" class="cn.wyn.ssm.pojo.UserInfo">
<!--通過UserInfo的setUsername()方法注入-->
<property name="username" value="Lily"/>
</bean>
</beans>
property元素的name的值對應(yīng)的就是setXxx方法的xxx成員變量勃教。
2. java config 配置方式
java config配置方式是通過注解來配置bean淤击。
示例:
@Configuration
class DemoConfig{
@Bean
public A setA() {
return new A();
}
// 方式1:執(zhí)行setA函數(shù)
@Bean
public B setB() {
return new B(setA());
}
// 方式2:Spring自動注入
@Bean
public B setB(A a) {
return new B(a);
}
}
使用@Configuration
注解注釋的類會被標(biāo)識為一個配置類,相當(dāng)于把該類作為spring的xml配置文件中的<beans>
,該類會作為一個單獨的spring容器故源。
在掃描該配置bean的時候會將其中的帶有@Bean
注解的方法依次執(zhí)行配置bean污抬,返回的bean默認(rèn)使用方法名作為bean的name。依賴注入使用方式2的spring自動注入方式相比方式1更優(yōu)雅绳军。
獲取該容器中的bean:
AnnotationConfigApplicationContext context2 = new AnnotationConfigApplicationContext(
DemoConfig.class);
context2.getBean("A");//獲取name為A的bean印机。
3. 自動裝配方式
@Configuration
@ComponentScan
public class DemoConfig {
}
使用了ComponentScan注解自動掃描,@ComponentScan注解如果不設(shè)置掃描的包的屬性矢腻,則默認(rèn)是掃描DemoConfig類所在包下的帶有@Component注解的Bean,并將帶有@Autowire和@Resource等注解的成員變量或者方法所需的依賴自動注入耳贬。
獲取該IOC容器中的Bean的方法同java config配置的方式踏堡。
ps:@Configuration
注解標(biāo)注的類其實也是一個Component猎唁,因為該注解的定義上標(biāo)注了@Component
注解咒劲,所以掃描的時候@Configuration
注解也會被包括進(jìn)去,并自動執(zhí)行該類中所有@Bean
注解的方法來配置Bean诫隅。
/**
* Configuration注解源碼
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
String value() default "";
}
4. 整合使用java config容器中的bean和xml配置容器中的bean
4.1 在java config中整合進(jìn)另一個java config容器
通過@Import
注解來實現(xiàn)
@Configuration
@Import(BConfig.class)
@ComponentScan
public class AConfig {
//此處省略一些@Bean方法
}
4.2 在java config 容器中整合進(jìn)另一個xml 的IOC容器中的bean
通過@ImportResource
注解來實現(xiàn)腐魂。
@Configuration
@ImportResource("classpath:xxx.xml")
@ComponentScan
public class AConfig {
//此處省略一些@Bean方法。
}
4.3 在xml 中整合另一個xml 中配置的bean逐纬。
通過<import>
元素來實現(xiàn)蛔屹。
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<import resource="classpath:xxx.xml"/>
</beans>
4.4 在xml 容器中整合java config中的bean
首先,我們想到的是直接將java config 類以bean的形式配置到xml中:
<bean class="cn.wyn.XxxConfig"/>
但是豁生,很明顯這種方法是不對的兔毒,因為注解方式是由spring中某些類來實現(xiàn)裝配的,使用java config方式會自動配置這些類到容器甸箱,但是xml方式不會自動配置這些類育叁,因此,我們需要手動配置這些類芍殖。
- 如果你想使用
@Autowired
注解豪嗽,那么就必須事先在xml中聲明AutowiredAnnotationBeanPostProcessor
這個Bean - 如果想使用
@ Resource
、@ PostConstruct
豌骏、@ PreDestroy
等注解就必須在xml中聲明CommonAnnotationBeanPostProcessor
這個Bean - 如果想使用
@PersistenceContext
注解龟梦,就必須在xml中聲明PersistenceAnnotationBeanPostProcessor
的Bean。 - 如果想使用
@Required
的注解窃躲,就必須在xml中聲明RequiredAnnotationBeanPostProcessor
這個Bean计贰。
不過這些配置都可以直接用一條配置來替代:
<context:annotation-config/>
這條配置會顯示的向spring容器添加上面四個bean。
配置如下:
<context:anotation-config/>
<bean class="cn.wyn.XxxConfig"/>
但是蒂窒,還能更簡潔躁倒!
使用<component:scan>
可以直接省去在xml中聲明java config 的Bean。
<componet:scan basePackage="cn.wyn"/>
一條配置解決刘绣,還能定義掃描的包樱溉。