首页
统计
关于
Search
1
Sealos3.0离线部署K8s集群
1,085 阅读
2
类的加载
742 阅读
3
Spring Cloud OAuth2.0
726 阅读
4
SpringBoot自动装配原理
691 阅读
5
集合不安全问题
586 阅读
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Linux
容器
Docker
Kubernetes
Python
FastApi
登录
Search
标签搜索
Java
CSS
mysql
RabbitMQ
JavaScript
Redis
JVM
Mybatis-Plus
Camunda
多线程
CSS3
Python
Spring Cloud
注解和反射
Activiti
工作流
SpringBoot
Mybatis
Spring
html5
蘇阿細
累计撰写
389
篇文章
累计收到
4
条评论
首页
栏目
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Linux
容器
Docker
Kubernetes
Python
FastApi
页面
统计
关于
搜索到
102
篇与
的结果
2020-12-16
代理模式
代理模式代理模式的分类:静态代理动态代理自己 --- > 租房中介 --- > 房东1、静态代理角色分析:抽象角色:一般使用抽象类或接口真实角色:被代理的角色代理角色:代理真实角色,同时可以做一些附属操作客户:访问代理对象的人租房Demo:1、接口package com.sw.demo01; /** * @Author suaxi * @Date 2020/12/16 10:06 */ //租房 public interface Rent { public void rent(); } 2、真实角色package com.sw.demo01; /** * @Author suaxi * @Date 2020/12/16 10:08 */ //房东 public class Host implements Rent{ public void rent() { System.out.println("房东要出租房子"); } } 3、代理角色package com.sw.demo01; /** * @Author suaxi * @Date 2020/12/16 10:10 */ public class Proxy { private Host host; public Proxy() { } public Proxy(Host host) { this.host = host; } public void rent(){ seeHouse(); host.rent(); hetong(); getFee(); } //看房 public void seeHouse(){ System.out.println("中介带租客看房!"); } //签合同 public void hetong(){ System.out.println("签租赁合同"); } //收中介费 public void getFee(){ System.out.println("中介收取中介费用!"); } } 4、客户端访问代理角色package com.sw.demo01; /** * @Author suaxi * @Date 2020/12/16 10:09 */ public class Client { public static void main(String[] args) { //房东想要出租房子 Host host = new Host(); //通过中介帮房东代理发布出租信息,同时中介角色带有一些附属操作 Proxy proxy = new Proxy(host); //客户不用面对房东,直接找中介即可 proxy.rent(); } } 静态代理的优点:可以使真实角色(房东)的操作更加纯粹,即只做租房一件事,不用去关注其他的公共事务公共事务交给代理角色(中介),实现业务分工公共业务需要拓展的时候,方便集中管理缺点:一个真实角色就需要对应的一个代理角色,开发效率低。2、动态代理动态代理与静态代理的角色一样代理类是动态生成的分为两类:基于接口的动态代理,基于类的动态代理基于接口:JDK动态代理基于类:cglibJava字节码实现:javasist动态代理的优点:可以使真实角色的操作更加纯粹,不用去关注其他的公共事务公共事务交给代理角色,实现业务分工公共业务需要拓展的时候,方便集中管理一个动态代理类代理的是一个接口,即对应一类业务一个动态代理可以代理多个类,只要实现同一个接口即可1、接口package com.sw.demo04; /** * @Author suaxi * @Date 2020/12/16 11:00 */ public interface UserService { public void add(); public void delete(); public void find(); public void update(); } 2、真实角色package com.sw.demo04; /** * @Author suaxi * @Date 2020/12/16 11:01 */ public class UserServiceImpl implements UserService{ public void add() { System.out.println("新增了一个用户"); } public void delete() { System.out.println("删除了一个用户"); } public void find() { System.out.println("查询用户"); } public void update() { System.out.println("修改用户信息"); } } 3、ProxyInvocationHandlerpackage com.sw.demo04; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; /** * @Author suaxi * @Date 2020/12/16 10:39 */ public class ProxyInvocationHandler implements InvocationHandler { //被代理的接口 private Object target; public void setTarget(Object target) { this.target = target; } //生成代理对象 public Object getProxy(){ return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this); } //处理代理实例,并返回结果 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { log(method.getName()); Object result = method.invoke(target, args); return result; } public void log(String msg){ System.out.println("执行了"+msg+"方法!"); } } 4、具体实现package com.sw.demo04; /** * @Author suaxi * @Date 2020/12/16 10:57 */ public class Client { public static void main(String[] args) { //真实角色 UserServiceImpl userService = new UserServiceImpl(); //代理角色(不存在) ProxyInvocationHandler pih = new ProxyInvocationHandler(); pih.setTarget(userService); //设置要代理的对象 //动态生成代理类 UserService proxy = (UserService) pih.getProxy(); proxy.add(); } } 代理多个类时,只要实现同一个UserService接口即可UserServiceImpl01 userService = new UserServiceImpl01(); UserServiceImpl02 userService = new UserServiceImpl02(); ……
2020年12月16日
93 阅读
0 评论
0 点赞
2020-12-16
Bean的自动装配
Bean的自动装配==Spring会在上下文中自动寻找,并自动给bean装配属性==三种方式:1、在xml中显示的配置2、在Java中显示配置3、隐式的自动装配bean1、测试环境搭建:一个人有两个宠物Demo2、byName自动装配<bean id="cat" class="com.sw.pojo.Cat"/> <bean id="dog" class="com.sw.pojo.Dog"/> <!--byName:会自动在容器上下文中查找和自己对象中(Person)set方法后面的值对应的beanid--> <bean id="person" class="com.sw.pojo.Person" autowire="byName"> <property name="name" value="孙笑川"/> </bean>3、byType自动装配<bean id="cat2" class="com.sw.pojo.Cat"/> <bean id="dog1" class="com.sw.pojo.Dog"/> <!-- byName:会自动在容器上下文中查找,和自己对象中(Person)set方法后面的值对应的beanid byType:会自动在容器上下文中查找,和自己对象属性类型相对应的bean --> <bean id="person" class="com.sw.pojo.Person" autowire="byType"> <property name="name" value="孙笑川"/> </bean>注:byName:需要保证所有bean的id唯一,且这个bean需要和自动注入的属性的set方法的值一致byType:需要保证所有bean的class唯一,且这个bean需要和自动注入的属性的类型一致4、使用注解实现自动装配1、导入context约束2、开启注解<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> </beans>@Autowired 直接在属性上用即可,也可以在set方法上使用@Nullable //字段标记了这个注解,说明这个字段可以为null@Autowired(required = false) //如果显示的定义Autowired的required属性为fasle,说明这个对象可以为空//默认为true 不允许为空 public @interface Autowired { boolean required() default true; }注:如果@Autowired自动装配的环境比较复杂,无法通过一个@Autowired注解完成的时候,可以使用@Qualifier(value = "xxx"),指定一个唯一的bean对象public class Person { private String name; @Autowired private Dog dog; @Autowired @Qualifier(value = "cat") private Cat cat; }@Resourcepublic class Person { private String name; @Resource(name = "cat1212") private Dog dog; @Resource private Cat cat; }小结:@Autowired与@Resource的区别都是用来自动装配的@Autowired 通过byType的方式实现,且对象必须存在@Resource 默认通过byName的方式实现,如果找不到对应的bean id,则通过byType实现
2020年12月16日
75 阅读
0 评论
0 点赞
2020-12-15
依赖注入
依赖注入1、构造器注入参考IoC创建对象的方式2、Set方式注入依赖注入:Set注入依赖:bean对象的创建依赖于容器注入:bean对象中的所有属性,由容器来注入环境搭建:1、复杂类型package com.sw.pojo; /** * @Author suaxi * @Date 2020/12/14 16:32 */ public class Adress { private String adress; public String getAdress() { return adress; } public void setAdress(String adress) { this.adress = adress; } } 2、真实测试对象package com.sw.pojo; import java.util.*; /** * @Author suaxi * @Date 2020/12/14 16:32 */ public class Student { private String name; private Adress adress; private String[] books; private List<String> hobbys; private Map<String,String> card; private Set<String> games; private String wife; private Properties info; …… } 3、beans.xml<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student" class="com.sw.pojo.Student"> <!--普通值注入 value--> <property name="name" value="孙笑川"/> </bean> </beans>4、测试类import com.sw.pojo.Student; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @Author suaxi * @Date 2020/12/14 16:38 */ public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student student = (Student) context.getBean("student"); System.out.println(student.getName()); } } beans.xml补充:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="com.sw.pojo.Address"> <property name="address" value="昆明"/> </bean> <bean id="student" class="com.sw.pojo.Student"> <!--普通值注入 value--> <property name="name" value="孙笑川"/> <!--bean注入 ref--> <property name="address" ref="address"/> <!--数组--> <property name="books"> <array> <value>高数一</value> <value>英语一</value> <value>毛概</value> </array> </property> <!--list--> <property name="hobbys"> <list> <value>跑步</value> <value>游泳</value> <value>听音乐</value> </list> </property> <!--map--> <property name="card"> <map> <entry key="身份证" value="12345678"/> <entry key="银行卡" value="87654321"/> </map> </property> <!--set--> <property name="games"> <set> <value>LOL</value> <value>FF14</value> <value>CSGO</value> </set> </property> <!--null--> <property name="wife"> <null/> </property> <!--properties--> <property name="info"> <props> <prop key="姓名">孙笑川</prop> <prop key="学号">123</prop> <prop key="性别">男</prop> </props> </property> </bean> </beans>3、拓展方式注入可以使用p命名空间和c命名空间<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!--p命名空间注入,可以直接注入属性的值:property--> <bean id="user" class="com.sw.pojo.User" p:id="1" p:name="孙笑川"/> <!--c命名空间注入,通过构造器注入constructor-args--> <bean id="user1" class="com.sw.pojo.User" c:id="2" c:name="刘波"/> </beans>注:使用前需导入xml约束xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"4、bean的作用域1、单例模式(Spring默认)<bean id="user1" class="com.sw.pojo.User" c:id="2" c:name="刘波" scope="singleton"/>@Test public void test1(){ ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml"); User user1 = (User) context.getBean("user"); User user2 = (User) context.getBean("user"); System.out.println(user1==user2); //true }2、原型模式:每次从容器中get的时候都会产生一个新对象<bean id="user1" class="com.sw.pojo.User" c:id="2" c:name="刘波" scope="prototype"/>@Test public void test1(){ ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml"); User user1 = (User) context.getBean("user1"); User user2 = (User) context.getBean("user1"); System.out.println(user1.hashCode()); //838411509 System.out.println(user2.hashCode()); //1434041222 System.out.println(user1==user2); //false }3、其余的request、session、application在web开发中使用
2020年12月15日
82 阅读
0 评论
0 点赞
2020-12-14
IOC创建对象的方式
IOC创建对象的方式:1、使用无参构造创建对象(默认)<bean id="user" class="com.sw.pojo.User"> <property name="name" value="孙笑川"/> </bean>2、使用有参构造创建对象下标赋值<bean id="user" class="com.sw.pojo.User"> <constructor-arg index="0" value="孙笑川"/> </bean>类型<bean id="user" class="com.sw.pojo.User"> <constructor-arg type="java.lang.String" value="孙笑川"/> </bean>参数名<bean id="user" class="com.sw.pojo.User"> <constructor-arg name="name" value="孙笑川"/> </bean>==在配置文件加载的时候,容器中管理的对象就已经初始化了。==
2020年12月14日
61 阅读
0 评论
0 点赞
2020-12-14
Spring配置(applicationContext.xml)
Spring配置1、别名<!--别名--> <alias name="user" alias="newUser"/>2、Bean配置<!-- id:bean的唯一标识符(相当于对象名) class:bean对象所对应的全限定类名,格式:包名 + 类型 name:别名,可以同时取多个(alias只能一对一) --> <bean id="user2" class="com.sw.pojo.User2" name="a,b c;d"> <property name="name" value="test"/> </bean>3、import可以将多个配置文件导入合并为一个例:一个项目由多个人负责开发,每个人负责的类不相同,且不同的类需要注册在不同的bean中,则可以利用import将所有人的beans.xml合并为一个。<import resource="beans1.xml"/> <import resource="beans2.xml"/> <import resource="beans3.xml"/>
2020年12月14日
62 阅读
0 评论
0 点赞
2020-12-14
IOC理论推导
IOC本质:控制反转IoC(Inversion of Contorl)是一种设计思想,ID(依赖注入)是实现IoC的一种方法,在没有IoC的程序中,我们使用面向对象编程,对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方。采用XML方式配置Bean的时候,Bean的定义信息是和实现分离的,而采用注解的方式可以把两者合为一体,Bean的定义信息直接以注解的形式定义在实现类中,从而达到零配置的目的。控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式,在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入(Dependency Injection,DI)
2020年12月14日
94 阅读
0 评论
0 点赞
2020-12-14
动态SQL
动态SQL==根据不同的条件生成不同的SQL语句==ifchoose (when, otherwise)trim (where, set)foreach搭建环境CREATE TABLE `blog`( `id` VARCHAR(50) NOT NULL COMMENT 'id', `title` VARCHAR(100) NOT NULL COMMENT '标题', `author` VARCHAR(50) NOT NULL COMMENT '作者', `create_time` datetime NOT NULL COMMENT '创建时间', `views` int(11) NOT NULL COMMENT '浏览量' )ENGINE=INNODB DEFAULT CHARSET=utf8;1、导包2、编写配置文件3、创建实体类package com.sw.pojo; import lombok.Data; import java.io.Serializable; import java.util.Date; /** * @Author suaxi * @Date 2020/12/13 16:20 */ @Data public class blog implements Serializable { private String id; private String title; private String author; private Date createTime; private int views; } 4、编写实体类对应的Mapper接口和Mapper.xml文件IF<select id="findBlogIf" parameterType="map" resultType="Blog"> select * from blog where 1=1 <if test="title != null"> and title = #{title} </if> <if test="author != null"> and author = #{author} </if> </select>chose(when,otherwise)<select id="findBlogChoose" parameterType="map" resultType="Blog"> select * from blog <where> <choose> <when test="title != null"> title = #{title} </when> <when test="author != null"> and author = #{author} </when> <otherwise> and views = #{views} </otherwise> </choose> </where> </select>trim(where,set) <select id="findBlogIf" parameterType="map" resultType="Blog"> select * from blog <where> <if test="title != null"> title = #{title} </if> <if test="author != null"> and author = #{author} </if> </where> </select><update id="UpdateBlog" parameterType="map"> update blog <set> <if test="title != null"> title = #{title}, </if> <if test="author != null"> author = #{author} </if> </set> where id = #{id} </update>SQL片段将部分SQl抽取出来,方便复用1、使用SQL标签抽取公共部分<sql id="if-title-author"> <if test="title != null"> title = #{title} </if> <if test="author != null"> and author = #{author} </if> </sql>2、在需要使用的地方用include标签引用、<select id="findBlogIf" parameterType="map" resultType="Blog"> select * from blog <where> <include refid="if-title-author"></include> </where> </select>注:最好基于单表查询,不要存在where标签Foreach<!--select * from bolg where 1=1 and (id=1 or id=2 or id=3)--> <select id="findBlogForeach" parameterType="map" resultType="Blog"> select * from blog <where> <foreach collection="ids" item="id" open="and (" close=")" separator="or"> id = #{id} </foreach> </where> </select>==动态SQL就是在拼接SQL语句,先确保SQL的正确性,再按照SQL格式去排列组合==先在MySQL中写出完整的sql,再去修改成动态sql实现通用
2020年12月14日
101 阅读
0 评论
0 点赞
2020-12-13
一对多
一对多处理一个老师拥有多个学生(对老师而言,就是一对多的关系)1、新建实体类Teacherpackage com.sw.pojo; import lombok.Data; import java.io.Serializable; import java.util.List; /** * @Author suaxi * @Date 2020/12/13 10:55 */ @Data public class Teacher implements Serializable { private int id; private String name; //一个老师对应多个学生 private List<Student> students; } Studentpackage com.sw.pojo; import lombok.Data; import java.io.Serializable; /** * @Author suaxi * @Date 2020/12/13 10:55 */ @Data public class Student implements Serializable { private int id; private String name; private int tid; } 2、TeacherMapper.xml配置(两种方式)<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.sw.dao.TeacherMapper"> <!--按结果集嵌套处理--> <select id="findTeacherById" resultMap="TeacherStudent"> select s.id sid,s.name sname,t.id tid ,t.name tname from student s,teacher t where s.tid = t.id and t.id = #{tid}; </select> <resultMap id="TeacherStudent" type="teacher"> <result property="id" column="tid"/> <result property="name" column="tname"/> <!-- 集合使用 collection javaType="" 指定属性的类型 集合中的泛型信息,使用 ofType 获取 --> <collection property="students" ofType="Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <result property="tid" column="tid"/> </collection> </resultMap> <!--按照查询嵌套处理--> <select id="findTeacherById2" resultMap="TeacherStudent2"> select * from teacher where id = #{tid}; </select> <resultMap id="TeacherStudent2" type="Teacher"> <collection property="students" javaType="ArrayList" ofType="Student" select="findStudentByTeacherId" column="id"/> </resultMap> <select id="findStudentByTeacherId" resultType="Student"> select * from student where tid = #{tid}; </select> </mapper>注:1、关联 - association 【多对一】2、集合 - collection 【一对多】3、javaType & ofTypejavaType用来指定实体类中属性的类型ofType用来指定映射到List或者集合中的pojo类型,泛型中的约束类型
2020年12月13日
136 阅读
0 评论
0 点赞
1
...
3
4
5
...
13