Spring的三种注入方式
- 1.属性注入(setter注入),该类中必须有一个setter方法,且必须有一个无参构造方法。
<bean id="" class="">
<property name="" ref=""/>
<property name="" value=""/>
</bean>
<!-- Inner beans,内部bean的声明方式-->
<bean id="" class="">
<property name="">
<bean class="">
<property name="" value=""/>
<property name="" ref=""/>
</bean>
</property>
</bean>
- 2.构造注入(构造方法注入)
<bean id="" class="">
<constructor-arg name="" value=""/> <!--按照属性名称注入-->
<constructor-arg name="" ref=""/>
<constructor-arg index="0" value=""/> <!--按照下标注入-->
<constructor-arg index="1" ref=""/>
<constructor-arg type="java.lang.String" value=""/> <!--按照类型注入-->
<constructor-arg type="com.my.*" ref=""/>
</bean>
- 3.接口注入
注入list 、set、map、props类型的集合数据
<property name="">
<list>
<value></value>
<ref bean=""></ref>
</list>
</property>
<property name="">
<set>
<value></value>
<ref bean=""></ref>
</set>
</property>
<property name="">
<map>
<entry key="" value=""/>
<entry key="" value-ref=""/>
</map>
</property>
<property name="">
<props>
<prop key=""></prop>
<prop key=""></prop>
</props>
</property>
使用 P 命名空间(p-namespace)
<!--在命名空间中加入-->
xmlns:p="http://www.springframework.org/schema/p"
<bean id="" class="com.apesource.service.DataService"
p:属性名称=""
p:属性名称-ref="">
</bean>
JavaConfig:通过Java代码显示装配Bean
JavaConfig的优势:
- 强大:提供更为丰富的配置功能;
- 类型安全:保证配置时的类型安全;
- 重构友好:更方便的进行重构;
@Configuration:类级别的声明注解;定义Spring配置类。
@Bean:方法级别注解;声明配置该方法所产生的对象为Spring中的bean。
@Import:导入其他配置类;
@ImportResource:导入其他XML配置文件;
@Configuration
public class SystemSpringConfig{
@Bean
public ISaveData excelSaveData(){
return new ExcelSaveDataImpl();
}
@Bean
public ISaveData fileSaveData(){
return new FileSaveData();
}
@Bean
public DataService dataService(){
return new DataService(fileSaveData());
}
@Bean
public DataService dataService(@Qualifier("fileSaveData")ISaveData saveData){
return new DataService(saveData)
}
}
Spring 常用注解
@Component : 类级别注解,用于标注当前类为spring容器中的一个bean(组件)
@Component public class DataService{ }@ComponentScan:类级别注解,使用在配置类中;启用组件扫描,默认当前配置类所在包为基础包。basePackages:基础包,basePackageClasses:指定类所在包为基础包。
@ComponentScan(basePackages = "com.my.*") @ComponentScan(basePackageClasses = 类名.class) public class SystemSpringConfig{ }@Service : 业务层组件
@Service public class Service{ }@Repository :数据访问层组件
@Repository public class Dao{ }@Autowired :方法级别注解(构造器、setter方法、普通方法);自动装配(默认按照类型自动转配),按照当前声明的接口类型,在容器中查找该接口实现类对象bean,进行自动注入(装配)
参数:required 是否为自动注入项@Autowired public DataService(ISaveData saveData){ //构造方法 this.saveData = saveData; } @Autowired(required = true) public void setSaveData(ISaveData saveData){ //setter方法 this.saveData = saveData; } @Autowired(required = false) public void injectionSaveData(ISaveData saveData){ //自定义普通方法 this.saveData = saveData; }@Primary:类级别的注解,一般与@Component配合使用;在自动装配时,设置某个bean为首选
@Component @Primary public class DataService{ }@Qualifier("bean名称") :方法级别注解(构造器、setter方法、普通方法);按照bean名称自动装配,与@Autowired注解配合使用。按照当前指定的bean名称,在容器中查找该名称对应的bean,进行自动注入;
@Autowired @Qualifier("saveDataImpl") public DataService(ISaveData saveData){ //构造方法 this.saveData = saveData; }@Scope:类级别的声明注解;定义bean的作用域,
@Component @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public class DataService{ }< bean> 节点的常用属性
- id: 给被管理的对象起个名字,不能重复
- class: 被管理对象的完整类名
- scope="singleton" 单例模式(默认) scope="prototype"多例模式
- init-method: 为实例设置初始化方法
- destroy-method : 为实例设置销毁方法
Spring容器的两种加载方式
//在classpath路径下加载xml配置文件,完成Spring容器的加载; //或者,采用注解方式时,需要在xml配置文件中使用<context:component-scan base-package="com.apesource"/> //完成类扫描 ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); ApplicationContext context = new FileSystemXmlApplicationContext("c:/springConfig.xml"); //基于注解配置的Spring容器加载方式 ApplicationContext context = new AnnotationConfigApplicationContext("com.apesource");