随笔分类 -  Spring注解驱动开发

摘要:注解解释 @Controller 组合注解(组合了@Component注解),应用在MVC层(控制层),DispatcherServlet会自动扫描注解了此注解的类,然后将web请求映射到注解了@RequestMapping的方法上。 @Service 组合注解(组合了@Component注解),应 阅读全文
posted @ 2022-03-13 22:05 KwFruit 阅读(101) 评论(0) 推荐(0)
摘要:一、 利用@ConfigurationProperties 读取配置 文件 1、Yml自定义配置信息 esc: ip: 192.168.23.1 port: 9898 2、信息读取配置类 package com.mangoubiubiu.conf; import lombok.Data; impor 阅读全文
posted @ 2022-02-27 16:55 KwFruit 阅读(90) 评论(0) 推荐(0)
摘要:1 日志打印用来监控程序的执行很重要 手动在Controller 里面打印日志很繁琐。 package com.mangoubiubiu.controller; import com.alibaba.fastjson.JSONObject; import com.mangoubiubiu.utils 阅读全文
posted @ 2021-12-10 22:23 KwFruit 阅读(250) 评论(0) 推荐(0)
摘要:一丶 基本概念 AOP: [动态代理] 指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式。 1丶导入 aop模块:Spring Aop: (spring-aspects) 2丶定义一个业务逻辑类(MathCalulcator);在业务逻辑运行的时候将日志进行打印(方法之前,方 阅读全文
posted @ 2021-11-08 22:06 KwFruit 阅读(128) 评论(0) 推荐(0)
摘要:Profile: Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能; 开发环境、测试环境、生产环境; 数据源:(/A)(/B)(/C); @Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件。1)丶加了环境标识的bean ,只有 阅读全文
posted @ 2021-08-10 23:18 KwFruit 阅读(100) 评论(0) 推荐(0)
摘要:1丶自定义组件想要使用Spring容器底层的一些组件(ApplicationContext,BeanFactory,xxx);自定义组件实现xxxAware;在创建对象的时候,会调用接口规定的方法注入相关组件;Aware; 把Spring底层的一些组件注入到自定义的Bean中; xxxAware: 阅读全文
posted @ 2021-08-09 23:01 KwFruit 阅读(83) 评论(0) 推荐(0)
摘要:@Autowired构造器 方法 参数 属性;都是从容器中获取参数组件的值 1 【标在方法位置】;@Bean + 方法参数;参数从容器中获取;默认不写@Autowired效果是一样的;都能自动装配;2 标在构造器上 如果组件只有一个有参构造器,这个有参构造器的@Autowired 可以省略,参数位置 阅读全文
posted @ 2021-08-08 23:05 KwFruit 阅读(125) 评论(0) 推荐(0)
摘要:Spring 还支持使用@Resource(JSR250)和@Injet(JSR330)[JAVA 规范的注解] @Resource: 可以和@Autowired一样实现自动装配功能; 默认是按照组件的名称进行装配 没有能支持@Priary功能没有支持@Autwired(required=false 阅读全文
posted @ 2021-08-05 23:35 KwFruit 阅读(95) 评论(0) 推荐(0)
摘要:自动装配; Spring利用依赖注入(DI),完成对IOC容器中各个组件的依赖关系赋值; 1)丶@Autowired: 自动注入 1 默认优先按照类型去容器中找队应的组件:applica.getBean(UserDao.class);找到就赋值 2 如果找到多个相同类型的组件,再将属性的名称作为组件 阅读全文
posted @ 2021-08-04 23:45 KwFruit 阅读(71) 评论(0) 推荐(0)
摘要:使用value赋值 1丶基本数值2丶可以写SpEL; #{}3丶可以写${};取出配置文件中的值(在运行环境变量里面的值) 1丶基本数值&可以写SpEL; #{} 一丶配置类 package com.mongoubiubiu.conf; import org.springframework.bean 阅读全文
posted @ 2021-08-03 22:49 KwFruit 阅读(87) 评论(0) 推荐(0)
摘要:1丶 BeanPostProcessor【interface】:bean的后置处理器; 在bean初始化前后进行一些处理工作 postProcessBeforeInitialization:在初始化之前工作 postProcessAfterInitialization:在初始化之后工作 BeanPo 阅读全文
posted @ 2021-07-27 23:29 KwFruit 阅读(79) 评论(0) 推荐(0)
摘要:可以使用JSR250; @PostConstruct: 在bean创建完成并且属性赋值完成;来执行初始化方法 @PreDestroy:在容器销毁bean之前通知我们进行清理工作 实现类 package com.mongoubiubiu.bean; import javax.annotation.Po 阅读全文
posted @ 2021-07-27 23:15 KwFruit 阅读(55) 评论(0) 推荐(0)
摘要:实现类 package com.mongoubiubiu.bean; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; 阅读全文
posted @ 2021-07-27 23:08 KwFruit 阅读(35) 评论(0) 推荐(0)
摘要:Bean的生命周期, bean创建 初始化 销毁的过程 容器管理bean的生命周期; 我们可以自定义初始化和销毁方法;容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法 单实例:在容器启动的时候创建对象多实例:在每次获取的时候创建对象 初始化: 对象创建完成,并赋值好,调用这个 阅读全文
posted @ 2021-07-27 22:55 KwFruit 阅读(78) 评论(0) 推荐(0)
摘要:实现类 package com.mongoubiubiu.bean; import org.springframework.beans.factory.FactoryBean; //创建一个spring 定义的工厂bean public class ColorFactoryBean implemen 阅读全文
posted @ 2021-07-26 23:10 KwFruit 阅读(49) 评论(0) 推荐(0)
摘要:实现类 package com.mongoubiubiu.condition; import org.springframework.beans.factory.support.BeanDefinitionDefaults; import org.springframework.beans.fact 阅读全文
posted @ 2021-07-26 22:54 KwFruit 阅读(60) 评论(0) 推荐(0)
摘要:实现类 package com.mongoubiubiu.condition; import org.springframework.context.annotation.ImportSelector; import org.springframework.core.type.AnnotationM 阅读全文
posted @ 2021-07-26 22:43 KwFruit 阅读(52) 评论(0) 推荐(0)
摘要:给容器中注册组件的三种方式 1)丶包扫描+组件标注注解(@Controller / @Service / @Repository /@Component )[自己写的类,方便加上注解,用上包扫描] 2)丶@Bean[导入的第三方包里面的组件] 3) 丶@Import[快速给容器中导入一个组件] 直接 阅读全文
posted @ 2021-07-26 22:16 KwFruit 阅读(45) 评论(0) 推荐(0)
摘要:@Conditional spring4.0之后添加的功能,按照一定的条件进行判断,满足条件注册bean 场景:2个Bean按照 Conditional 条件看注入或者不住入 @Bean public Person Bill(){ return new Person("Bill Gates",67) 阅读全文
posted @ 2021-07-25 23:04 KwFruit 阅读(66) 评论(0) 推荐(0)
摘要:懒加载: 单实例bean:默认在容器启动的时候创建对象; 懒加载:容器启动不创建对象。第一次使用(获取)Bean创建对象,并初始化; 默认是单实例的 //默认都是单实例的 @Bean public Person person(){ System.out.println("给容器中添加person") 阅读全文
posted @ 2021-07-25 22:31 KwFruit 阅读(50) 评论(0) 推荐(0)