Spring
1.IOC
1.Xml方式DI
< context: property-placeholder location = " classpath:jdbc.properties" />
注入必须又getter/setter方法
普通类型直接注入 使用value
< property name = " name" value = " 罗翔" />
引用数据类型使用 ref
< property name = " user" ref = " user" />
引用类 1.外部bean 和 内部bean
引用list < list> </ list> 标签
< list>
< bean class = " com.atguigu.pojo.User" >
< property name = " id" value = " 1" />
< property name = " username" value = " root" />
< property name = " password" value = " 123456" />
</ bean>
< bean class = " com.atguigu.pojo.User" >
< property name = " id" value = " 2" />
< property name = " username" value = " admin" />
< property name = " password" value = " 77777" />
</ bean>
</ list>
引用set < set> </ set> 标签
引用map < map> </ map> 标签
< property name = " map" >
< map>
< entry key = " 小马" value-ref = " user" />
</ map>
</ property>
scope="singleton|prototype" 是否单例
< bean class = " com.atguigu.controller.TeacherController" scope = " prototype" >
< bean id = " user" class = " com.atguigu.pojo.User" >
< property name = " id" value = " 3" />
< property name = " username" value = " root" />
< property name = " password" value = " 123456" />
</ bean>
2.Xml加注解方式DI
不需要getter/ setter方法
@Scope ( "singleton" ) | @Scope ( "prototype" )
@Component
@Controller
@Server
@Repository 区别是类型名不同,作用一样,用来区分那一层
@Autowired 注入,1. 先按照类型进行匹配,2. 按照id,id默认为属性名
@Qualifier ( "xxx" ) 改变id配合 @Autowired 一起使用
@Value ( ) 为普通类型赋值
@PropertySource ( "classpath:jdbc.properties" ) 加载properties文件
< context: component- scan base- package = "com.atguigu" / >
3.配置类+注解方式DI
@Configuration -- > 声明类为配置类
@ComponentScan ( basePackages = { "com.atguigu" } ) -- > 包扫描
@PropertySource ( "classpath:jdbc.properties" ) -- > 加载properties文件
@Bean -- > 生命方法为一个bean
@Bean
public Student getStudentAndCon ( ) {
return new Student ( 1 , "张三" , getUser ( ) , new ArrayList < > ( ) , new HashMap < > ( ) ) ;
}
@Bean
public DruidDataSource getDruidDataSource ( @Value ( "${jdbc.username}" ) String username,
@Value ( "${jdbc.password}" ) String password,
@Value ( "${jdbc.url}" ) String url,
@Value ( "${jdbc.driver}" ) String driver) {
DruidDataSource druidDataSource = new DruidDataSource ( ) ;
druidDataSource. setUsername ( username) ;
druidDataSource. setPassword ( password) ;
druidDataSource. setUrl ( url) ;
druidDataSource. setDriverClassName ( driver) ;
return druidDataSource;
}
2.AOP
1.动态代理
public Object getProxy ( ) {
ClassLoader targetClassLoader = target. getClass ( ) . getClassLoader ( ) ;
Class < ? > [ ] interfaces = target. getClass ( ) . getInterfaces ( ) ;
return Proxy . newProxyInstance ( targetClassLoader, interfaces, new InvocationHandler ( ) {
@Override
public Object invoke ( Object proxy, Method method, Object [ ] args) throws Throwable {
String methodName = method. getName ( ) ;
System . out. println ( methodName+ "开始执行参数为:" + Arrays . toString ( args) ) ;
Object result = method. invoke ( target, args) ;
System . out. println ( methodName+ "执行结束" + result) ;
return result;
}
} ) ;
}
Object o = PRoxy . newProxyInstance ( ) JDK 方式动态代理
参数1. 目标类ClassLoader
参数2. 目标类所有父接口
参数3. 调用处理器 new InvocationHandler ( )
2.面向切面
1. 名词:
通知:增强的方法
切面:方法存放的类
横切关注点:增强方法和被增强方法的交点
切入点:是表达式
切入点表达式:
格式:权限修饰符 全类名 方法名( 参数列表类型)
public int com. atguigu. aop. impl. CalImpl. add ( int , int ) || || * * . *( . . )
int = *
com. atguigu. aop. impl. CalImpl = *
add = *
( . . )
代理:被创建出来执行增加方法的对象
目标:被代理的对象
织入:通知应用到目标上面
使用AOP 动态代理
1. < aop: aspectj- autoproxy/ >
2. 书写代理对象
@Before ( value = "切入点表达式" ) -- > 方法执行前
@After ( value = "切入点表达式" ) -- > 方法执行后
@AfterThrowing ( value = "切入点表达式" , throwing = "exc" ) -- > 遇到异常执行
public void methodException ( JoinPoint joinPoint, Throwable exc)
@AfterReturning ( value = "切入点表达式" , returning = "result" ) -- > 方法执行完毕
public void methodReturn ( JoinPoint joinPoint, Object result)
@Around ( "切入点表达式" ) -- > 环绕通知 以上四个总和
3. 提取切入点表达式
@Pointcut ( "execution(public Integer com.atguigu.aop.impl.CalImpl.div(int,int))" )
public void pointCut ( ) { } ;
本类里面使用:
@Around ( "pointCut()" )
非本类使用:
@Around ( "方法全类名" )
3.配置类方式实现AOP
@EnableAspectJAutoProxy
3.事物
1.依赖
< dependencies>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-context</ artifactId>
< version> 6.0.6</ version>
</ dependency>
< dependency>
< groupId> org.junit.jupiter</ groupId>
< artifactId> junit-jupiter-api</ artifactId>
< version> 5.3.1</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-test</ artifactId>
< version> 6.0.6</ version>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> jakarta.annotation</ groupId>
< artifactId> jakarta.annotation-api</ artifactId>
< version> 2.1.1</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 8.0.25</ version>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> druid</ artifactId>
< version> 1.2.8</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-jdbc</ artifactId>
< version> 6.0.6</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-aop</ artifactId>
< version> 6.0.6</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-aspects</ artifactId>
< version> 6.0.6</ version>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
< version> 1.18.12</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-tx</ artifactId>
< version> 6.0.6</ version>
</ dependency>
</ dependencies>
2.JdbcTemplate
@Override
public void insertUser ( User user) {
jdbcTemplate. update ( "insert into user values (null,?,?)" , user. getUsername ( ) , user. getPassword ( ) ) ;
}
@Override
public void deleteUserById ( Integer id) {
jdbcTemplate. update ( "delete from user where id = ?" , id) ;
}
@Override
public void updateUser ( User user) {
jdbcTemplate. update ( "update user set password = ? where id = ?" , user. getPassword ( ) , user. getId ( ) ) ;
}
@Override
public User findUser ( Integer id) {
User user = jdbcTemplate. queryForObject ( "select * from user where id = ?" , new DataClassRowMapper < > ( User . class ) , id) ;
return user;
}
@Override
public List < User > findAllUser ( ) {
List < User > userList = jdbcTemplate. query ( "select * from user" , new BeanPropertyRowMapper < > ( User . class ) ) ;
return userList;
}
3.Xml+注解方式
< context: property-placeholder location = " classpath:jdbc.properties" />
< context: component-scan base-package = " com.atguigu" />
< bean id = " dataSource" class = " com.alibaba.druid.pool.DruidDataSource" >
< property name = " driverClassName" value = " ${jdbc.className}" />
< property name = " url" value = " ${jdbc.url}" />
< property name = " username" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ bean>
< bean class = " org.springframework.jdbc.core.JdbcTemplate" >
< property name = " dataSource" ref = " dataSource" />
</ bean>
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" />
</ bean>
< tx: annotation-driven />
-------------------------------------------------------
@Transactional()
/**
* 事物的特性:
* 原子性:要执行,一起执行
* 一致性:操作数据库前和操作数据库后要完整
* 隔离性: 多线程存在,一个事物对另一个事物的影响
* 持续性: 事物提交后就永久保存下来
*
* 隔离级别:
* 存在的问题
* 脏读:一个事物读取到另一个并行的未提交的事物
* 不可重复读: 一个事物读取之前操作的事物,发现被另外一个事物做了修改 同一个事物中两次查询结果不一致
* 幻读:在前后两个时间段内执⾏对同⼀个数据项的读取,可能出现不⼀致的结果
* 级别:
* 读未提交(READ_UNCOMMITTED): 以上三个问题,一般不用
* 读已提交(READ_COMMITTED):只能读取到已提交的数据,避免了脏读
* 可重复读(REPEATABLE_READ):两次查询保障结果一致,避免了脏读和不可重复读
* 串行化(SERIALIZABLE):完全禁止了并发,只允许一个事务执行完毕之后才能执行另一个事务
*
*
* isolation 隔离级别
* readOnly 该方法是否只读
* rollbackFor 回滚那个异常 类字节文件
* noRollbackFor 不回滚那个异常 类字节文件
* rollbackForClassName: 回滚那个异常 全类名
* noRollbackForClassName 不回滚那个异常 全类名
* timeout 超时时间
* propagation 事物的传播机制
* Propagation.REQUIRES 当前事物有就用当前事物的,没有就用自己的
* Propagation.REQUIRES_NEW 无论当前事物有没有,自己都新建一个
*/
4…配置类方式
@Configuration
@ComponentScan ( "com.atguigu" )
@PropertySource ( "classpath:jdbc.properties" )
@EnableTransactionManagement
public class SpringConfig {
@Bean
public DruidDataSource getDataSource (
@Value ( "${jdbc.className}" ) String className,
@Value ( "${jdbc.url}" ) String url,
@Value ( "${jdbc.username}" ) String username,
@Value ( "${jdbc.password}" ) String password
) {
DruidDataSource druidDataSource = new DruidDataSource ( ) ;
druidDataSource. setDriverClassName ( className) ;
druidDataSource. setUrl ( url) ;
druidDataSource. setUsername ( username) ;
druidDataSource. setPassword ( password) ;
return druidDataSource;
}
@Bean
public JdbcTemplate getJdbcTemplate ( DruidDataSource druidDataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate ( ) ;
jdbcTemplate. setDataSource ( druidDataSource) ;
return jdbcTemplate;
}
@Bean
public DataSourceTransactionManager getDataSourceTransactionManager ( DruidDataSource druidDataSource) {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager ( ) ;
dataSourceTransactionManager. setDataSource ( druidDataSource) ;
return dataSourceTransactionManager;
}
}