能够说出Spring的体系结构
能够编写IOC入门案例
能够编写DI入门案例
能够配置setter方式注入属性值
能够配置构造方式注入属性值
能够理解什么是自动装配
<bean>标签中id属性和class属性的作用是什么?
x【第一步】导入Spring坐标【第二步】定义Spring管理的类(接口)【第三步】创建Spring配置文件,配置对应类作为Spring管理的bean对象【第四步】初始化IOC容器(Spring核心容器/Spring容器),通过容器获取bean对象
【第一步】导入Spring坐标
xxxxxxxxxx
<dependencies> <!--导入spring的坐标spring-context,对应版本是5.2.10.RELEASE--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.10.RELEASE</version> </dependency></dependencies>
【第二步】定义Spring管理的类(接口)
x
public interface BookDao { public void save();}public class BookDaoImpl implements BookDao { public void save() { System.out.println("book dao save ..."); }}
xxxxxxxxxx
public interface BookService { public void save();}public class BookServiceImpl implements BookService { private BookDao bookDao = new BookDaoImpl(); public void save() { System.out.println("book service save ..."); bookDao.save(); }}
【第三步】创建Spring配置文件,配置对应类作为Spring管理的bean对象
xxxxxxxxxx
<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标签:表示配置bean id属性:表示给bean起名字 class属性:表示给bean定义类型 --> <bean id="bookService" class="com.itheima.service.impl.BookServiceImpl"></bean></beans>
==注意事项:bean定义时id属性在同一个上下文中(IOC容器中)不能重复==
【第四步】初始化IOC容器(Spring核心容器/Spring容器),通过容器获取Bean对象
xxxxxxxxxx
public class App { public static void main(String[] args) { //1.创建IoC容器对象,加载spring核心配置文件 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //2 从IOC容器中获取Bean对象(BookService对象) BookService bookService= (BookService)ctx.getBean("bookService"); //3 调用Bean对象(BookService对象)的方法 bookService.save(); }}

<property>标签中name属性和ref属性的作用是什么?
xxxxxxxxxx【第一步】删除使用new的形式创建对象的代码【第二步】提供依赖对象对应的setter方法【第三步】配置service与dao之间的关系
【第一步】删除使用new的形式创建对象的代码
xxxxxxxxxx
public class BookServiceImpl implements BookService { private BookDao bookDao; //【第一步】删除使用new的形式创建对象的代码 public void save() { System.out.println("book service save ..."); bookDao.save(); }
}
【第二步】提供依赖对象对应的setter方法
xxxxxxxxxx
public class BookServiceImpl implements BookService { private BookDao bookDao; public void save() { System.out.println("book service save ..."); bookDao.save(); } //【第二步】提供依赖对象对应的setter方法 public void setBookDao(BookDao bookDao) { this.bookDao = bookDao; }}
【第三步】配置service与dao之间的关系
在applicationContext.xml中配置
xxxxxxxxxx
<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标签:表示配置bean id属性:表示给bean起名字 class属性:表示给bean定义类型 --> <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/> <bean id="bookService" class="com.itheima.service.impl.BookServiceImpl"> <!--配置server与dao的关系 property标签:表示配置当前bean的属性 name属性:表示配置哪一个具体的属性 ref属性:表示参照哪一个bean --> <property name="bookDao" ref="bookDao"/> </bean></beans>
