1. Spring IOC(控制反转):
作用:通过引入IOC容器,利用依赖关系注入的方式,实现对象之间的解耦
实现原理:注解(标识)、反射、工厂
2. Spring AOP(面向切面):
作用:在不修改源代码的情况下,可以实现功能的增强
AOP思想:基于代理思想,对原来目标对象,创建代理对象,在不修改原对象代码情况下,通过代理对象,调用增强功能的代码,从而对原有业务方法进行增强
实现原理:AOP 框架负责动态地生成 AOP 代理类,这个代理类的方法则由 Advice 和回调目标对象的方法所组成。
实现技术:jdk动态代理、CGLIB动态代理
核心概念:
切面(Aspect):是一个类,里面定义了通知与切点
切点(PointCut):表达式。就是告诉程序要在执行哪些核心业务的时候,执行非核心的业务
@Pointcut("execution(* com.suning.retailcloud.iss.dao.*.*(..))")
通知(advice):五种通知方式:
@Before
:前置通知,在调用目标方法之前执行通知定义的任务
@After
:后置通知,在目标方法执行结束后,无论执行结果如何都执行通知定义的任务
@After-returning
:后置通知,在目标方法执行结束后,如果执行成功,则执行通知定义的任务
@After-throwing
:异常通知,如果目标方法执行过程中抛出异常,则执行通知定义的任务
@Around
:环绕通知,在目标方法执行前和执行后,都需要执行通知定义的任务。
普通顺序:Around --> Before --> (业务) --> Around --> After --> After-returning
异常顺序:Around --> Before --> (业务) --> After --> After-throwing
3. Spring多个配置加载:
web.xml中增加如下配置,多个配置文件用逗号分隔:
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:conf/spring/applicationContext_core*.xml, classpath*:conf/spring/applicationContext_dict*.xml, classpath*:conf/spring/applicationContext_hibernate.xml, </param-value> </context-param>
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
4 SpringCloud项目开发:
4.1 请求URL根路径配置:server.servlet.context-path
4.2 服务端口号配置:server.port
4.3 配置文件内容加载:
@Component
@ConfigurationProperties(prefix = "server")
@PropertySource(value = "classpath:appconfig.yml")
public class AppConfig{
@Value("${server.port}")
private String serverPort;
}
4.5 单元测试类加上注解:
@SpringBootTest(classes = InvCenterApplication.class)
@RunWith(SpringRunner.class)
4.6 openfeign接口定义:
4.6.1 服务端定义:
@RequestMapping(value = "/test/sayHello/{name}", produces = "application/json", consumes = "application/json", method = RequestMethod.POST)
public TestRespDTO sayHello(@PathVariable String name, @RequestBody TestReqDTO testReqDTO, @RequestParam String school)
4.6.2 客户端定义:
@RequestMapping(value = "/inv/test/sayHello/{name}", method = RequestMethod.POST)
public @ResponseBody TestRespDTO sayHello(@PathVariable String name, @RequestBody TestReqDTO reqDTO, @RequestParam String school);
注意:接口参数只能有一个@RequestBody
4.7 openfeign抽象接口定义:
抽象接口:
@FeignClient("fushang-stock-center2")
public interface ITestController {
@RequestMapping(value = "/inv/test/sayHello/{name}/{age}", produces = "application/json", consumes = "application/json", method = RequestMethod.POST)
public TestRespDTO sayHello(@PathVariable String name, @RequestBody TestReqDTO testReqDTO, @PathVariable String age, @RequestParam String school);
}
实现接口:
@RestController
public class TestController implements ITestController {
@Override
public TestRespDTO sayHello(@PathVariable String name, @RequestBody TestReqDTO testReqDTO, @PathVariable String age, @RequestParam String school){
...
}
}
消费端:
启动类上加上注解,并标注扫描服务端接口包路径
@EnableFeignClients(basePackages = "com.suning.retailcloud.fmcg.invcenter.atest")
5. spring cloud stream
目前支持rabbitmq和kafka
5.1 生产者
5.1.1 定义Stream接口
public interface MyStream{
// 消息输入出
@Output("outputQueue")
MessageChannel output();
@Input("inputQueue")
SubscribableChannel input();
}
5.1.2 启动类上增加绑定注解
@EnableBinding(MyStream.class)
5.1.3 rabbitmq配置:
rabbitmq-server:
ip: 192.22.51.27
port: 5672
username: xxx
password: xxxxx
5.1.4 消息推送
@Autowire
private MyStream myStream;
private String msg = "";// 被发送的消息
myStream.output().send(MessageBuilder.withPayload(msg).build());
5.2 消费者
5.2.1 定义Stream接口
public interface MyStreamInput{
@Input("wuanMqQueue")
SubscribableChannel wuanMqQueue();
}
5.2.2 启动类上加绑定注解
@EnableBinding(MyStreamInput.class)
5.2.3 配置rabbitmq
rabbitmq-server:
ip: 192.22.51.27
port: 5672
username: xxx
password: xxxxx
5.2.4 消息接收类
@Component
public class MsgReciver{
@Autowire
private MyStreamInput myStreamInput;
//注解方式监听
@org.springframework.cloud.stream.annotation.StreamListener("wuanMqQueue")
public void onMessage(String data){
System.out.println("==>" + data);
}
//使用接口编程的方式监听
@PostConstruct
public void init() {
SubscribableChannel channel = myStreamListener.wuanMqQueue();
channel.subscribe(message -> {
System.err.println("接收到:" + new String((byte[]) message.getPayload()));
});
}
}
6. Spring的@EnableXXX工作原理
通过简单的@Enable*来开启一项功能的支持,从而避免自己配置大量的代码,大大降低了使用难度。
@Import 用来导入动态注册的Bean,这些Bean提供完成某些功能的接口
@ConditionalOnClass 当前classpath下存在类时有效
@ConditionalOnMissingBean(Xxx.class) 容器中不存在Xxx.class实例时生效
强大的@EnableAutoConfigure注解:可以依据classpath里的依赖内容自动配置Bean到IOC容器,Auto-Configuration会尝试推断哪些bean可能是用户需要的(根据@ConditionOnClass和@ConditionOnMissingBean推断)。
https://www.cnblogs.com/zgwjava/p/10892810.html