基于xml 方式(不推荐使用)
1、基于 xml 方式创建对象
(1)在 spring 配置文件中,使用 bean 标签,标签里面添加对应属性,就可以实现对象创建
(2)在 bean 标签有很多属性,介绍常用的属性
id 属性:唯一标识(当调用id属性时,实际调用class属性所在的类)
class 属性:类全路径(包类路径)
(3)创建对象时候,默认也是执行无参构造方法完成对象创建
<?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">
<!--配置 User 对象创建-->
<bean id="user" class="com.xiaoqiu.spring.User"></bean>
</beans>
2、基于 xml 方式注入属性
DI:依赖注入,就是注入属性
注入属性就相当于给创建的对象属性赋值的过程
1)使用 set 方法进行注入
(1)创建类,定义属性和对应的 set 方法同时需要保证无参构造方法有效
public class Book {
private String bname;
private String bauthor;
public void setBname(String bname) {
this.bname = bname;
}
public void setBauthor(String bauthor) {
this.bauthor = bauthor;
}
}
(2)在 spring 配置文件配置对象创建,配置属性注入
<?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">
<!--set 方法注入属性-->
<bean id="book" class="com.xiaoqiu.DI.Book">
<!--使用 property 完成属性注入 name:类里面属性名称 value:向属性注入的值-->
<property name="bname" value="西游记"></property>
<property name="bauthor" value="吴承恩"></property>
</bean>
</beans>
(3)在类中增加toString()方法做测试(后面内容同理,将不再做测试)
public class Test {
public static void main(String[] args) {
//1、加载 spring 配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//2 获取配置创建的对象
Book book = context.getBean("book", Book.class);
System.out.println(book);//Book{bname='西游记', bauthor='吴承恩'}
}
}
2)使用有参数构造进行注入
(1)创建类,定义属性,创建属性对应有参数构造方法
public class Book {
private String bname;
private String bauthor;
public Book(String bname, String bauthor) {
this.bname = bname;
this.bauthor = bauthor;
}
}
(2)在 spring 配置文件中进行配置
<?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">
<!--有参数构造注入属性方式1:name(推荐)-->
<bean id="book" class="com.xiaoqiu.DI.Book">
<!--使用constructor-arg完成属性注入 name:类里面属性名称 value:向属性注入的值-->
<constructor-arg name="bname" value="西游记"></constructor-arg>
<constructor-arg name="bauthor" value="吴承恩"></constructor-arg>
</bean>
<!--有参数构造注入属性方式2:index-->
<bean id="book" class="com.xiaoqiu.DI.Book">
<!--使用constructor-arg完成属性注入 index:按构造方法参数顺序从0开始 value:向属性注入的值-->
<constructor-arg index="0" value="西游记"></constructor-arg>
<constructor-arg index="1" value="吴承恩"></constructor-arg>
</bean>
</beans>
3)p 名称空间注入(了解)
使用 p 名称空间注入,可以简化基于 xml 配置方式(本质上在set注入方法上做了优化)
(1)添加 p 名称空间在配置文件中
<?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">
</beans>
(2)创建类,定义属性和对应的 set 方法同时需要保证无参构造方法有效
public class Book {
private String bname;
private String bauthor;
public void setBname(String bname) {
this.bname = bname;
}
public void setBauthor(String bauthor) {
this.bauthor = bauthor;
}
}
(3)进行属性注入,在 bean 标签里面进行操作
<?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">
<bean id="book" class="com.xiaoqiu.DI.Book" p:bname="西游记" p:bauthor="吴承恩"></bean>
</beans>
4)xml 注入null 值和包含特殊符号
其他的内容同上,只在bean赋值阶段有区别
①null 值
<bean id="book" class="com.xiaoqiu.DI.Book">
<property name="bname" value="西游记"></property>
<!--null 值-->
<property name="bauthor">
<value>null</value>
</property>
</bean>
②属性值包含特殊符号
<bean id="book" class="com.xiaoqiu.DI.Book">
<!--属性值包含特殊符号-->
<!--1、把<>进行转义 < >-->
<property name="bname" value="<西游记>"></property>
<property name="bauthor">
<!--2、把带特殊符号内容写到 CDATA-->
<value><![CDATA[《吴承恩》]]></value>
</property>
</bean>
5)注入属性-外部 bean
(1)创建 service 类和 dao 类,在 service 调用 dao 里面的方法
Dao类
public class Dao {
public void add(){
System.out.println("add.....");
}
}
Service类
public class Service {
private Dao dao;
public void setDao(Dao dao) {
this.dao = dao;
}
public void add(){
System.out.println("service......");
dao.add();
}
}
(2)在 spring 配置文件中进行配置
<!-- 创建dao对象-->
<bean id="dao" class="com.xiaoqiu.Dao"></bean>
<!-- 创建service对象-->
<bean id="service" class="com.xiaoqiu.Service">
<!--注入dao 对象
name 属性:类里面属性名称
ref 属性:创建Dao对象bean标签id值-->
<property name="dao" ref="dao"></property>
</bean>
6)注入属性-内部 bean
(1)创建 Dept部门 类和 Emp员工类,在员工类中添加部门属性
Dept类
public class Dept {
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
}
Emp类
public class Emp {
private Integer id;
private String ename;
private Dept dept;
public void setId(Integer id) {
this.id = id;
}
public void setEname(String ename) {
this.ename = ename;
}
public void setDept(Dept dept) {
this.dept = dept;
}
}
(2)在 spring 配置文件中进行配置
<!--创建emp对象-->
<bean id="emp" class="com.xiaoqiu.Emp">
<!--普通属性赋值-->
<property name="id" value="1001"></property>
<property name="ename" value="张三"></property>
<!--对象属性赋值-->
<property name="dept">
<!--创建dept对象-->
<bean id="dept" class="com.xiaoqiu.Dept">
<!--dept属性赋值-->
<property name="dname" value="财务部"></property>
</bean>
</property>
</bean>
7)注入属性-级联赋值
案例同上
第一种写法
<!--创建emp对象-->
<bean id="emp" class="com.xiaoqiu.Emp">
<!--普通属性赋值-->
<property name="id" value="1001"></property>
<property name="ename" value="张三"></property>
<!--对象属性赋值-->
<property name="dept" ref="dept"></property>
</bean>
<!--创建dept对象-->
<bean id="dept" class="com.xiaoqiu.Dept">
<!--dept属性赋值-->
<property name="dname" value="财务部"></property>
</bean>
第二种写法
需要在emp类中增加dept属性的get方法
public class Emp {
private Integer id;
private String ename;
private Dept dept;
public Dept getDept() { return dept; }
public void setId(Integer id) { this.id = id;}
public void setEname(String ename) { his.ename = ename; }
public void setDept(Dept dept) { this.dept = dept;}
}
<!--创建emp对象-->
<bean id="emp" class="com.xiaoqiu.Emp">
<!--普通属性赋值-->
<property name="id" value="1001"></property>
<property name="ename" value="张三"></property>
<!--对象属性赋值-->
<property name="dept.dname" value="技术部"></property>
</bean>
<!--创建dept对象-->
<bean id="dept" class="com.xiaoqiu.Dept">
<!--dept属性赋值-->
<property name="dname" value="财务部"></property>
</bean>
8)xml 注入集合属性
①注入数组类型属性
②注入 List 集合类型属性
③注入 Map 集合类型属性
④注入 Set 集合类型属性
(1)创建Stu类定义数组、List、Map、Set等属性值,生成对应 set 方法
public class Stu {
//1 数组类型属性
private String[] courses;
//2 list 集合类型属性
private List<String> list;
//3 map 集合类型属性
private Map<String,String> maps;
//4 set 集合类型属性
private Set<String> sets;
public void setSets(Set<String> sets) {
this.sets = sets;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
}
(2)在 spring 配置文件进行配置
<!--集合类型属性注入-->
<bean id="stu" class="com.xiaoqiu.Stu">
<!--数组类型属性注入-->
<property name="courses">
<array>
<value>java 课程</value>
<value>数据库课程</value>
</array>
</property>
<!--list 类型属性注入-->
<property name="list">
<list>
<value>张三</value>
<value>小三</value>
</list>
</property>
<!--map 类型属性注入-->
<property name="maps">
<map>
<entry key="JAVA" value="java"></entry>
<entry key="PHP" value="php"></entry>
</map>
</property>
<!--set 类型属性注入-->
<property name="sets">
<set>
<value>MySQL</value>
<value>Redis</value>
</set>
</property>
</bean>
9)在集合里面设置对象类型值
(1)创建book类与Stu类
public class Book {
private String bname;
public void setBname(String bname) {
this.bname = bname;
}
}
public class Stu {
//list 集合类型属性
private List list;
public void setList(List<Object> list) {
this.list = list;
}
}
(2)在 spring 配置文件进行配置(其他类型同理的做法)
<!--创建多个book对象-->
<bean id="book1" class="com.xiaoqiu.Book">
<property name="bname" value="Spring5 框架"></property>
</bean>
<bean id="book2" class="com.xiaoqiu.Book">
<property name="bname" value="MyBatis 框架"></property>
</bean>
<!--集合类型属性注入-->
<bean id="stu" class="com.xiaoqiu.Stu">
<!--注入 list 集合类型,值是对象-->
<property name="list">
<list>
<ref bean="book1"></ref>
<ref bean="book2"></ref>
</list>
</property>
</bean>
10)把集合注入部分提取出来
(1)在 spring 配置文件中引入名称空间 util
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
</beans>
(2)使用 util 标签完成 list 集合注入提取
<!--1 提取 list 集合类型属性注入-->
<util:list id="bookList">
<value>易筋经</value>
<value>九阴真经</value>
<value>九阳神功</value>
</util:list>
<!--2 提取 list 集合类型属性注入使用-->
<bean id="stu" class="com.xiaoqiu.Stu">
<property name="list" ref="bookList"></property>
</bean>
3、xml 自动装配
什么是自动装配?
根据指定装配规则(属性名称或者属性类型),Spring 自动将匹配的属性值进行注入,bean 标签属性 autowire,配置自动装配
autowire 属性常用两个值:
- byName 根据属性名称注入 ,注入值 bean 的 id 值和类属性名称一样
- byType 根据属性类型注入
1)根据属性名称自动注入
<bean id="emp" class="com.xiaoqiu.Emp" autowire="byName"></bean>
<bean id="dept" class="com.xiaoqiu.Dept"></bean>
2)根据属性类型自动注入
<bean id="emp" class="com.xiaoqiu.Emp" autowire="byType"></bean>
<bean id="dept" class="com.xiaoqiu.Dept"></bean>
4、注入外部属性文件
引入依赖的jar包
1)直接配置数据库信息
<!--直接配置数据库信息-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
2)引入外部属性文件配置数据库连接池
(1)创建外部属性文件,properties 格式文件,写数据库信息
jdbc.properties
prop.driverClass=com.mysql.jdbc.Driver
prop.url= jdbc:mysql://localhost:3306/test
prop.userName=root
prop.password=123456
(2)引入 context 名称空间
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
(3)在 spring 配置文件使用标签引入外部 properties 属性文件
<!--引入外部属性文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClass}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.userName}"></property>
<property name="password" value="${prop.password}"></property>
</bean>