Spring boot 笔记!很则乱(自己的笔记)

logger log=logger.getLogger(要打印的类.class);//来自log4j用于在打印台输出信息

ModelAndView 对象// 模板化操作用于重定向。一般直接定义到网页
RedirectView  对象  //模板化操作用于请求转发 ,服务器内部操作,将一个指令交给另一个请求的url来反馈

@JsonIgnore/忽略/在传输json对象的时候,以免发生连表循环查询,导致内存溢出!可在bean中加上此注解
@RestController 注解在类上,相当于将所有方法都注解@ResponseBody

拦截器要小于下面的拦截地址
 
  dispatcherServlet
  *.job

异常拦截
在controller中不知道哪个  @RequestMapping("?")可能会抛出异常/为了将异常封装管理起来
实现类HandlerExceptionResolver
实现方法resolveException(请求,响应,对象,异常)
设置好之后我们需要将异常类对象放入到Spring中    
HandlerMethod hm=(HandlerMethod )handler 
(HandlerMethod )导包import org.springframework.web.method.HandlerMethod;
如果报如下这样的错,说明包导错了
Request processing failed; nested exception is java.lang.ClassCastException: org.springframework.web.method.HandlerMethod cannot be cast to org.springframework.messaging.handler.HandlerMethod
hm.getMethod().getName获取异常出现的是哪个方法
异常.printStackTrace()在控制台上输出异常

代码区
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Exception implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, java.lang.Exception ex) {
        HandlerMethod hm=(HandlerMethod )handler;
       String me= hm.getMethod().getName();
        System.out.println(me);
        ex.printStackTrace();
        ModelAndView mv=new ModelAndView("error.jsp");
        mv.addObject("ee","我出错了");
        return mv;
    }
}

========================================================
restfull风格 
//大括号可以传参至下面的@PathVariable 所定义的类型中/根据类型来返回参数
 @RequestMapping("json/id/{tag}/str/{str}.job")
    @ResponseBody
    public String getJson(@PathVariable int tag,@PathVariable String str){
        if (tag==0){
            return "输入的值为0"+str;
        }else if(tag==1){
            return "输入的值为1"+str;
        }else{
            return "输入的值为2"+str;
        }
    }
//在访问时我们可以直接将值放入到地址中就能得到想要的json数据
http://localhost:8080/war/json/id/2/str/ppp.job
new一个 RestTemplate返回交给@Bean  spring 来管理 
Spring Boot就可以注入RestTemplate 来.getForEntity("url", 条件,类型(类)).getBody();
==================================================================
Spring Boot 
是用来简化spring的,简化了xml文件(没有了),虽然取消了xml但需要在类中配置。集成了tomcat。
预装了类(在java中已经有的类被spring统一管理起来了将对象放到了内存中)

SPring Boot 可搭建微服务器

SPring主启动需要注解@SpringBootApplication
创建main方法{
 //启动springboot
        SpringApplication.run(BootMain.class);
}
此主方法需要放在根目录中
再bean类中/自动创建表的规则是驼峰命名的峰处以下划线表示。
因为数据库不分大小写例如bean中userName/数据库user_name


测试类中需要注解
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BootMain.class)//classes = BootMain.class是主boot启动类
注解后可直在test中测试service层;
前面说到配置需要在类中,那么我们需要创建配置类/该类实现了接口WebMvcConfigurer并在该方法上加上注解@Component详情桌面

《---------------------------代码区------------------------------》
当然也可以不实现该接口,可以在别的类中书写配置方法在存在方法的类上注解@Configuration 那么这个类也就是一个配置类了
如何让定义一个bean交给spring 管理 在@Configuration 管理类下直接书写方法返回值为该bean 再设置bean的属性 return 这个bean
并且再该方法上注解@Bean 表示他是一个类交给spring处理

关于leaf模板/类似jsp ,thymeleaf模板技术默认存放在templetes文件下,在网页上声明
所有文本数据

 


补充重定向RedirectView重定向在leaf模板
ModelAndView (跳转至网页名);
返回ModelAndView 
RedirectView (跳转至另一个@RequestMapping方法中 )
modelAndView .setView(redirectView )
再返回 modelAndView 
 ModelAndView mv=new ModelAndView();
        userProService.del(Integer.parseInt(id));
        RedirectView rv=new RedirectView("findAll");
        mv.setView(rv);

leaf模板下 html中的字符串点击事件!遇到的坑/如下正确写法(传输字符串)

//上传maven导包,上传配置文件类,上传application.properties/属性 ,具体配置看需求

----------------------------------------------------异常统一捕获----------------------------------------------------------

异常管理需要实现HandlerExceptionResolver/完成异常的统一捕获需要在实现类上注解@Component


跨域 只存在html     只要后台遵循cors就能实现跨域 jsonp很少被使用

----------------------------------------------------------分布式开发-------------------------------------------------------

cloud 注解spring boot启动项 为客户端@EnableEurekaServer注解服务器 @EnableEurekaClient
配置注册客户端到服务器中@EnableEurekaClient-->@EnableEurekaServer

使用restTemplate访问restful接口非常的简单粗暴无脑。(url、requestMap、ResponseBean.class)这三个参数分别代表 请求地址、请求参数、HTTP响应转换被转换成的对象类型。

配置服务器:
用于统一管理服务器的配置文件,在服务器启动前首先需要加载配置文件,通过服务器名为前缀,服务器定义一个后缀生成,去配置服务器寻找和生成名相同的配置,完成设置
断熔:时备用方法返回类型,要和原方法一样,短融需要导包Hystrix,Hystrix-dashboard 两个包

-----------------------------------------------------------队列-----------------------------------------------------------

队列:可用于生产者和消费者 LinkedList实现,生产者-->缓冲区LinkedList缓冲区-->消费者
要想实现阻塞功能,需要调用put(e) take() 方法,当不满足约束条件时,会阻塞线程
ArrayBlockingQueue(可容纳数量int) //创建组赛队列
ConcurrentLinkedQueue //非阻塞队列 
上面两种都是线程安全的,。

----------------------------------------------------MQ tcpip------------------------------------------------------------
MQ 是一种应用程序与应用程序的一种通讯方法  MQ消息队列(异步操作)不针对任何语言 都可已使用| 
MQ 可以将多个服务器中的信息都交给MQ /需要使用的其他端口可以在MQ中去取处理结果
JMS 是javaee平台创建的
ActiveMQ apache出品 , ActiveMQ 支持JMS1.1 和J2EE1.4

MQ和tomcat差不多都为中间件

为MQ配置xml文件

spring.activemq.broker-url=tcp://127.0.0.1:61616
#activeMQ用户名,根据实际情况配置
spring.activemq.user=j171
#activeMQ密码,根据实际情况配置
spring.activemq.password=j171
#是否启用内存模式(也就是不安装MQ,项目启动时同时也启动一个MQ实例)
spring.activemq.in-memory=false
#信任所有的包
spring.activemq.packages.trust-all=true
#如果是点对点(queue),那么此处默认应该是false,如果发布订阅,那么一定设置为true
spring.jms.pub-sub-domain=true

点对点// 指定发送消息

ActiveMQQueue mqQueue; //注入MQ /点对点
private JmsMessagingTemplate jmsMessagingTemplate;//注入模板

//new ActiveMQQueue("j171queue")为一对一发送消息,申明名字。方便

@JmsListener(destination = "j171queue")//@JmsListener注解按照名字进行拦截j171queue
jmsMessagingTemplate.convertAndSend(new ActiveMQQueue("j171queue"),"hello mq");//通过模板发送消息

和上面操作一样

订阅发布// 批量发送消息
端口号设置在jetty.xml
程序连接MQ activemq.xml 来设置端口号以及账号和密码

json对象格式:"{'键值':值}"

//网页上
var c=eval("("+date+")") ; //把josn字符串转换为json对象
-----------------------------------------------用于高并发/连接池--------------------------------------------------------------------
连接池!面向于高并发!大数据
1.初始化连接
2.当连接空间不足时,配置增量
3.连接数的最大量(不可能无限增加)
4.空闲时间()
5.最小连接数

配置连接池xml文件
#最大等待连接中的数量,设 0 为没有限制
spring.datasource.max-idle=10 
#最大连接数据库连接数,设 0 为没有限制
spring.datasource.max-active=15
#用来设置一个connection在连接池中的存活时间,默认是1800000,即30分钟。如果设置为0,表示存活时间无限大。如果不等于0且小于30秒则会被重置回30分钟
spring.datasource.max-lifetime=86430000
#使用DBCP connection pool,是否追踪废弃statement或连接,默认为: false
spring.datasource.log-abandoned=true
#指定当连接超过废弃超时时间时,是否立刻删除该连接
spring.datasource.remove-abandoned=true
#指定连接应该被废弃的时间(秒)
spring.datasource.remove-abandoned-timeout=60
spring.datasource.sqlScriptEncoding=UTF-8

你可能感兴趣的:(Spring,Spring)