一、概述
概述:spring就是一个轻量级的控制反转和面向切面编程的框架
二、自动装配![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c7c4dbb649bcf006c801fbd1eccfca21.png)
三、AOP
1、XML方式
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.13</version>
</dependency>
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log1 implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
System.out.println("参数为:"+args[0]);
}
}
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class Log2 implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
}
}
public class Log3 {
public void before(){
System.out.println("======before=======");
}
public void after(){
System.out.println("======after=======");
}
}
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- bean definitions here -->
<bean id="userService" class="com.kuangshen.service.UserServiceImpl"/>
<bean id="log1" class="com.kuangshen.log.Log1"/>
<bean id="log2" class="com.kuangshen.log.Log2"/>
<bean id="log3" class="com.kuangshen.log.Log3"/>
<!-- 配置AOP,需要导入aop的约束-->
<aop:config >
<!--切入点 execution表达式:excution(要执行的位置 * * * * *)-->
<aop:pointcut id="pointcut" expression="execution(* com.kuangshen.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加-->
<aop:advisor advice-ref="log1" pointcut-ref="pointcut" />
<aop:advisor advice-ref="log2" pointcut-ref="pointcut" />
</aop:config>
<!--自定义类-->
<aop:config>
<!--自定义切面,ref要引用的类-->
<aop:aspect ref="log3">
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.kuangshen.service.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut" />
</aop:aspect>
</aop:config>
</beans>
public class AopTest {
@Test
public void log1Test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml.");
UserService userService = context.getBean("userService", UserService.class);
userService.add("喜喜");
}
}
2、注解
@Configuration
@ComponentScan(basePackages={"com.kuangshen.pojo","com.kuangshen.aspect"})
@EnableAspectJAutoProxy
public class AppConfig {
@Bean
public User getUser(){
return new User();
}
@Bean
public UserServiceImpl userService(){
return new UserServiceImpl();
}
}
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Aspect
@Component
public class Log {
@Before("execution(* com.kuangshen.service.UserServiceImpl.*(..))")
public void before() throws Throwable {
System.out.println("=====执行前=====");
}
@After("execution(* com.kuangshen.service.UserServiceImpl.*(..))")
public void after() throws Throwable {
System.out.println("=====执行后=====");
}
}
public class AppTest {
@Test
public void testBean(){
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
IUserService userService = context.getBean("userService", IUserService.class);
userService.add("我爱你~");
}
}
四、事务
1.pom文件
pom
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.13</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.13</version>
</dependency>
</dependencies>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases >
<package name="com.kuang.pojo"/>
</typeAliases>
<mappers>
<package name="com.kuang.mapper"/>
</mappers>
</configuration>
spring-dao.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- <!–DataSource:使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid-->
<!-- 我们这里使用Spring提供的JDBC : org.springframework.jdbc.datasource-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis-study?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--sqlSessionFactory 注释-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--绑定Mabatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--SqlSessionTemplate:就是我们使用的sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!--配置声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="dataSource" />
</bean>
<!--结合AOP实现事务的织入-->
<!--配置事务通知;-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--给那些方法配置事务-->
<!--配置事务的传播特性:new propagation= -->
<tx:attributes>
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="delete" propagation="REQUIRED"/>
<tx:method name="update" propagation="REQUIRED"/>
<tx:method name="query" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置事务切入-->
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.kuang.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
</beans>
applicationContext.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">
<import resource="classpath:spring-dao.xml" />
<bean id="userMapper" class="com.kuang.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
<bean id="userService" class="com.kuang.service.UserServiceImpl">
<property name="userMapper" ref="userMapper"/>
</bean>
</beans>
UserMapperImpl
public class UserMapperImpl implements UserMapper{
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> queryList() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.queryList();
}
@Override
public int addUser(User user) {
return sqlSession.getMapper(UserMapper.class).addUser(user);
}
@Override
public int deleteUser(@Param("id") int id) {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.deleteUser(id);
}
}
Test
public class TestUserMapper {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper mapper = context.getBean("userMapper", UserMapper.class);
List<User> users = mapper.queryList();
for (User user:users){
System.out.println(user.toString());
}
}
@Test
public void testUserService(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserSerivce userService = context.getBean("userService", UserSerivce.class);
userService.bus();
}
}