Spring的配置和依赖注入(DI)
1. Spring的配置:Bean的配置其实就是对某个实体类的配置,由spring接管和控制。在spring中配置类的属性以及引用。
public class User {private String name;public String getName() {return name; //name 要与properties中的name="name"相同;}public void setName(String name) {this.name = name;}public void show(){System.out.println("name:"+name);}
}
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 注册User类 将其变为一个bean对象id是给bean起的别名, class :全类名,就是类所在的地址scope: 表示该bean是单例还多例的(singleton:表示创建对象只会创建一个 prototype:创建多个)property :是对应类中的属性。 name的值要与User类中的属性名相同; value:是常量值;--><bean id="User" class="com.spring.pojo.User" scope="">
<!-- name是User中的属性 value是常量 ref:是对于其他bean的引用;
--><property name="name" value="jack"/> </bean><!-- 注册的其他的bean; --><bean id="userDaoMysql=" class="com.spring.dao.UserDaoMysqlImpl"/><bean id="userDaoSqlServer" class="com.spring.dao.UserDaoSqlServerImpl"/><!-- id是别名,用来进行context.getBean() 进行调用--><bean id="userService" class="com.spring.service.UserServiceImpl">
<!-- name="xxx":是userservice中的属性,而且名字要一样才行;ref是引用其他的bean --><property name="userDao" ref="userDaoSqlServer"/></bean></beans>
团队的合作通过import来实现 .<import resource="{path}/beans.xml"/>
细节:
实体类User中的name属性,在配置Bean的properties属性时,对应的name的值要与Setxxx()方法后面的小写名字相同如setName ===>name;
同样的,在配置UserServiceImpl时也需要相同。
bean的作用域:scope="xxx" ;最长用的是前两种,
singleton:单例: 只能实例化一次,创建一个对象.
prototype:多例,可以多次实例化同一个对象bean ,即可以new多个新得对象.
2. 使用IOC创建对象的两种方法:
1.使用无参构造:
Bean的配置:
测试:
总结:先使用无参构造器进行实例化类,然后再调用方法。
2.使用有参构造:
配置Bean:
测试:
3. 依赖注入(DI)
-
依赖注入(Dependency Injection,DI)。
-
依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
-
注入 : 指Bean对象所依赖的资源 , 由spring容器来设置和装配
1. 构造器注入(上面的例子)
2. set注入 (要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is .)
实体类:
public class Address {private String address;public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}}
package com.spring.pojo;import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;/* @author Lenovo* @date 2023/4/8* @time 13:49* @project SpringDemo01/
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;public void setName(String name) {this.name = name;}public void setAddress(Address address) {this.address = address;}public void setBooks(String[] books) {this.books = books;}public void setHobbys(List<String> hobbys) {this.hobbys = hobbys;}public void setCard(Map<String, String> card) {this.card = card;}public void setGames(Set<String> games) {this.games = games;}public void setWife(String wife) {this.wife = wife;}public void setInfo(Properties info) {this.info = info;}public void show(){System.out.println("name="+ name+ ",address="+ address.getAddress()+ ",books=");for (String book:books){System.out.print("<<"+book+">>\\t");}System.out.println("\\n爱好:"+hobbys);System.out.println("card:"+card);System.out.println("games:"+games);System.out.println("wife:"+wife);System.out.println("info:"+info);}
}
Bean的配置:applicationcontext.xml
使用各种注入方式:常量 map list set properties 数组等注入属性的方式
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 注册Address--><bean id="addr" class="com.spring.pojo.Address"><property name="address" value="太原"/></bean><!-- 注册Student--><bean id="student" class="com.spring.pojo.Student">
<!-- 引用addr--><property name="address" ref="addr"/>
<!-- 常量属性--><property name="name" value="小航"/><property name="books">
<!-- 数组注入--><array><value>西游记</value><value>红楼梦</value><value>水浒传</value></array></property><property name="hobbys">
<!-- list集合--><list><value>听歌</value><value>看电影</value><value>爬山</value></list></property><property name="card">
<!-- map注入的方式--><map><entry key="中国邮政" value="123"/><entry key="中国电信" value="222"/></map></property><property name="games" ><!--set注入--><set><value>LOL</value><value>王者荣耀</value><value>吃鸡</value></set></property><property name="wife">
<!-- 空值注入--><null/></property><property name="info">
<!-- 使用properties注入--><props><prop key="学号">20190604</prop><prop key="性别">女</prop><prop key="姓名">小航</prop></props></property></bean></beans>
测试: