2-1 IOC及Bean容器
- 接口及面向接口编程
- 什么是IOC
IOC:控制反转,控制权的转移,应用程序本身不负责依赖对象的创建和维护,而是由外部容器负责创建和维护
DI:依赖注入,是一种实现的方式
目的:创建对象并且组装对象之间的关系 - Spring的Bean配置
- Bean的初始化
- Spring的常用注入方式
- 设值注入
- 构造注入
Spring的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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloWorId" class="springTest.HelloWorld">
<property name="message" value="Hello Word"/>
</bean>
</beans>
Bean容器的初始化
- 基础:两个包
- org.springframework.beans
- org.springframework.context
- BeanFactory提供配置结构和基本功能,加载并初始化Bean
- ApplicationContext保存了Bean对象并在Spring中被广泛使用
- 初始化方式,ApplicationContext
- 本地文件
- Classpath
- Web应用中依赖servlet或Listener
- 文件: FileSystemXmlApplicationContext context =
new FileSystemXmlApplicationContext(“D:/resourcess/spring-ioc.xml”);
- Classpath: ClassPathXmlApplicationContext applicationContext= new ClassPathXmlApplicationContext(springXmlPath.split("[,\s]"));
- Web应用
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">
<bean id="bookDao" class="com.torey.dao.impl.BookDaoImpl"></bean>
<bean id="bookService" class="com.torey.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao"/>
</bean>
</beans>
构造注入
<?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">
<bean id="bookDao" class="com.torey.dao.impl.BookDaoImpl"></bean>
<bean id="bookService" class="com.torey.service.impl.BookServiceImpl">
<constructor-arg name="bookDao" ref="bookDao"/>
</bean>
</beans>
单元测试说明
- 下载
源码
spring-ioc.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="oneInterface" class="com.torey.springtest.OneInterfaceImpl"/>
</beans>
UnitTestBase.java
package com.torey.springtest.base;
import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;
/**
* @ClassName:UnitTestBase
* @Description:
* @author: Torey
*/
public class UnitTestBase {
public UnitTestBase(){}
private String springXmlPath;
private ClassPathXmlApplicationContext applicationContext;
public UnitTestBase(String springXmlpath){
this.springXmlPath=springXmlpath;
}
@Before
public void before(){
if (StringUtils.isEmpty(springXmlPath)) {
springXmlPath="classpath*:spring-*.xml";
}
applicationContext= new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]"));
applicationContext.start();
}
@After
public void after(){
applicationContext.destroy();
}
public Object getBean(String beanId){
return applicationContext.getBean(beanId);
}
}
TestOneInterface .java
package com.torey.springtest.ioc;
import com.torey.springtest.OneInterface;
import com.torey.springtest.base.UnitTestBase;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.Test;
/**
* @ClassName:TestOneInterface
* @Description:
* @author: Torey
*/
@RunWith(BlockJUnit4ClassRunner.class)
public class TestOneInterface extends UnitTestBase {
public TestOneInterface(){
super("classpath*:spring-ioc.xml");
}
@Test
public void testHello(){
OneInterface oneInterface = (OneInterface)super.getBean("oneInterface");
System.out.println(oneInterface.hello("hello"));
}
}
2-2 Spring注入方式
设值注入源码
BookDao.java
package com.torey.dao;
/**
* @ClassName:BookDao
* @Description:与数据库交互
* @author: Torey
*/
public interface BookDao {
void addBook();
}
BookDaoImpl.java
package com.torey.dao.impl;
import com.torey.dao.BookDao;
/**
* @ClassName:BookDaoImpl
* @Description:
* @author: Torey
*/
public class BookDaoImpl implements BookDao {
@Override
public void addBook() {
System.out.println(this.getClass().getName()+"Dao一般与数据库交互");
}
}
BookService.java
package com.torey.service;
/**
* @InterfaceName:BookService 用途:
* 作者: litaofeng
* 时间: 2019/8/18 8:56
*/
public interface BookService {
void addBook();
}
BookServiceImpl.java
package com.torey.service.impl;
import com.torey.dao.BookDao;
import com.torey.service.BookService;
/**
* @ClassName:BookServiceImpl
* @Description:
* @author: Torey
*/
public class BookServiceImpl implements BookService {
private BookDao bookDao;
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
@Override
public void addBook() {
System.out.println(this.getClass().getName()+"service一般写业务逻辑");
bookDao.addBook();
}
}
TestOneInterface.java单元测试类
package com.torey.springtest.ioc;
import com.torey.service.BookService;
import com.torey.springtest.base.UnitTestBase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
/**
* @ClassName:TestOneInterface
* @Description:
* @author: Torey
*/
@RunWith(BlockJUnit4ClassRunner.class)
public class TestOneInterface extends UnitTestBase {
public TestOneInterface() {
super("classpath*:spring-ioc.xml");
}
@Test
public void testHello() {
BookService bookService = (BookService) super.getBean("bookService");
bookService.addBook();
}
}
执行结果
构造函数注入
package com.torey.service.impl;
import com.torey.dao.BookDao;
import com.torey.service.BookService;
/**
* @ClassName:BookServiceImpl
* @Description:
* @author: Torey
*/
public class BookServiceImpl implements BookService {
private BookDao bookDao;
public BookServiceImpl(){}
public BookServiceImpl(BookDao bookDao){
this.bookDao=bookDao;
}
@Override
public void addBook() {
System.out.println(this.getClass().getName()+"service一般写业务逻辑");
bookDao.addBook();
}
}
spring-ioc.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bookDao" class="com.torey.dao.impl.BookDaoImpl"></bean>
<bean id="bookService" class="com.torey.service.impl.BookServiceImpl">
<constructor-arg name="bookDao" ref="bookDao"/>
</bean>
</beans>