文章目录
Spring作用
方便解耦,简化开发
Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理
AOP编程的支持
Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能
声明式事务的支持
只需要通过配置就可以完成对事务的管理,而无需手动编程
方便程序的测试
Spring对Junit的支持,可以通过注解方便的测试Spring程序
方便集成各种优秀框架
Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts2、Hibernate、MyBatis、Quartz等)的直接支持
降低JavaEE API的使用难度
Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低。
IOC
spring最重要的就是IOC(控制反转)和AOP(面向切面),控制反转的意思就是将对象交给容器来管理,而不是在我们需要的时候new一个。
‘
谁控制谁,控制什么:传统Java SE程序设计,我们直接在对象内部通过new进行创建对象,是程序主动去创建依赖对象;而IoC是有专门一个容器来创建这些对象,即由Ioc容器来控制对象的创建;谁控制谁?当然是IoC 容器控制了对象;控制什么?那就是主要控制了外部资源获取(不只是对象包括比如文件等)。
为何是反转,哪些方面反转了:有反转就有正转,传统应用程序是由我们自己在对象中主动控制去直接获取依赖对象,也就是正转;而反转则是由容器来帮忙创建及注入依赖对象;为何是反转?因为由容器帮我们查找及注入依赖对象,对象只是被动的接受依赖对象,所以是反转;哪些方面反转了?依赖对象的获取被反转了。
Spring中的ApplicationContext接口和ClassPathXmlApplicationContext
a:相当于一个bean工厂,用于管理,装配对象。
c:用于加载xml文件。
第一个spring demo
1.先添加依赖
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.3</version>
</dependency>
</dependencies>
2.分别创建dao和daoimpl
package edu.xja.dao;
package edu.xja.dao.impl;
import edu.xja.dao.CustomerDao;
public class CustomerDaoImpl implements CustomerDao {
@Override
public void save() {
System.out.println("存入数据了");
}
}
public interface CustomerDao {
void save();
}
3.创建application.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!-- 创建 CustomerDaoImpl对象-->
<bean id="customerDao" class="edu.xja.dao.impl.CustomerDaoImpl"></bean>
</beans>
4.创建测试类:
@Test
void save() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerDao customerDao = (CustomerDao) applicationContext.getBean("customerDao");
customerDao.save();
ClassPathXmlApplicationContext c = (ClassPathXmlApplicationContext) applicationContext;
c.close();
}
结果
好处:省去了在类里面new,降低程序的耦合性
在应用中mvc中,serviceimpl中通常要实例化一个daoimpl,测试也要实例化一个serviceimpl,这时有两种方法创建对象
第一种:利用构造方法创建对象
1.配置xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!-- 创建 CustomerDaoImpl对象-->
<bean id="customerDao" class="edu.xja.dao.impl.CustomerDaoImpl"></bean>
<bean id="customerService" class="edu.xja.service.impl.CustomerServiceImpl">
<!-- 通过构造方法创建对象-->
<constructor-arg index="0" ref="customerDao"></constructor-arg>
<!-- <property name="customerDao" ref="customerDao"></property>-->
</bean>
</beans>
2.创建service和serviceimpl
package edu.xja.service;
public interface CustomerService {
void save();
}
package edu.xja.service.impl;
import edu.xja.dao.CustomerDao;
import edu.xja.service.CustomerService;
public class CustomerServiceImpl implements CustomerService {
private CustomerDao customerDao;
public CustomerServiceImpl() {
}
public CustomerServiceImpl(CustomerDao customerDao) {
this.customerDao = customerDao;
}
public CustomerDao getCustomerDao() {
return customerDao;
}
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
@Override
public void save() {
customerDao.save();
}
}
3.创建测试类
package edu.xja.service.impl;
import edu.xja.service.CustomerService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.junit.jupiter.api.Assertions.*;
class CustomerServiceImplTest {
@Test
void save() {
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerService customerService = (CustomerService) applicationContext.getBean("customerService");
customerService.save();
}
}
第二种:通过set方法注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!-- 创建 CustomerDaoImpl对象-->
<bean id="customerDao" class="edu.xja.dao.impl.CustomerDaoImpl"></bean>
<bean id="customerService" class="edu.xja.service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"></property>
</bean>
</beans>