Spring工具包下载地址:https://repo.spring.io/libs-release-local/org/springframework/spring/
参考博客:https://blog.csdn.net/sinat_32012145/article/details/77988423
全局配置文件
配置pom.xml,导入spring依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mj</groupId>
<artifactId>day01_01jdbc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
</dependencies>
</project>
映射配置文件
在工程路径下创建一个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">
</beans>
把对象创建交给spring管理
spring创建Bean的三种方式:
-
第一种:使用默认构造函数
在配置文件中使用bean标签,配以id和class属性,且无其他属性时,调用默认构造函数 -
第二种:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)
<bean id="xxx" class="工厂类的全类名"> </bean>
<bean id="*****" factory-bean="工厂类的bean id :xxx" factory-method="方法名">
</bean>
- 第三种:使用普通工厂中的静态 方法创建对象
<bean id="xxx" class="工厂类的全类名" factory-method="方法名"> </bean>
<!--bean的作用范围
bean的scope属性:
singleton:单例(默认)
prototype:多例
request:作用于web的请求范围
session:作用于web的会话范围
global-session:作用于集群环境的会话范围(全局会话)
-->
<!-- bean对象的生命周期
单例:生命周期与容器一致
多例:使用对象时才创建对应对象
-->
依赖注入
把类与类之间的依赖交给Spring管理,降低程序的耦合性。
针对以下AccountServiceImpl 类配置映射文件
public class AccountServiceImpl implements IAccountService {
//基本类型
public String name;
public String sex;
public Integer age;
//其他bean类型
public Date birthday;
private IAccountDao accountDao ;//自定义类
//集合
public Set<String> myset;
public Map<String,String> mymap;
public AccountServiceImpl() {
System.out.println("对象通过默认构造函数创建了");
}
public AccountServiceImpl(String name, String sex, Integer age, Date birthday) {
this.name = name;
this.sex = sex;
this.age = age;
this.birthday = birthday;
System.out.println("对象通过有参构造器创建了");
}
public void setUserName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public void setMySet(Set<String> myset) {
this.myset = myset;
}
public void setMyMap(Map<String, String> mymap) {
this.mymap = mymap;
}
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
- 构造函数注入
<bean id="accountService" class="com.mj.service.AccountServiceImpl"
scope="singleton" init-method="init" destroy-method="destroy">
<!--构造函数注入
constructor-arg标签的属性:
type:类属性的类型
index:构造函数的参数索引,从0开始
name: 给构造函数中的指定名称的参数赋值
==============================================================
value: 作用于基本数据类型
ref: 作用于指定的其他bean类型数据,在IOC核心容器中出现过的bean对象
缺点:必须给构造函数的所有参数赋值,即使用不到某些参数也必须赋值,否则程序运行报错
-->
<constructor-arg type="java.lang.String" value="张长"></constructor-arg>
<constructor-arg type="java.lang.Integer" value="18"></constructor-arg>
<constructor-arg name="sex" value="男"></constructor-arg>
<constructor-arg name="birthday" ref="nowdate"></constructor-arg>
</bean>
<!--配置一个日期对象-->
<bean id="nowdate" class="java.util.Date"></bean>
<bean id="accountDao" class="com.mj.dao.AccountDaoImpl">
</bean>
- set方法注入
<bean id="accountService2" class="com.mj.service.AccountServiceImpl"
scope="singleton" >
<!--set方法注入
name:set方法名称 如方法名为setUserName,则name="userName"
-->
<property name="userName" value="莓莓"></property>
<property name="sex" value="女"></property>
<property name="birthday" ref="nowdate"></property>
<property name="accountDao" ref="accountDao"></property>
</beans>
- 集合注入
<bean id="accountService2" class="com.mj.service.AccountServiceImpl"
scope="singleton" init-method="init1" destroy-method="destroy">
<!--set方法注入
name:set方法名称 如方法名为setUserName,则name="userName"
-->
<property name="userName" value="莓莓"></property>
<property name="sex" value="女"></property>
<property name="birthday" ref="nowdate"></property>
<property name="accountDao" ref="accountDao"></property>
<property name="mySet">
<set>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</set>
</property>
<property name="myMap">
<map>
<entry key="aa">
<value>AA</value>
</entry>
<entry key="bb" value="BB"></entry>
</map>
</property>
<property name="myPro">
<props>
<prop key="vv">nnn</prop>
<prop key="mm">fff</prop>
</props>
</property>
</bean>
</beans>
测试代码
public class Client {
public static void main(String[] args) {
/*
* 获取spring的Ioc核心容器,根据id获取对象
* ClassPathXmlApplicationContext:获取类路径下的配置文件
* FileSystemXmlApplicationContext: 获取磁盘任意路径下的配置文件
*AnnotationConfigApplicationContext:用于读取注解创建容器
* */
//1.获取spring的Ioc核心容器
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
// ApplicationContext ac=new FileSystemXmlApplicationContext("d:\\bean.xml");
//2.根据id获取对象
IAccountService as= (IAccountService) ac.getBean("accountService2");
IAccountDao ad = ac.getBean("accountDao",IAccountDao.class);
System.out.println(as);
System.out.println(ad);
}