导入包
gradle导入方式
compile group: 'org.springframework', name: 'spring-webmvc', version: '5.2.0.RELEASE'
spring ioc主要有两种方式注入bean,首先是xml方式
配置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"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
</beans>
在xml文件中注入bean,bean标签主要有两种常用属性:id(为bean指定id值) 和 class(要生成bean的类的地址) ,另外还有一种不常用属性值name:为此bean取别名 {name1,name2,…},其次是bean中值的指定一般有两种value-表示基本数据类型以及string类型的值,ref-其他引用类型(相当于地址中为地址的为ref,常量为value)
bean可以有以下几种常用标签
- constructor-arg 标签 (构造方法注入):name属性-参数名称 ref/value-要注入的值
- property 标签(set方法注入):name属性-成员变量名称 ref/value-要注入的值(其中注入的值也可以通过标签注入list、map、set、props、null标签,其中的子标签为ref、value、prop、entry)
简便写法也可以引入属性标签
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
然后属性中用c: 或者p:表示,属性名称可以为注入的参数名称或者从零开始的序号值
示例代码:引入的bean**<bean id="anotherBean" class="com.power.jiang.AnotherBean" />
**
private AnotherBean anotherBean;
private AnotherBean anotherBean1;
private String string;
private String string1;
public Bean(AnotherBean anotherBean, String string) {
this.anotherBean = anotherBean;
this.string = string;
}
public void setString(String string) {
this.string = string
}
public void setAnotherBean1(AnotherBean anotherBean1) {
this.anotherBean1 = anotherBean1;
}
属性方式=====>
<bean id="bean" class="com.power.jiang.Bean" c:anotherBean-ref="anotherBean" c:_1="sssss" p:anotherBean1-ref="anotherBean" p:string1="ooooo">
标签方式=====>
<constructor-arg name="anotherBean" ref="anotherBean"/>
<constructor-arg name="string" value="aaa"/>
<property name="anotherBean1" ref="anotherBean"/>
<property name="string" value="0000"/>
<property name="anotherBean2">
一下省略set方法,但是代码中是必须有的
List类型
private List<String> stringList;
private List<AnotherBean> anotherBeanList;
=====>
<property name="stringList" >
<list>
<value>aaaa</value>
<value>ccccc</value>
</list>
</property>
<property name="anotherBeanList">
<list>
<ref bean="anotherBean"/>
<ref bean="anotherBean"/>
</list>
</property>
set类型
private Set<String> stringSet;
private Set<AnotherBean> anotherBeanSet;
=====>
<property name="stringSet">
<set>
<value>cccc</value>
<value>ccccdddd</value>
</set>
</property>
<property name="anotherBeanSet">
<set>
<ref bean="anotherBean"/>
<ref bean="anotherBean"/>
</set>
</property>
map类型
private Map<String,String> stringMap;
private Map<AnotherBean,AnotherBean> anotherBeanMap;
=====>
<property name="stringMap">
<map>
<entry key="kkkk" value="yyyyy"/>
<entry key="kook" value="yyyxxxyy"/>
</map>
</property>
<property name="anotherBeanMap">
<map>
<entry key-ref="anotherBean" value-ref="anotherBean"/>
</map>
</property>
properties类型
private Properties properties;
=====>
<property name="properties">
<props>
<prop key="aaxx">kkoo</prop>
<prop key="ccxx">kkkkll</prop>
</props>
</property>
注解的方式
- 创建一个Configuration类并添加注解@Configuration,
- 添加包扫描,自动检测包中类的注解情况@ComponentScan(“com.power.jiang.annotation”)
- 在需要自动生成的类上标注@Component(“otherBean”),值可以省略,有值代表别名。@Scope(“prototype”)表示生命周期,可以不写默认为单例
- 在需要自动注入的变量上或者set方法上添加注解@Autowired既可自动注入
- 常量值的注入注解使用注解 @Value(“kkksss”) 值为想注入的值
- 非常量的bean需要在Configuration中创建一个方法添加上@bean,里面可以添加别名值,在bean中需要添加对应的@Qualifier
Configuration中:
@Bean("stringList")
public List<String> stringList(){
List<String> list = new ArrayList<>();
list.add("111");
list.add("222");
return list;
}
private List<String> stringList;
@Autowired
@Qualifier("stringList")
public void setStringList(List<String> stringList) {
this.stringList = stringList;
}