Spring
IOC
通过IOC容器创建对象步骤
(1)创建Maven项目,导入依赖
1 2 3 4 5
| <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.5.RELEASE</version> </dependency>
|
(2)定义类
1 2 3 4
| public class Student { private String name; private int age; }
|
(3)注册bean
1
| <bean id="student" class="com.cskaoyan.Student"/>
|
(4)获取对象
属性赋值
XML
(1)通过Set方法给属性赋值
基本数据类型
1 2 3 4
| <bean id="student" class="com.cskaoyan.Student"> <property name="name" value="xyz"/> <property name="age" value="18"/> </bean>
|
引用数据类型
1 2 3 4 5 6 7 8 9 10
| <bean id="student" class="com.cskaoyan.Student"> <property name="name" value="xyz"/> <property name="age" value="18"/> <property name="address" ref="address"/> </bean>
<bean id="address" class="com.cskaoyan.Address"> <property name="province" value="广东省"/> <property name="city" value="广州市"/> </bean>
|
(2)通过构造方法赋值
定义类
1 2 3 4 5 6 7 8 9 10
| @Data public class Address { private String province; private String city;
public Address(String province, String city){ this.province = province; this.city = city; } }
|
通过构造方法给对象赋值
1 2 3 4
| <bean id="address" class="com.cskaoyan.Address"> <constructor-arg name="province" value="广东省"></constructor-arg> <constructor-arg name="city" value="广州市"></constructor-arg> </bean>
|
自动赋值方式
byName
属性名与bean的id值相同
1 2 3 4 5 6 7 8 9
| <bean id="student" class="com.cskaoyan.Student" autowire="byName"> <property name="name" value="xyz"/> <property name="age" value="18"/> </bean>
<bean id="address" class="com.cskaoyan.Address"> <constructor-arg name="province" value="广东省"></constructor-arg> <constructor-arg name="city" value="广州市"></constructor-arg> </bean>
|
byType
属性类型与bean的类型相同
1 2 3 4 5 6 7 8 9
| <bean id="student" class="com.cskaoyan.Student" autowire="byType"> <property name="name" value="xyz"/> <property name="age" value="18"/> </bean>
<bean id="address" class="com.cskaoyan.Address"> <constructor-arg name="province" value="广东省"></constructor-arg> <constructor-arg name="city" value="广州市"></constructor-arg> </bean>
|
注解
定义类
Student
1 2 3 4 5 6 7 8 9 10
| @Component(value = "student") @Data public class Student { @Value("xyz") private String name; @Value("20") private int age; @Autowired private Address address; }
|
Address
1 2 3 4 5 6 7
| @Component(value = "address") public class Address { @Value("广东省") private String province; @Value("广州市") private String city; }
|
注册扫描器
1 2
| <context:component-scan base-package="com.cskaoyan"/>
|
AOP
AOP的底层实现就是动态代理
AspectJ是一个AOP框架
使用步骤
(1)新建Maven项目,导入依赖
1 2 3 4 5
| <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.2.5.RELEASE</version> </dependency>
|
(2)添加AOP约束文件
1 2 3 4 5 6 7 8 9 10 11 12
| <?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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
|
(3)定义接口及其实现类
UserService
1 2 3
| public interface UserService { User selectUserById(int id); }
|
UserServiceImpl
1 2 3 4 5 6 7 8 9 10
| @Component public class UserServiceImpl implements UserService {
@Override public User selectUserById(int id) { System.out.println("根据用户id查找用户"); return null; } }
|
(4)注册Aspect组件
1 2 3 4 5 6 7 8 9 10 11
| @Aspect @Component public class MyAspect {
@Before("execution(* com.cskaoyan.service.Impl.UserServiceImpl.*(..))") public void doLog(){ System.out.println("日志记录..."); } }
|
(5)设置组件扫描器以及AspectJ自动生成代理
1 2 3 4 5
| <context:component-scan base-package="com.cskaoyan"/>
<aop:aspectj-autoproxy/>
|
AspectJ事务管理
AOP面向切面编程事务管理
配置如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="datasource"/> </bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="transfer" rollback-for="java.lang.Exception"/> </tx:attributes> </tx:advice>
<aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.cskaoyan.service..*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/> </aop:config>
|
Spring事务管理
声明式事务管理
(1)配置事务管理器
1 2 3 4 5 6 7
| <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="datasource"/> </bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
|
(2)在Service层所在事务方法添加注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @Transactional @Override public void transfer(Integer fromId, Integer toId, Integer money) { Integer sourceAccountBalance = accountMapper.selectBalanceById(fromId); Integer targetAccountBalance = accountMapper.selectBalanceById(toId);
sourceAccountBalance -= money; targetAccountBalance += money;
accountMapper.updateAccountBalance(fromId, sourceAccountBalance); System.out.println(1 / 0); accountMapper.updateAccountBalance(toId, targetAccountBalance); }
|
只要发生运行时异常,那么就会自动回滚
Spring整合Mybatis
(1)新建Maven项目,导入依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.15.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency> </dependencies>
|
(2)配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="datasource"/> </bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.cskaoyan.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>
|
(3)Dao层
AccountDao
1 2 3 4 5 6 7
| public interface AccountDao {
Integer selectBalanceById(Integer id);
int updateAccountBalance(@Param("id") Integer id, @Param("money") Integer money);
}
|
(4)Service层
AccountService
1 2 3
| public interface AccountService { void transfer(Integer fromId, Integer toId, Integer money); }
|
AccountServiceImpl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| @Component public class AccountServiceImpl implements AccountService {
@Autowired @Qualifier("accountMapper") AccountMapper accountMapper;
@Override public void transfer(Integer fromId, Integer toId, Integer money) {
Integer sourceAccountBalance = accountMapper.selectBalanceById(fromId); Integer targetAccountBalance = accountMapper.selectBalanceById(toId);
sourceAccountBalance -= money; targetAccountBalance += money;
accountMapper.updateAccountBalance(fromId, sourceAccountBalance); accountMapper.updateAccountBalance(toId, targetAccountBalance); } }
|
(5)测试
1 2 3 4 5 6
| ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) app.getBean("accountServiceImpl");
accountService.transfer(1, 2, 1000);
|