
SpringInActionFourthEdition
ZacharyZheng1
你若精彩,天自安排
展开
-
传递模型数据到视图中+jstl包
导入jstl相关包standardb包放入Tomcat的lib目录下jstl包放入web-inf/lib中没有这两个包,jsp解析时会报错传递模型数据实例@Controller@RequestMapping("/spittles")public class SpittleController { private SpittleRepository spittleRepository; @RequestMapping(method = RequestMethod.GET) public原创 2020-07-26 11:54:30 · 223 阅读 · 0 评论 -
springMVC起步-基本控制器测试
配置DispatcherServletpackage com.zachary.config;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;public class SpringWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Ov原创 2020-07-25 10:55:55 · 248 阅读 · 0 评论 -
在XML中申明切面
代码示例:Performance接口public interface Performance { void perform(String performContent);}Performance实现类@Componentpublic class Concert implements Performance { @Override public void perform(String performContent) { System.out.println(performContent原创 2020-07-19 21:43:33 · 196 阅读 · 0 评论 -
通过注解引入新功能
使用Spring AOP,我们可以为Bean引入新的方法。@DeclareParents示例通过@DeclareParents注解,将Encoreable接口引入到Performance bean中。Performance接口public interface Performance { void perform(String performContent);}Performance接口实现@Componentpublic class Concert implements Perform原创 2020-07-19 16:27:07 · 247 阅读 · 0 评论 -
定义切面通知
@AspectSpring使用AspectJ来申明通知方法。注解通知@After通知方法会在目标返回或抛出异常时调用@AfterReturning通知方法会在目标返回后调用@AfterThrowing通知方法在目标抛出异常时调用@Around通知方法将目标方法封装起来@Before通知方法在目标方调用前执行@Pointcut可以通过@Pointcut注解申明频繁使用的切点表达式@EnableAspectJAutoProxy启动Aspect原创 2020-07-13 17:14:04 · 225 阅读 · 0 评论 -
定义简单的切面
连接点方法:preform()public interface Performance { void preform();}定义实现类@Componentpublic class Concert implements Performance { @Override public void preform() { System.out.println("表演开始"); System.out.println("表演中......"); System.out.println("表演原创 2020-06-29 00:25:56 · 812 阅读 · 0 评论 -
SpEL样例
public class BlankDisc implements CompactDisc { public String title; public String artist; public List<Object> tracks; public BlankDisc(String title, String artist, List<Object> tracks) { this.title = title; this.artist = artist; th原创 2020-06-28 00:12:45 · 266 阅读 · 0 评论 -
运行时值注入
直接注入Bean直接注入示例:@Bean("blankDisc1")@Profile("dev")@Primarypublic BlankDisc getDevBlankDiscPrimary() { BlankDisc blankDisc = new BlankDisc(); blankDisc.setTitle("Sgt. Pepper's Lonely Hearts Club Band dev Primary"); List<String> tracks = new Arr原创 2020-06-27 17:59:08 · 273 阅读 · 0 评论 -
Bean的作用域
在默认情况下,Spring应用上下文中所有的Bean都是以单例(singleton)的形式创建的。Spring定义了多种作用域,可以基于这些作用域创建Bean:**单例(Singleton):**整个应用中只创建Bean的一个实例。**原型(Prototype):**每次注入,或通过Spring上下文获取时,都会创建一个新的Bean实例。**会话(Session):**在web应用中,为每个会话创建一个Bean实例。**请求(Request)????*在web应用中,为每个请求创建一个Bean实原创 2020-06-27 12:05:43 · 145 阅读 · 0 评论 -
条件化Bean,限定自动转配Bean
条件化Beanprofile与环境@Profile注解在类上使用使用@Profiledev环境模拟@Configuration@Profile("dev")public class DevelopmentProfileConfig { @Bean("blankDisc") public BlankDisc getBlankDisc() { BlankDisc blankDisc = new BlankDisc(); blankDisc.setTitle("Sgt. Pepper'原创 2020-06-26 22:02:32 · 198 阅读 · 0 评论 -
通过XML配置Bean
最简单的Spring 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原创 2020-06-22 22:56:26 · 266 阅读 · 0 评论 -
装配Bean
自动装配Bean相关注解@Component组件扫描时,如果类上有@Component注解,为其创建bean注解参数说明:@Component没有参数时,默认将类名首字母小写作为bean的ID@Component(“XXX”)设置bean的ID@NamedJava依赖注入规范(Java Dependency Injection)中所提供。Spring支持将@Named作为@Component注解的替代方案。两者有细微的差异,但是在大多数场景可以相互替换。@Autowired@Inje原创 2020-06-21 20:50:45 · 212 阅读 · 0 评论 -
Spring最简单的自动化装配Bean,总结使用junit测试遇到的坑
soundsystem例子创建CD接口package com.zachary.soundsystem;/** * @author Zachary.Zheng * @version 1.0 * @date 2020年6月20日 下午9:20:07 */public interface CompactDisc { void play();}创建SgtPeppers类实现CompactDisc 接口,使用@Component注解@Componentpublic class SgtPe原创 2020-06-21 11:26:19 · 427 阅读 · 0 评论 -
下载spring框架jar包
Spring官网地址https://spring.io/1.打开Spring Framework的开源项目Spring Framework概述Spring Framework文档和APISpring是开源项目,所以相关依赖jar包可在GitHub中找找到 Spring Framework Artifacts打开Spring Artifactory有三个版本可供选择,一般选择稳定的release版本打开依赖包地址,嫌麻烦的可以忽略前面的步骤https://repo.spri原创 2020-06-21 10:49:12 · 426 阅读 · 0 评论