1.1 框架概述
2.1 Spring程序开发步骤
2.2 导入Spring开发的基本包坐标
<properties>
<spring.version>5.0.5.RELEASEspring.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.0.5.RELEASEversion>
dependency>
dependencies>
2.3 编写Dao接口和实现类
public interface UserDao {
public void find();
}
public class UserDaoImpl implements UserDao {
@Override
public void find() {
System.out.println("UserDao find method running....");
}
}
2.4 创建Spring核心配置文件
<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">
beans>
2.5 在Spring配置文件中配置UserDaoImpl
<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">
<bean id="userDao" class="www.baidu.dao.impl.UserDaoImpl">bean>
beans>
2.6 使用Spring的API获得Bean实例
@Test
public void test1(){
//加载spring配置文件。获取spring核心容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取spring容器中的userDao对象
UserDao userDao = (UserDao) ac.getBean("userDao");
//调用方法
userDao.find();
}
3.1 Bean标签基本配置
<bean id="userDao" class="www.baidu.dao.impl.UserDaoImpl">bean>
id:Bean实例在Spring容器中的唯一标识
class:Bean的全限定名称
3.2 Bean标签范围配置
<bean id="userDao" class="www.baidu.dao.impl.UserDaoImpl" scope="singleton" >bean>
scope:指对象的作用范围,取值如下:
3.3 Bean生命周期配置
<bean id="userDao" class="www.baidu.dao.impl.UserDaoImpl"/>
//工厂类
public class StaticFactoryBean {
//静态方法,通过该方法获取对象
public static UserDao createUserDao(){
return new UserDaoImpl();
}
}
<bean id="userDao" class="www.baidu.factory.StaticFactoryBean" factory-method="createUserDao" />
//工厂类
public class DynamicFactoryBean {
//非静态方法,需要先创建工厂类的对象,再通过对象调用该方法,获取对象
public UserDao createUserDao(){
return new UserDaoImpl();
}
}
<bean id="factoryBean" class="www.baidu.factory.DynamicFactoryBean"/>
<bean id="userDao" factory-bean="factoryBean" factory-method="createUserDao"/>
4.1 概述
public class UserServiceImpl implements UserService {
private UserDao userDao;
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void find() {
userDao.find();
}
}
<bean id="userDao" class="www.baidu.dao.impl.UserDaoImpl"/>
<bean id="userService" class="www.baidu.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao">constructor-arg>
bean>
4.2.2 set方法赋值
在UserServiceImpl中添加setUserDao方法
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void find() {
userDao.find();
}
}
配置Spring容器调用set方法进行注入
<bean id="userDao" class="www.baidu.dao.impl.UserDaoImpl"/>
<bean id="userService" class="www.baidu.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
bean>
4.3 Bean的依赖注入的数据类型
public class UserDaoImpl implements UserDao {
private String company;
private int age;
public void setCompany(String company) {
this.company = company;
}
public void setAge(int age) {
this.age = age;
}
public void save() {
System.out.println(company + "===" +age);
}
}
<bean id="userDao" class="www.baidu.dao.impl.UserDaoImpl">
<property name="company" value="百度">property>
<property name="age" value="20">property>
bean>
public class UserDaoImpl implements UserDao {
private List<String> strList;
public void setStrList(List<String> strList) {
this.strList = strList;
}
public void save() {
System.out.println(strList);
System.out.println("UserDao save method running....");
}
}
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
<property name="strList">
<list>
<value>aaavalue>
<value>bbbvalue>
<value>cccvalue>
list>
property>
bean>
(2)集合数据类型(List
public class UserDaoImpl implements UserDao {
private List<User> userList;
public void setUserList(List<User> userList) {
this.userList = userList;
}
public void save() {
System.out.println(userList);
System.out.println("UserDao save method running....");
}
}
<bean id="u1" class="www.baidu.domain.User"/>
<bean id="u2" class="www.baidu.domain.User"/>
<bean id="userDao" class="www.baidu.dao.impl.UserDaoImpl">
<property name="userList">
<list>
<bean class="www.baidu.domain.User"/>
<bean class="www.baidu.domain.User"/>
<ref bean="u1"/>
<ref bean="u2"/>
list>
property>
bean>
(3)集合数据类型( Map
public class UserDaoImpl implements UserDao {
private Map<String,User> userMap;
public void setUserMap(Map<String, User> userMap) {
this.userMap = userMap;
}
public void save() {
System.out.println(userMap);
System.out.println("UserDao save method running....");
}
}
<bean id="u1" class="www.baidu.domain.User"/>
<bean id="u2" class="www.baidu.domain.User"/>
<bean id="userDao" class="www.baidu.dao.impl.UserDaoImpl">
<property name="userMap">
<map>
<entry key="user1" value-ref="u1"/>
<entry key="user2" value-ref="u2"/>
map>
property>
bean>
(4)集合数据类型(Properties)的注入
public class UserDaoImpl implements UserDao {
private Properties properties;
public void setProperties(Properties properties) {
this.properties = properties;
}
public void save() {
System.out.println(properties);
System.out.println("UserDao save method running....");
}
}
<bean id="userDao" class="www.baidu.dao.impl.UserDaoImpl">
<property name="properties">
<props>
<prop key="p1">aaaprop>
<prop key="p2">bbbprop>
<prop key="p3">cccprop>
props>
property>
bean>
5.1 引入其他配置文件(分模块开发)
实际开发中,Spring的配置内容非常多,这就导致Spring配置很繁杂且体积很大,所以,可以将部分配置解到其他配置文件中,而在Spring主配置文件通过import标签进行加载
<import resource="applicationContext-xxx.xml"/>
5.2 getBean()方法使用
//加载配置文件,获取Spring核心容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//从Spring核心容器中获取对象
//获取id="userService"的对象
UserService us1 = (UserService)ac.getBean("userService");
//获取类型为"UserService"的对象,
//注意,该操作要求,在spring核心容器中只能有一个"UserService"类型的对象
UserService us2 = ac.getBean(UserService.class);
//获取类型为"UserService",id为"userService"的对象,
UserService us3 = ac.getBean("userService",UserService.class);