Spring
https://share.mubu.com/doc/4j0FPADSJ7C
一 : 整合数据源
(一) 添加依赖
(二) 编写配置文件
二 : 整合DBUtils
(一) 添加依赖
(二) 编写实体类
(三) 编写dao
(四) 编写service
(五) 编写配置文件
(六) 测试
三 : 基于注解实现IOC
(一) 常用注解
-
对象实例化
@Component : 表示该类要被spring所管理
延伸注解 :
@Service
@Controller
@Repository -
属性注入的注解
@AutoWired : 按照类型进行注入, 而且不需要set方法
@Qualifier(“girlDao”) : 配合@Autowired, 按照名字进行注入
@Resource : 直接按照名字进行注入
@Value : 注入普通属性 -
作用范围
@Scope
(二) 基于注解的IOC配置
- 标记组件
- 开启组件扫描
- 为组件注入属性
- 测试
(三) 纯注解开发 - 配置类
package com.ujiuye.demo.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
@ComponentScan("com.ujiuye.demo")
public class IOCConfig {
@Bean
public DataSource dataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
druidDataSource.setUrl("jdbc:mysql:///offcn?serverTimezone=UTC");
druidDataSource.setUsername("root");
druidDataSource.setPassword("");
return druidDataSource;
}
@Bean
public QueryRunner queryRunner(){
return new QueryRunner(dataSource());
}
}
- 测试
@Test
public void test(){
ApplicationContext applicationContext=new AnnotationConfigApplicationContext(IOCConfig.class);
GirlService girlService = (GirlService) applicationContext.getBean("girlService");
List<Girl> allGirl = girlService.findAllGirl();
System.out.println(allGirl);
}
四 : spring-test
(一) 添加依赖
(二) 在测试类上添加测试注解
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = IOCConfig.class)
(三) 自动注入
@Autowired
private GirlService girlService;
(四) 测试
package com.ujiuye.test;
import com.ujiuye.demo.config.IOCConfig;
import com.ujiuye.demo.pojo.Girl;
import com.ujiuye.demo.service.GirlService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = IOCConfig.class)
public class GirlTest1 {
@Autowired
private GirlService girlService;
@Test
public void test(){
List<Girl> allGirl = girlService.findAllGirl();
System.out.println(allGirl);
}
}
五 : AOP
(一) 概述
AOP: aspect oriented program, 面向切面编程
OOP: object oriented program, 面向对象编程
AOP通过提供了另一种编程结构的思考方式来完善OOP, OOP中的核心单元是类, 而AOP中的核心单元是切面, 切面可以将内容模块化, 例如事务管理
OOP无法对代码块中的业务进行增强, 所以我们要引入AOP, 只是为我们OOP进行补充
AOP的核心思想就是代理模式
Spring-aop中采用的是动态代理, jdk动态代理proxy, cglib动态代理enhancer
(二) jdk动态代理
查询api
代码实现
package com.ujiuye.test;
import com.ujiuye.demo.service.KillerService;
import com.ujiuye.demo.service.impl.KillerServiceImpl;
import org.junit.Test;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class KillerTest {
@Test
public void test(){
KillerService killerService=new KillerServiceImpl();
//获取目标对象的类加载器
ClassLoader classLoader = killerService.getClass().getClassLoader();
//获取目标对象的接口
Class<?>[] interfaces = killerService.getClass().getInterfaces();
//使用JDK动态代理类proxy定义增强规则,返回代理对象
KillerService proxy = (KillerService) Proxy.newProxyInstance(classLoader, interfaces, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("开始...");
KillerService invoke = (KillerService) method.invoke(killerService, args);
System.out.println("结束...");
return null;
}
});
proxy.kill();
}
}
(三) Cglib动态代理
@Test
public void test1(){
KillerService killerService=new KillerServiceImpl();
//构建代理对象的构建对象
Enhancer enhancer=new Enhancer();
//传递一个超类
enhancer.setSuperclass(killerService.getClass());
//设置回调
enhancer.setCallback(new net.sf.cglib.proxy.InvocationHandler() {
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
System.out.println("开始...");
KillerService invoke = (KillerService) method.invoke(killerService, objects);
System.out.println("结束...");
return invoke;
}
});
//创建代理对象
KillerService proxy = (KillerService) enhancer.create();
proxy.kill();
}
(四) Jdk动态代理和cglib的区别
- Jdk动态代理一定要有接口才能实现
- Cglib动态代理有接口能实现, 没有接口也能实现
- Jdk动态代理, 代理类和目标类是兄弟关系
- Cglib动态代理, 代理类和目标类是父子关系
(五) Jdk动态代理工厂和cglib动态代理工厂
Jdk动态代理工厂
package com.ujiuye.demo.factory;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class JDKProxy implements InvocationHandler {
private Object target;
public JDKProxy(Object target) {
this.target = target;
}
public Object creatProxy(){
return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("开始....");
Object invoke = method.invoke(target, args);
System.out.println("结束...");
return invoke;
}
}
cglib动态代理工厂
package com.ujiuye.demo.factory;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;
import java.lang.reflect.Method;
public class CglibProxy implements InvocationHandler {
private Object target;
public CglibProxy(Object target) {
this.target = target;
}
public Object creatProxy(){
Enhancer enhancer=new Enhancer();
enhancer.setSuperclass(target.getClass());
enhancer.setCallback(this);
return enhancer.create();
}
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
System.out.println("开始...");
Object invoke = method.invoke(target, objects);
System.out.println("结束...");
return invoke;
}
}