1.构造器注入
之前写过了~~~~
2.Set方式注入【重点】
3.拓展方式注入
2.Set方式注入【重点】
【环境搭建】
1.复杂类型
2.真实测试对象
四个文件
Student实体类的创建:
主要是依据官方文档来建立。那个Address也是为了测试不同的类型,而创建的引入类。主体是这个Student!!!
package com.Li.pojo;
import java.util.*;
//搭建多种注入方式,所以需要多种参数类型
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;
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> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
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) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", info=" + info +
", wife='" + wife + '\'' +
'}';
}
}
Address实体类的创建:
package com.Li.pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
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.Li.pojo.Student">
<property name="name" value="李"/>
</bean>
</beans>
MyTest
import com.Li.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.Li.pojo.Address"/>
<!--最基础的创建对象,设置name的值为李-->
<bean id="student" class="com.Li.pojo.Student">
<!--第一种,普通注入,value-->
<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="0000000"/>
<entry key="银行卡" value="1111111"/>
</map>
</property>
<!--第六种,Set注入-->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<!--第七种,null注入-->
<property name="wife">
<null></null>
</property>
<!--第八种,Properties注入(特殊)-->
<property name="info">
<props>
<prop key="driver">20220000</prop>
<prop key="url">xxx</prop>
<prop key="password">1111</prop>
<prop key="username">root</prop>
</props>
</property>
</bean>
</beans>
测试一下。
发现address有问题,所以给Address类里面增加一个toString方法
测试修改之后:
成功!(因为再bean里面没有设置adress的值,所以为null)
拓展方式注入(了解即可)
User实体类:
package com.Li.pojo;
public class User {
public String name;
public int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"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;
}
}
userBeans.xml:
导入xml约束:
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
测试p与c (p对应无参构造,c对应有参构造)
测试:
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
User user = context.getBean("user",User.class);//和强转本质一样,只不过换了一种形式
System.out.println(user);
User user2 = context.getBean("user2",User.class);
System.out.println(user2);
}
Bean的作用域
六种作用域:
默认单例:singleton(每一次创建的都是一个相同的bean对象)
prototype:(原型模式,每一次都是不同的bean对象)
测试之后不相等了。