注入方式
1.属性setter注入(需要无参构造方法):
对依赖对象写入setter方法,通过setter的对象注入容器中
<!-- 属性注入(setter注入)-->
<!--将容器中的messageService,注入给当前Controller的messageService属性(通过该属性的setter)-->
<property name = "messageService" ref="messageServiceBean"/>
public MessageController(){
}
public void setMessageService(IMessageService messageService){
this.messageService = messageService;
}
2.构造方法注入(有参构造方法):
注入对象构造方法中的参数,使容器识别依赖对象
<bean id="msgControllerBean" class="com.apescource.web.MessageController">
<!-- 构造注入(构造方法注入)-->
<constructor-arg name="messageService" ref="messageServiceBean"/>
<constructor-arg name="defaultMsg" value="默认消息模板"/>
</bean>
public MessageController(String defaultMsg,IMessageService messageService){
System.out.println(defaultMsg+":");
this.messageService = messageService;
}
3.接口注入
接口注入模式因为具备侵入性,它要求组件必须与特定的接口相关联,因此并不被看好,实际使用有限。
注入类型
1.注入字符串,单个数值类型
<bean id = "example" class = com.example>
<property name = " " value = " "></property>
</bean>
2.注入bean,引用类型
<bean id = "outerBean" class="com.outer">
<property name = "target">
<bean class = "com.Person">
<property name = "name" value = "Amy">
<property name = "age" value = "18">
</bean>
</property>
</bean>
3.注入集合,List、set、Map、Properties等
<bean id = "listBean" class = "com.rep.example01">
<property name = "list">
<list>
<value>一</value>
<value>二</value>
<value>三</value>
<ref bean = "dataSource"/>
</list>
</property>
</bean>
<bean id = "setBean" class = "com.rep.example02">
<property name = "set">
<set>
<value>张</value>
<value>王</value>
<value>李</value>
<ref bean = "dataSource"/>
</set>
</property>
</bean>
<bean id = "mapBean" class = "com.rep.example03">
<property name = "map">
<map>
<entry key = "an entry" value = "just some string"/>
<entry key = "a ref" value = "dataSource"/>
</map>
</property>
</bean>
<bean id = "propsBean" class = "com.rep.example04">
<property name = "props">
<props>
<prop key = "一">one</prop>
<prop key = "二">two</prop>
<prop key = "三">three</prop>
<props>
</property>
</bean>
Spring常用注解
1.@Component:类级别注解,标注一个普通的spring Bean类。表明该类会作为组件类,并告知Spring要为这个类创建Bean。
@Componet
public class DataService{
private IRepostitory repostitory;
public boolean saveData(){...};
}
2.@ComponentScan:类级别注解,使用在配置类。启用组件扫描,默认当前配置类所在包为基础包。
// 基础包
@ComponentScan(basePackages="com.codeup")
public class SystemSpringConfig{
}
//指定类所在包为基础包
@ComponentScan(basePackageClasses = ICodeupMarker.class)
public class SystemSpringConfig{
}
3.@Autowired:方法级别注解,自动注入一个符合类型要求的Bean。(参数:required:是否为必须注入项)
@Autowired
public DataService(Irepostitory repostitory){
this.repostitory = repostitory;
}
4.@Primary:类级别的注解,一般与@Component配合使用,在自动装备时候,设置某个bean为首选。
@Component
@Primary
public class FileRepostitoryImpl implements Irepostitory{
public boolean writeData(List<String> dataList){
}
}
5.@Qualifier:方法级别注解(一般使用于:构造器、Setter方法、普通方法),指定所注入的bean的ID。(参数:value:所注入的bean的ID)
@Autowired
@Qualifier("fileReopstitoryImpl")
public void setRepostitory(IRepostitory repostitory){
this.repostitory = repostitory;
}
6.@Scope:类级别声明注解,定义Bean的作用域。(参数:value:作用域参数值、singleton、prototype等)
public class DataService{
}