一、构造器注入
(前面已详述,不再赘述)
二、Set方式注入【重点】
1、依赖
bean对象的创建依赖于容器;
2、注入
bean对象所有的属性,由容器注入;
3、环境搭建
第一步:创建实体类Address
package com.zibo.pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
第二步:创建实体类Student
package com.zibo.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> habits;
private Map<String,String> card;
private Set<String> games;
private Properties info;
private String wife;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHabits() {
return habits;
}
public void setHabits(List<String> habits) {
this.habits = habits;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", habits=" + habits +
", card=" + card +
", games=" + games +
", info=" + info +
", wife='" + wife + '\'' +
'}';
}
}
第三步:编写配置文件applicationContext.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.zibo.pojo.Student">
<property name="name" value="訾博"/>
</bean>
</beans>
第四步:编写测试类
package com.zibo;
import com.zibo.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("applicationContext.xml");
Student student = context.getBean("student", Student.class);
System.out.println(student.getName());
}
}
测试结果:
訾博
4、注入其他类型的数据
第一步:修改配置文件applicationContext.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.zibo.pojo.Student">
<!--1、普通值注入-->
<property name="name" value="訾博"/>
<!-- 2、bean注入-->
<property name="address" ref="address"/>
<!--3、数组注入-->
<property name="books">
<array>
<value>Java核心技术</value>
<value>增长黑客</value>
<value>Spring MVC学习指南</value>
</array>
</property>
<!--4、list集合注入-->
<property name="habits">
<list>
<value>写代码</value>
<value>看电影</value>
<value>读书</value>
</list>
</property>
<!--5、map集合注入-->
<property name="card">
<map>
<entry key="身份认证号" value="123456"/>
<entry key="银行卡号" value="1357"/>
<entry key="学号" value="2468"/>
</map>
</property>
<!--6、set集合注入-->
<property name="games">
<set>
<value>CF</value>
<value>DNF</value>
<value>LOL</value>
</set>
</property>
<!--7、Properties注入-->
<property name="info">
<props>
<!--也是key与value,只是要注意书写的位置-->
<prop key="stuNum">123456</prop>
<prop key="age">22</prop>
</props>
</property>
<!--8、null注入-->
<property name="wife">
<null/>
</property>
</bean>
<bean id="address" class="com.zibo.pojo.Address">
<property name="address" value="河南省郑州市"/>
</bean>
</beans>
第二步:修改测试类
package com.zibo;
import com.zibo.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("applicationContext.xml");
Student student = context.getBean("student", Student.class);
System.out.println(student.toString());
}
}
测试结果(为便于查看,已手动换行):
Student{name='訾博', address=Address{address='河南省郑州市'}, books=[Java核心技术, 增长黑客,
Spring MVC学习指南], habits=[写代码, 看电影, 读书], card={身份认证号=123456, 银行卡号=1357,
学号=2468}, games=[CF, DNF, LOL], info={stuNum=123456, age=22}, wife='null'}
三、拓展方式
1、p命名空间注入
第一步:创建实体类User
package com.zibo.pojo;
public class User {
private String name;
private int 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 +
'}';
}
}
第二步:创建新的配置文件newBeans.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名空间注入,可以直接注入属性的值:property-->
<!--p命名空间注入:用于无参构造注入-->
<bean id="user" class="com.zibo.pojo.User" p:name="訾博" p:age="24"/>
</beans>
第三步:修改测试类
package com.zibo;
import com.zibo.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("newBeans.xml");
User user = context.getBean("user", User.class);
System.out.println(user.toString());
}
}
测试结果:
User{name='訾博', age=24}
2、c命名空间注入
第一步:修改实体类User
package com.zibo.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 +
'}';
}
}
第二步:修改配置文件newBeans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--c命名空间注入:用于带参构造注入-->
<!--c命名空间注入,可以直接注入属性的值:下标、属性都行-->
<bean id="user" class="com.zibo.pojo.User" c:_0="訾博" c:_1="24"/>
</beans>
测试结果:
User{name='訾博', age=24}
注意:
导入命名空间的相关约束;
四、Bean的作用域
1、官方文档截图
2、singleton单例模式(默认)
当配置文件被加载时创建所有bean对象,仅创建一次,以后无论调用多少次,所调用的都是同一个对象;
不再代码演示,可参考前面笔记:【Spring】002-探索Spring IOC创建对象的方式
3、prototype多例模式
第一步:创建一个实体类People
package com.zibo.pojo;
public class People {
private String name;
private int age;
public People() {
System.out.println("无参执行了!");
}
public People(String name, int age) {
this.name = name;
this.age = age;
System.out.println("带参执行了!");
}
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 "People{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
第二步:新建配置文件peopleBean.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="people" class="com.zibo.pojo.People" scope="prototype">
<property name="name" value="訾博"/>
<property name="age" value="24"/>
</bean>
</beans>
第三步:修改测试类
package com.zibo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("peopleBean.xml");
}
}
测试结果:
第四步:修改测试类,获取对象并使用
package com.zibo;
import com.zibo.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("peopleBean.xml");
People people = context.getBean("people", People.class);
System.out.println(people.toString());
}
}
测试结果:
无参执行了!
People{name='訾博', age=24}
说明了什么:
说明在被使用时创建对象;
第五步:修改测试类,再获取一个对象,看是否是同一个对象
package com.zibo;
import com.zibo.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("peopleBean.xml");
People people = context.getBean("people", People.class);
People people1 = context.getBean("people", People.class);
System.out.println(people == people1);
}
}
测试结果:
无参执行了!
无参执行了!
false
结论:
多例模式,配置文件被加载的时候并不立即创建对象,而是等到对象被获取的时候进行创建,且每次获取都会进行实例化,所获取到的不是同一个对象;
4、其他
正在web中使用到,暂不做探究;