依赖注入:
- 依赖:bean对象的创建依赖于容器。
- 注入:bena对象中的所有属性,由容器来注入。
1、构造器注入
即(三)中提及的三种方式。
2、set方式注入
【环境搭建】
1、复杂类型
package com.xiao.pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2、真实对象
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private Properties info;
//getter、setter
3、beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--普通值类型注入 -->
<bean id="student" class="com.xiao.pojo.Student">
<property name="name" value="松果"/>
</bean>
</beans>
4、测试类
import com.xiao.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext Context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) Context.getBean("student");
System.out.println(student.getName());
}
}
完善注入信息:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.xiao.pojo.Address">
<property name="address" value="北京"/>
</bean>
<bean id="student" class="com.xiao.pojo.Student">
<!-- 普通值注入 -->
<property name="name" value="松果"/>
<!-- bean注入 ,ref-->
<property name="address" ref="address"/>
<!-- 数组 -->
<property name="books">
<array>
<value>假如给我三天光明</value>
<value>雷雨</value>
<value>围城</value>
<value>飞鸟集</value>
</array>
</property>
<!-- list -->
<property name="hobbys">
<list>
<value>打球</value>
<value>跑步</value>
<value>听音乐</value>
</list>
</property>
<!-- Map -->
<property name="card">
<map>
<entry key="学生卡" value="154616512"/>
<entry key="身份证" value="486464164563"/>
</map>
</property>
<!-- Set-->
<property name="games">
<set>
<value>LOL</value>
<value>DNF</value>
<value>CF</value>
</set>
</property>
<!-- null -->
<property name="wife">
<null/>
</property>
<!-- Properties -->
<property name="info">
<props>
<prop key="学号">215648652231</prop>
<prop key="性别">男</prop>
</props>
</property>
</bean>
</beans>
3、拓展方式
- P命名空间
POJO
package com.xiao.pojo;
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.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;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
新建一个XML配置文件,添加第三方约束xmlns:p="http://www.springframework.org/schema/p"
<!-- p命名空间注入,可以直接注入属性的值 -->
<bean id="user" class="com.xiao.pojo.User" p:name="索隆" p:age="20"/>
- C命名空间
同理,在使用前导入第三方约束xmlns:c="http://www.springframework.org/schema/c"
<!-- C命名空间注入,通过构造器注入,即在实体类中需有有参构造和无参构造-->
<bean id="user2" class="com.xiao.pojo.User" c:age="18" c:name="布鲁克"/>
测试类
@Test
public void Test2(){
ApplicationContext Context = new ClassPathXmlApplicationContext("beans2.xml");
User user = (User)Context.getBean("user2");
System.out.println(user);
4、bean的作用域
- 单例模式(Spring模式机制)
<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>
- 原型模式:每次从容器中get的时候,都会产生一个新的对象(建议多线程下使用)
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
其余的request、session、application,这些只能在web开发中使用,具体例子可在官网中查看。点我点我