1、依赖注入
1.1、构造器注入
默认无参构造,有参构造采用3种方式:下标index赋值、类型type赋值、构造函数参数名称name
1.2、set方式注入「重点」
- 依赖注入:Set注入
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,有容器来注入
「环境创建」
1.复杂类型
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> hobbies;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
//省略get/set、toString方法
...
}
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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.jackey.pojo.Student">
<!--第一种,普通值注入,value-->
<property name="name" value="jackey"/>
</bean>
</beans>
4.测试类
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
System.out.println(student.toString());
}
}
完善注入信息
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.jackey.pojo.Address">
<property name="address" value="成都"/>
</bean>
<bean id="student" class="com.jackey.pojo.Student">
<!--1、普通值注入,value-->
<property name="name" value="jackey"/>
<!--2、引用类型注入,ref-->
<property name="address" ref="address"/>
<!--3、数组类型注入-->
<property name="books">
<array>
<value>《java编程思想》</value>
<value>《算法》</value>
<value>《Spring技术内幕》</value>
</array>
</property>
<!--4、list注入-->
<property name="hobbies">
<list>
<value>programming</value>
<value>music</value>
<value>basketball</value>
</list>
</property>
<!--5、Map注入-->
<property name="card">
<map>
<entry key="No1" value="202031"/>
<entry key="No2" value="202032"/>
<entry key="No3" value="202033"/>
</map>
</property>
<!--6、Set注入-->
<property name="games">
<set>
<value>CS</value>
</set>
</property>
<!--7、null注入-->
<property name="wife">
<null/>
</property>
<!--8、Properties注入-->
<property name="info">
<props>
<prop key="driver">com.mysql.jdbc.Driver</prop>
<prop key="url">jdbc:mysql://localhost:3306/test_dev</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
1.3、拓展方式注入
可以使用p命名空间和c命名空间进行注入
官方解释:
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名空间注入,可以直接注入属性的值-->
<bean id="user" class="com.jackey.pojo.User" p:name="jackey" p:age="18"/>
<!--c命名空间注入,通过构造器注入,construct-args 类里面必须要有有参构造器-->
<bean id="user2" class="com.jackey.pojo.User" c:name="jack" c:age="18"/>
</beans>
测试:
@Test
public void test2() {
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
//User user = context.getBean("user",User.class);
User user = context.getBean("user2",User.class);
System.out.println(user.toString());
}
注意:p命名空间和c命名空间不能直接使用,需要导入xml约束
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
1.4、bean的作用域
查看Spring官方文档:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-setter-injection
1.单例模式(Spring默认机制)
<bean id="user2" class="com.jackey.pojo.User" c:name="jack" c:age="18" scope="singleton"/>
2.原型模式:每次从容器中get的时候都会产生一个新对象!
<bean id="user2" class="com.jackey.pojo.User" c:name="jack" c:age="18" scope="prototype"/>
3.其余的request、session、application、这些只能在web开发中使用到