Spring Bean的装配分为3中:
1、隐式发现和自动装配
@Component:将一个Java类声明为Bean(组件类),等待Spring扫描发现。
@ComponentScan:启用组件扫描将带有@Component的类实例化为Bean,一般用在配置类上,可指定扫描基础包,默认与配置类相同的包。
@Configuration:声明一个Java类是配置类,它与@Component作用相当,只是更为直观,一般@Bean与其连用。
<context:component-scan base-package="...">:通过XML方式启用组件扫描。
@Autowired:为bean中的属性自动注入,前提是该类必须声明为一个bean,如添加@Component
@Bean:将方法的返回对象作为bean,前提是该类必须是一个bean,如添加@Configuration
2、通过Java代码装配
该部分就是@Configuration和@Bean的运用,通过手动创建对象,然后在配置成Bean。
3、通过XML配置
xml配置规范:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p" 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"> </beans>
在<beans></beans>中声明需要的bean,<bean id="xxx" class="全类名" &others.../>,或<bean ...>some configuration</bean>的形式。
借助构造器注入初始化bean:使用<constructor-arg> or c命名空间(需要引入c命名空间,如上述xml头部的xmlns:c=...)。
使用<constructor-arg>:
<bean id="xxx" class="com.example.xxx"> <constructor-arg ref="bean-id"/> <constructor-arg value="value"/> <constructor-arg><null/></constructor-arg> </bean>
使用c命名空间:
<bean id="xxx" class="com.example.xxx" c:xxx-ref = "bean-id" c:_xxx = "constant value" /> <!-- 还可以根据参数顺序命名--> <bean id="xxx" class="com.example.xxx" c:_0-ref = "bean-id" c:_1 = "constant value" /> <!-- 当只有一个参数时,直接用c:_-ref或c:_=即可-->
但c命名空间有限制,当构造参数传入一个集合时,只能使用<constructor-arg>:
<bean id="xxx" class="com.example.xxx"> <constructor-arg ref="bean-id"/> <constructor-arg value="value"/> <constructor-arg><null/></constructor-arg> <constructor-arg> <list> <ref bean="b1"/> <ref bean="b2"/> <ref bean="b3"/> </list> </constructor-arg> <constructor-arg><!--set and constant--> <set> <value>one</value> <value>two</value> <value>three</value> </set> </constructor-arg> </bean>
设置属性
<bean id="xxx" class="com.example.xxx"> <property name="property-name" ref="bean-id"/> <property name="property-name" value="constant-value"/> <property name="property-name"> <list> ... </list> </property> </bean>
或者用p命名空间(在xml头部用xmlns:p引入)
<bean id="xxx" class="com.example.xxx" p:p1-ref = "bean-id" p:p2 = "constant-value" /> <!--p命名空间不可用顺序来确定属性,同样无法处理集合类-->
Spring util命名空间简化集合类:将集合变成一个bean
<!--使用util:list创建一个list的bean--> <util:list id = "bean-id"> <ref bean = "bean-id"/> <!-- or <value> tag --> </util:list>
4、导入和混合配置
JavaConfig引用xml配置:在配置类上使用@ImportResource注解导入xml,如@ImportResource("classpath:beans.xml")
JavaConfig互相引用:在配置类上使用@Import注解,如@Import(class-name.class)
XML引用XML:<import resource="beans.xml"/>
5、JUnit单元测试
maven引入JUnit和spring-test:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.4.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency>
编写测试类:
import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import soundsystem.CDPlayerConfig; import soundsystem.CompactDisc; import soundsystem.MediaPlayer; /** * Author: * Date:2018/3/14 15:21 * Email: * Comment: */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = CDPlayerConfig.class) public class CDPlayerTest { @Autowired private MediaPlayer player; @Autowired private CompactDisc cd; @Test public void cdShouldNotBeNull() { Assert.assertNotNull(cd); } @Test public void playTest() { player.play(); } }
持续更新...