spring入门:
已学过一遍,这次用来记录复习。有什么不对的地方欢迎指正交流。
IOC(控制反转):我的理解是就是容器技术。将所需的东西放入容器中,需要的时候取出来就可以了。这个容器平时又叫spring容器。
这里简单看一下基本的,原始的,非spring环境的:
这种最基本的方式,使用的是Java的new 对象的方式,这种方式的耦合度很高,试想:如果某个类的方法要修改,很麻烦,耦合度很高。
BookDao:
package Dao;
public interface BookDao {
public void save();
}
BookDaoImpl:
package Dao;
public class BookDaoImpl implements BookDao{
@Override
public void save() {
System.out.println("BookDaoImpl is running");
}
}
BookDaoService:
package Service;
public interface BookDaoService {
public void save();
}
BookDaoServiceImpl:
package Service;
import Dao.BookDao;
import Dao.BookDaoImpl;
public class BookDaoServiceImpl implements BookDaoService {
BookDao bookDao = new BookDaoImpl();
@Override
public void save() {
bookDao.save();
System.out.println("service is running ");
}
}
App:
import Service.BookDaoService;
import Service.BookDaoServiceImpl;
public class App {
public static void main(String[] args) {
BookDaoService bookDaoService = new BookDaoServiceImpl();
bookDaoService.save();
}
}
运行结果:
如果使用IOC呢?
App2:
import Dao.BookDao;
import Service.BookDaoService;
import com.sun.org.apache.bcel.internal.util.ClassPath;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App2 {
public static void main(String[] args) {
//获取Ioc容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取bean
BookDao bookDao =(BookDao) ctx.getBean("bookDao");
bookDao.save();
BookDaoService bookDaoService = (BookDaoService) ctx.getBean("bookDaoimpl");
bookDaoService.save();
}
}
配置文件:
<?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="Dao.BookDaoImpl" />
<bean id="bookDaoimpl" class="Service.BookDaoServiceImpl" />
</beans>
怎么使用DI呢?
配置文件:
<?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.导入spring的坐标spring-context,对应版本是5.2.10.RELEASE-->
<!--2.配置bean-->
<!--bean标签标示配置bean
id属性标示给bean起名字
class属性表示给bean定义类型-->
<bean id="bookDao" class="Dao.BookDaoImpl"/>
<bean id="bookService" class="Service.BookDaoServiceImpl">
<!--7.配置server与dao的关系-->
<!--property标签表示配置当前bean的属性
name属性表示配置哪一个具体的属性
ref属性表示参照哪一个bean-->
<!-- name后面的bookDao指的是BookDaoServiceImol里面的bookDao ref后面的bookDao是一个bean注入到另外一个bean里面 -->
<property name="bookDao" ref="bookDao"/>
</bean>
</beans>
BookDaoServiceImpl:
package Service;
import Dao.BookDao;
import Dao.BookDaoImpl;
public class BookDaoServiceImpl implements BookDaoService {
//删除使用new的方式
private BookDao bookDao;
public void save() {
bookDao.save();
System.out.println("service is running ");
}
//提供set对应的方法
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
}
bookDao.save();
System.out.println("service is running ");
}
//提供set对应的方法
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
}