1 set方式注入
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,由容器来注入
创建实体类【使用lombok jar包】
Student实体类
import java.util.*;
@Data
@AllArgsConstructor //全参构造
@NoArgsConstructor // 无参构造
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 String wife;
private Properties info;
}
Address实体类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Address {
private String address;
}
- 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="address" class="com.xgh.pojo.Address">
<property name="address" value="上海"/>
</bean>
<bean id="student" class="com.xgh.pojo.Student">
<property name="name" value="xgh"/>
<!--注入类-->
<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>game</value>
<value>code</value>
<value>lianai</value>
<value>wan</value>
</list>
</property>
<!--注入Map集合-->
<property name="card">
<map>
<entry key="张三" value="123"/>
<entry key="李四" value="456"/>
<entry key="王二" value="789"/>
<entry key="赵六" value="135"/>
</map>
</property>
<!--注入Set集合-->
<property name="games">
<set>
<value>吃鸡</value>
<value>王者</value>
<value>lol</value>
<value>穿越</value>
</set>
</property>
<!--注入wife指针-->
<!--方式一-->
<!-- <property name="wife">-->
<!-- <null/>-->
<!-- </property>-->
<!--方式二-->
<property name="wife" value=""/>
<!--注入Properties文件-->
<property name="info">
<props>
<prop key="学号">1234567</prop>
<prop key="姓名">法外狂徒张三</prop>
<prop key="性别">男</prop>
</props>
</property>
</bean>
</beans>
- 测试类
public class SpringTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
String s = student.toString();
System.out.println(s);
}
}
结果展示
拓展方式注入【p c】
创建实体类:
user实体类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor //全参构造
@NoArgsConstructor // 无参构造
public class User {
private String name;
private int age;
}
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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--方式一 通过p命名空间注入,可以直接注入属性的值,property 通过无参构造的方式-->
<bean id="user1" class="com.xgh.pojo.User" p:age="12" p:name="xgh"/>
<!--方式二 通过c命名空间注入,通过构造器注入:constructor-args-->
<bean id="user2" class="com.xgh.pojo.User" c:name="法外狂徒张三" c:age="12"/>
</beans>
- 注意
需要添加两个外部引用
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
测试
public class Spring_2 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
User users = context.getBean("user2", User.class);
System.out.println(users);
}
}
结果展示
3 bean的作用域
- 1 单例模式(Spring默认机制)
关键条件 scope="singleton"
<bean id="user1" class="com.xgh.pojo.User" p:age="12" p:name="xgh" scope="singleton"/>
- 2 原型模式:每一次从容器中get的时候,都会产生一个新的对象
关键条件 scope="prototype"
<bean id="user1" class="com.xgh.pojo.User" p:age="12" p:name="xgh" scope="prototype"/>