Bean的属性注入
1、构造方法注入
通过构造方法注入Bean的属性值或宜兰的对象,它保证了Bean实例在实例化后就可以使用。
构造器注入在元素里声明的属性
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
配置文件
<!--bean属性注入构造方法注入-->
<bean id="person" class="com.ioc.demo4.Person">
<constructor-arg name="name" value="张三"/>
<constructor-arg name="age" value="23"/>
</bean>
2、set方法的属性注入
spring配置文件中通过设置注入的属性
对象类型值用ref
<!--bean属性注入set注入-->
<bean id="person" class="com.ioc.demo4.Person">
<property name="name" value="李四"/>
<property name="age" value="25"/>
<property name="cat" ref="cat"/>
</bean>
<bean id="cat" class="com.ioc.demo4.Cat" >
<property name="name" value="大黄"
</bean>
3、p名称空间属性注入
需要在配置加上
xmlns:p="http://www.springframework.org/schema/p"
<!--p:名称空间注入-->
<bean id="person" class="com.ioc.demo4.Person" p:age="25" p:name="李四" p:cat-ref="cat"/>
<bean id="cat" class="com.ioc.demo4.Cat" p:name="大黄"/>
4、SpEl注入
spring expression language,spring表达式语言,对依赖注入进行简化
语法:#{表达式}
<bean id="person" class="com.ioc.demo4.Person">
<property name="name" value="#{'李四'}"/>
<property name="age" value="#{25}"/>
<property name="cat" value="#{cat}"/>
</bean>
<bean id="cat" class="com.ioc.demo4.Cat" >
<property name="name" value="#{'大黄'}"/>
</bean>
5、复杂类型注入
新建一个类叫CollectionBean,这里省去了get和set,以及toString方法。记得加上。
public class CollectionBean {
private String[] arrs;//数组类型
private List<String> list;//List集合类型
private Set<String> set;//Set集合类型
private Map<String,Integer> map;//Map集合类型
private Properties properties;
}
配置文件
<!--集合(复杂)类型的属性注入-->
<bean id="collectionBean" class="com.ioc.demo5.CollectionBean">
<!--数组类型-->
<property name="arrs">
<list>
<value>111</value>
<value>222</value>
<value>333</value>
</list>
</property>
<!--list集合的属性注入,基本类型用value,对象类型用ref-->
<property name="list">
<list>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</list>
</property>
<!--Set集合的属性注入-->
<property name="set">
<set>
<value>ddd</value>
<value>eee</value>
<value>fff</value>
</set>
</property>
<!--map集合的属性注入-->
<property name="map">
<map>
<entry key="aa" value="11"></entry>
<entry key="bb" value="22"></entry>
<entry key="bb" value="33"></entry>
</map>
</property>
<!--Property类型的属性注入-->
<property name="properties">
<props>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
6、使用注解属性注入
使用注解管理Bean
@Component(“beanId”)
可细分为以下三类,更清晰
@Repository用于对DAO实现类进行标注
@Service用于对Service实现类进行标注
@Controller用于对Controller实现类进行标注
Spring属性注入的注解
简单类型用value
对象类型
@Autowired自动匹配相同类名
强制要求按照注解id需要使用Qualifier(“beanId”)
@Resource(name=“beanId”)
相当于
@Autowired
@Qualifier(“beanId”)
Spring的其他注解
@Scope注解用于指定Bean的作用范围,默认单例singleton
@PostConstruct相当于配置文件中init-method="init_method_name"当bean被载入到容器的时候调用
@PreDestroy当bean从容器中删除的时候调用(scope=singleton有效)
XML与注解整合
XML方式的优势:结构清晰,易于阅读
注解方式的优势:开发便捷,属性注入方便
XML与注解的整合开发(将Bean管理交给XML,属性注入使用注解开发)
1、引入context命名空间
2、在配置文件中添加context:annotation-config标签