一、Spring框架
1.下载jar
基础jar(5+1)
spring-beans.jar
spring-core.jar
spring-context.jar
spring-aop.jar
spring-expression.jar
日志
commons-logging.jar
2. appliactionContext.xml配置文件
2.1. applicationContext头文件
基础头文件 bean context
开发事务 tx
开发AOP加上 aop
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
2.2. 在配置文件中放入Bean,依赖注入的三种方式
2.2.1. 通过property的方式
对象类必须有set方法
package com.test.entity;
public class Student {
private String stuName;
public void setStuName(String stuName) {
this.stuName = stuName;
}
}
配置文件中加入
<!--id=起个名字,class=对象类的完整包路径-->
<bean id="student" class=“com.test.entity.Student”>
<!--name=属性名,和类中属性名一致,value=属性值-->
<property name="stuName" value="张三">
</bean>
2.2.2. 通过构造器的方式
类中必须有有参构造方法
package com.test.entity;
public class Student {
private int stuId;
private String stuName;
private int stuAge;
public Student() {
super();
}
//必须有
public Student(int stuId, String stuName, int stuAge) {
super();
this.stuId = stuId;
this.stuName = stuName;
this.stuAge = stuAge;
}
}
配置文件
<bean id="student" class="com.test.entity.Student">
<!-- 通过构造器的方式 1. 顺序对应构造方法的参数顺序,2.可以指定类型,3. 按照下标位置赋值-->
<constructor-arg value="1" type="int" index="1"></constructor-arg>
<constructor-arg value="zs" type="String" index="2"></constructor-arg>
<constructor-arg value="18" type="int" index="3"></constructor-arg>
</bean>
2.2.3. p命名空间赋值
使用p命名空间必须在头文件加入p标签
xmlns:p=“http://www.springframework.org/schema/p”
<!-- P命名空间赋值 -->
<bean id="student" class="com.test.entity.Student" p:stuId="1" p:stuAge="23" p:stuName="zs"></bean>
2.3. 不同Bean之间的依赖注入
StudentServiceImpl实现类中有一个Dao的属性
之前需要通过new的方式赋值
public class StudentServiceImpl implements IStudentService{
StudentDaoImpl studentDao=new StudentDaoImpl();//需要new
@Override
public void addStudent(Student student) {
studentDao.addStudent(student);//再调用方法
}
}
现在不需要赋值,
package com.test.service.impl;
public class StudentServiceImpl implements IStudentService{
StudentDaoImpl studentDao;//不需要赋值
public void setStudentDao(StudentDaoImpl studentDao) {
this.studentDao = studentDao;
}
@Override
public void addStudent(Student student) {
studentDao.addStudent(student);
}
}
配置文件
<!--先将dao放入IOC容器-->
<bean id="studentDao" class="com.test.dao.impl.StudentDaoImpl"></bean>
<!--再将Service实现类放入IOC容器-->
<bean id="studentService" class="com.test.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"></property> //将上面的dao注入到Service实现类的dao属性中
</bean>
2.4. 事务配置
**
开启一个名叫txManager的事务
<tx:annotation-driven transaction-manager=“txManager”/>
配置事务
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"></property>
</bean>
事务需要一个数据源DataSource,再配置下DataSource
<!-- 配置数据库相关 -->
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
2.5. 基于配置的AOP实现
将普通类变成通知类,需要实现接口即可
前置通知->MethodBeforeAdvice
后置通知->AfterReturningAdvice
异常通知->ThrowsAdvice
环绕通知->MethodInterceptor
例如:
public class BeforeAdvice implements MethodBeforeAdvice {
//将此类变成前置通知,必须重写接口的before方法
@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println("打开数据库连接...");
}
}
再将通知类放入IOC容器中
<bean id="beforeAdvice" class="com.test.advice.BeforeAdvice"></bean>
将具体要加通知的方法所在的类放入IOC容器中
<bean id="studentService" class="com.test.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"></property>
</bean>
配置AOP(将通知和方法连接起来)
<aop:config>
<!--配置切入点,既在那个方法前执行通知-->
<aop:pointcut expression="execution(public void com.test.service.impl.StudentServiceImpl.addStudent(com.test.entity.Student))" id="pointcut1">
</aop:pointcut>
<!--配置通知 pointcut-ref将此通知和切入点连接起来-->
<aop:advisor advice-ref="beforeAdvice" pointcut-ref="pointcut1">
</aop:advisor>
</aop:config>
3. 除了通过配置的方式,还可以用注解的方式
3.1 使用注解将Bean放入IOC容器
使用注解,必须将所在包放入注解扫描配置中,将这句加入配置文件
<context:component-scan base-package=“com.test.entity,com.test.advice”></context:component-scan>
@Component("teacher") //将此类放入IOC容器,id为teacher。
public class Teacher {
@Value("李四") //给name赋值
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Spring提供的几种注解
@Component:当对组件的层次难以定位的时候使用这个注解(最大)
@Controller:表示控制层的组件
@Service:表示业务逻辑层的组件
@Repository:表示数据访问层的组件(Dao层)
**
3.2注解的方式实现AOP
一个普通类加上@Aspect注解就变成了通知类,不需要再继承接口
package com.test.advice;
@Aspect //加上此注解,即变成通知类
public class Annotation {
@Before("execution(public void com.test.service.impl.StudentServiceImpl.addStudent(com.test.entity.Student))")
public void before() {
System.out.println("《注解形式的前置通知》");
}
@After("execution(public void com.test.service.impl.StudentServiceImpl.addStudent(com.test.entity.Student))")
public void after() {
System.out.println("《注解形式的后置通知》");
}
@AfterThrowing("execution(public void com.test.service.impl.StudentServiceImpl.addStudent(com.test.entity.Student))")
public void AfterThrowing() {
System.out.println("《注解形式的异常通知》");
}
@Around("execution(public void com.test.service.impl.StudentServiceImpl.addStudent(com.test.entity.Student))")
public void Around() {
System.out.println("《注解形式的环绕通知》");
}
}
4. Spring的使用
通过Context初始化配置文件,即创建了所有的放在配置文件中的bean的对象
//创建上下文
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//通过上下文调用GetBean方法获取对象,不再需要new
Student student=context.getBean("student") //括号中放入配置文件中bean的id值
System.out.println(student。getStuName());
5. Spring开发Web项目
java项目中,为了使用配置文件中的bean,是通过第4点的方式,创建上下文来获取bean。
web项目中需要在web.xml做配置,让web项目加载时就创建一次,整个项目中直接使用即可。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
6. Spring整合MyBatis
不管什么框架要与Spring整合,都是交给Spring来管理
1.jar
mybatis-spring.jar spring-tx.jar spring-jdbc.jar spring-expression.jar
spring-context-support.jar spring-core.jar spring-context.jar
spring-beans.jar spring-aop.jar spring-web.jar commons-logging.jar
commons-dbcp.jar ojdbc.jar mybatis.jar log4j.jar commons-pool.jar
- MyBatis的核心就是SqlSessionFactory,将它交给Spring来生成
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- MyBatis核心SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--配置mapper文件的位置-->
<property name="mapperLocations" value="com/test/mapper/*.xml"></property>
</bean>
- 核心就是获取mapper对象,有三种方法
a. 通过实现类继承SqlSessionDaoSupport
public class IStudentDaoImpl extends SqlSessionDaoSupport implements StudentMapper {
//SqlSessionDaoSupport这个父类中有一个sqlSession属性
@Override
public Student selectStudentByNo(int tunNo) {
SqlSession sqlSession=super.getSqlSession();
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
return studentMapper.selectStudentByNo(tunNo);
}
}
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="com/test/mapper/*.xml"></property>
</bean>
<bean id="studentMapper" class="com.test.dao.impl.IStudentDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean id="studentService" class="com.test.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"></property>
</bean>
b. 通过使用MapperFactoryBean配置实现(可以不写Dao的实现类)
<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
<property name="mapperInterface" value="com.test.mapper.StudentMapper"></property>
</bean>
<bean id="studentService" class="com.test.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"></property>
</bean>
c. 批量配置
整个包配置成mapper接口,service的bean引用时,id就为它的接口名
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="com.test.mapper"></property>
</bean>
<bean id="studentService" class="com.test.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"></property>
</bean>
d. test类
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService=(StudentService)applicationContext.getBean("studentService");
Student student=studentService.selectStudentByNo(111);
System.out.println(student);
}