基础jar(5+1)
spring-beans.jar
spring-core.jar
spring-context.jar
spring-aop.jar
spring-expression.jar
日志
commons-logging.jar
2.1. applicationContext头文件
基础头文件 bean context
开发事务 tx
开发AOP加上 aop
2.2.1. 通过property的方式
对象类必须有set方法
package com.test.entity;
public class Student {
private String stuName;
public void setStuName(String stuName) {
this.stuName = stuName;
}
}
配置文件中加入
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;
}
}
配置文件
2.2.3. p命名空间赋值
使用p命名空间必须在头文件加入p标签
xmlns:p=“http://www.springframework.org/schema/p”
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注入到Service实现类的dao属性中
**
开启一个名叫txManager的事务
配置事务
事务需要一个数据源DataSource,再配置下DataSource
将普通类变成通知类,需要实现接口即可
前置通知->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容器中
将具体要加通知的方法所在的类放入IOC容器中
配置AOP(将通知和方法连接起来)
使用注解,必须将所在包放入注解扫描配置中,将这句加入配置文件
@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层)
**
一个普通类加上@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("《注解形式的环绕通知》");
}
}
通过Context初始化配置文件,即创建了所有的放在配置文件中的bean的对象
//创建上下文
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//通过上下文调用GetBean方法获取对象,不再需要new
Student student=context.getBean("student") //括号中放入配置文件中bean的id值
System.out.println(student。getStuName());
java项目中,为了使用配置文件中的bean,是通过第4点的方式,创建上下文来获取bean。
web项目中需要在web.xml做配置,让web项目加载时就创建一次,整个项目中直接使用即可。
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
不管什么框架要与Spring整合,都是交给Spring来管理
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
<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>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="mapperLocations" value="com/test/mapper/*.xml">property>
bean>
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);
}