Spring-Day2
1.1、Hello Spring(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="userDaoImpl" class="com.wzw.dao.UserDaoImpl"/>
<bean id="userServiceImpl" class="com.wzw.service.UserServiceImpl">
<property name="userDao" ref="userDaoImpl"/>
</bean>
</beans>
@Test
void testGetUser() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserService) context.getBean("userServiceImpl");
userService.getUser();
}
通过 **ApplicationContext context = new ClassPathXmlApplicationContext(“beans.xml”)**读取配置文件。
1.2、IOC(控制反转)创建对象的方式
- 使用无参构造方法创建对象。如果直接采用采用有参构造方法创建对象则报错!
public User() {
System.out.println("使用无参构造方式创建对象!");
}
<bean id="user" class="com.wzw.pojo.User">
<property name="name" value="王智威"/>
</bean>
@Test
void testGetUser() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) context.getBean("user");
}
-
使用有参构造方法创建对象 public User(String name) {
this.name = name;
}
@Test
void testGetUser() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) context.getBean("user");
user.show();
}
<bean id="user" class="com.wzw.pojo.User">
<constructor-arg index="0" value="王智威"/>
</bean>
基本类型可以直接用,引用类型需要全限定类名 不建议使用(例如多个String) <bean id="user" class="com.wzw.pojo.User">
<constructor-arg type="java.lang.String" value="王智威"/>
</bean>
<bean id="user" class="com.wzw.pojo.User">
<constructor-arg name="name" value="王智威"/>
</bean>
Spring中无论调用不,bean中所有的对象都被创建了,通过getBean("")方法引用时,引用的是同一对象。
总结:在配置文件加载的时候,容器中管理的对象就已经初始化了。
1.3、Spring配置
<alias name="user" alias="user2"/>
<!-- id : bean的唯一标识符,相当于对象名
class : bean对象所对应的全限定名:包名+类型
name : 也可作为别名使用,name可以同时取多个别名
-->
<bean id="user" class="com.wzw.pojo.User" name="user3 user4">
<constructor-arg name="name" value="王智威"/>
</bean>
- import 一般用于团队开发中 利用import将多个配置文件合并到一个,使用的时候采用核心配置文件就好
<import resource="beans.xml"/>
1.4、依赖注入
1.4.1 构造器注入
上面已经说过了
1.4.2 Set方式注入【重点】
- 依赖注入:set注入!
- 依赖:bean对象的创建依赖于容器!
- 注入:bean对象中所有属性,由容器来创建!
【环境搭建】
- Student.java 真实测试对象
package com.wzw.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 String wifer;
private Properties info;
public Student() {
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wifer='" + wifer + '\'' +
", info=" + info +
'}';
}
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 String getWifer() {
return wifer;
}
public void setWifer(String wifer) {
this.wifer = wifer;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
}
- Address.java 复杂测试对象
package com.wzw.pojo;
public class Address {
private String address;
public Address(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + 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="userDaoImpl" class="com.wzw.dao.UserDaoImpl"/>
<bean id="userServiceImpl" class="com.wzw.service.UserServiceImpl">
<property name="userDao" ref="userDaoImpl"/>
</bean>
<bean id="user" class="com.wzw.pojo.User" name="user3 user4">
<constructor-arg name="name" value="王智威"/>
</bean>
<alias name="user" alias="user2"/>
<bean id="student" class="com.wzw.pojo.Student">
<property name="name" value="王智威"/>
</bean>
</beans>
- 测试类Test
@Test
void testStudnt() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
}
- 完善注入信息
<bean id="address" class="com.wzw.pojo.Address">
<property name="address" value="河南工业大学"/>
</bean>
<bean id="student" class="com.wzw.pojo.Student">
<property name="name" value="王智威"/>
<property name="address" ref="address"/>
<property name="books">
<array>
<value>宫楼梦</value>
<value>西游记</value>
<value>三国演义</value>
</array>
</property>
<property name="hobbys">
<list>
<value>听歌</value>
<value>看电影</value>
<value>打游戏</value>
<value>阅读</value>
</list>
</property>
<property name="card">
<map>
<entry key="身份证" value="410426388874362512"/>
<entry key="银行卡" value="4673826388874362512"/>
</map>
</property>
<property name="games">
<set>
<value>LOL</value>
<value>王者荣耀</value>
<value>DNF</value>
</set>
</property>
<property name="wifer">
<null/>
</property>
<property name="info">
<props>
<prop key="学号">201916040419</prop>
<prop key="姓名">王智威</prop>
<prop key="性别">男</prop>
</props>
</property>
</bean>
|