spring总结
- spring mvc
request-->DispatcherServlet-->MappingHandler-->AdaptorHandler-->Handler-->ModelAndView(String逻辑视图,void直接response、sendRedict、forward)-->view-->response - IOC控制反转
对象的声明周期交给spring容器来管理,xml配置
,也可以通过注解的方式向spring容器注册bean;也可以通过代码注册(通过context获得DefaultListableBeanFactory接口类型的对象,调用registerSingleton和registerBeanDefinition方法注册)
注解 | 描述 |
---|---|
@Component | 没有明确的角色分工 |
@Repository | dao,dao层 |
@Service | service,service层 |
@Controller | action,表现层 |
使用注解的时候可以指定bean的id,如@Repository("userDao")
,如果不指定,默认为把类名的首字母改为小写后的字符串作为bean的id
- DI依赖注入
可以使用XML配置文件的方式(分为基于getter/setter方法和构造函数两种配置方式),也可以使用注解的方式。
注解 | 描述 |
---|---|
@Autowired(require=true/false) | Autowired按类型进行自动装配,配合@Qualifier使用,@Qualifier("beanid") |
@Inject | 是JSR-330提供的注解 |
@Resource(name="beanname") | 是JSR-250提供的注解 |
AOP面向切面编程
以动态代理为基础
其他注解
注解 | 描述 |
---|---|
@RequestBody | request数据使用json形式 |
@ResponseBody | json形式返回 |
@RequestMapping | url映射 |
@RequestParam | 参数绑定 |
spring核心组件
Bean,Context,Core
- Bean,即bean,是IOC容器中的bean
- Context,应用上下文,是IOC容器,是容器中各个bean之间的关系的集合
- Core,一些核心类,Resource表示资源,是通用的,屏蔽了不同类型的资源的差异。Resource 接口继承了 InputStreamSource 接口,这个接口中有个 getInputStream 方法,返回的是 InputStream 类,这样所有的资源都被可以通过 InputStream 这个类来获取,所以也屏蔽了资源的提供者。另外还有一个问题就是加载资源的问题,也就是资源的加载者要统一,这个任务是由 ResourceLoader 接口完成,它屏蔽了所有的资源加载者的差异,只需要实现这个接口就可以加载所有的资源,它的默认实现是 DefaultResourceLoader。
- BeanFactory接口;ApplicationContext接口就是IOC容器,它继承了BeanFactory接口。ApplicationContext有好几个实现,常见的有
ClassPathXmlApplicationContext
和FileSystemXmlApplicationContext
和WebApplicationContext
,配置的方式有多种,xml-based,annotation-based,java-based - 一般在IOC容器中注册dao,service,action,基础设施如SessionFactory等,domain的bean一般不注册,非IOC容器创建的bean也可以通过IOC来管理
Typically, you define service layer objects, data access objects (DAOs), presentation objects such as Struts Action instances, infrastructure objects such as Hibernate SessionFactories, JMS Queues, and so forth. Typically, one does not configure fine-grained domain objects in the container, because it is usually the responsibility of DAOs and business logic to create and load domain objects. However, you can use Spring’s integration with AspectJ to configure objects that have been created outside the control of an IoC container.
IOC(xml注册(单个或包扫描),注解注册,DefaultListableBeanFactory代码注册)
- Naming
- Alias
- Instantiate
- 默认构造器
- 静态工厂方法
对应的java代码
public class ClientService {
private static ClientService clientService = new ClientService();
private ClientService() {}
public static ClientService createInstance() {
return clientService;
}
}
- 实例工厂方法
对应java代码
public class DefaultServiceLocator {
private static ClientService clientService = new ClientServiceImpl();
private static AccountService accountService = new AccountServiceImpl();
public ClientService createClientServiceInstance() {
return clientService;
}
public AccountService createAccountServiceInstance() {
return accountService;
}
}
DI(xml配置,注解)
IOC容器通过构造器或者工厂方法创建了bean后,对bean进行依赖注入
- constructor-based
- 构造器参数是引用类型
package x.y;
public class ThingOne {
public ThingOne(ThingTwo thingTwo, ThingThree thingThree) {
// ...
}
}
- 构造器函数式简单类型
ackage examples;
public class ExampleBean {
// Number of years to calculate the Ultimate Answer
private int years;
// The Answer to Life, the Universe, and Everything
private String ultimateAnswer;
public ExampleBean(int years, String ultimateAnswer) {
this.years = years;
this.ultimateAnswer = ultimateAnswer;
}
}
①使用type
②使用index
,index从0开始
③使用name
,
Keep in mind that, to make this work out of the box, your code must be compiled with the debug flag enabled so that Spring can look up the parameter name from the constructor. If you cannot or do not want to compile your code with the debug flag, you can use @ConstructorProperties JDK annotation to explicitly name your constructor arguments. The sample class would then have to look as follows:
package examples;
public class ExampleBean {
// Fields omitted
@ConstructorProperties({"years", "ultimateAnswer"})
public ExampleBean(int years, String ultimateAnswer) {
this.years = years;
this.ultimateAnswer = ultimateAnswer;
}
}
- setter-based
spring团队提倡使用CDI,对于必须的依赖选择CDI,对于可选的依赖使用SDI,有的场景不得不使用SDI,比如要求可以重新配置或赋值的bean,再比如循环依赖,则必须对其中一个使用SDI
①SDI
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public void setBeanOne(AnotherBean beanOne) {
this.beanOne = beanOne;
}
public void setBeanTwo(YetAnotherBean beanTwo) {
this.beanTwo = beanTwo;
}
public void setIntegerProperty(int i) {
this.i = i;
}
}
②CDI
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public ExampleBean(
AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
this.beanOne = anotherBean;
this.beanTwo = yetAnotherBean;
this.i = i;
}
}
③静态工厂方法
public class ExampleBean {
// a private constructor
private ExampleBean(...) {
...
}
// a static factory method; the arguments to this method can be
// considered the dependencies of the bean that is returned,
// regardless of how those arguments are actually used.
public static ExampleBean createInstance (
AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
ExampleBean eb = new ExampleBean (...);
// some other operations...
return eb;
}
}
-
ref
,ref可以引用同一IOC容器或父容器中的bean,不管是不是定义在同一个xml文件中 idref
- 集合
[email protected]
[email protected]
[email protected]
just some string
- ·depends-on,保证自己所依赖的项在自己创建之前创建
- scope为singleton的bean,和有默认值的bean在IOC容器创建的时候被创建,需要懒加载可以使用
lazy-init
属性
scope
-
proxy-target-class
属性
false为jdk动态代理,true为cglib动态代理
生命周期callback函数
构建后和销毁前
Multiple lifecycle mechanisms configured for the same bean, with different initialization methods, are called as follows:
Methods annotated with @PostConstruct
afterPropertiesSet() as defined by the InitializingBean callback interface
A custom configured init() method
Destroy methods are called in the same order:
Methods annotated with @PreDestroy
destroy() as defined by the DisposableBean callback interface
A custom configured destroy() method
使用注解需要开启
@Autowired @Inject @Resource @Value
- JavaConfig方式
@Configuration @Bean @Import
@Configuration
和@Bean一起使用
@Import
@Configuration
用在类上,@Bean
用在方法上,该方法返回一个对象,将返回的对象注册到IOC容器中,注册的bean的name就是方法名 -
@Primary
@Qualifier
@Autowired
是根据类型来匹配的,如果存在多个condidates,我们使用@Primary
@Qualifier
来指明精确的匹配
@Resource(name="")
@Configuration
public class MovieConfiguration {
@Bean
@Primary
public MovieCatalog firstMovieCatalog() { ... }
@Bean
public MovieCatalog secondMovieCatalog() { ... }
// ...
}
public class MovieRecommender {
@Autowired
private MovieCatalog movieCatalog;
// ...
}
be equivalent to the following xml
public class MovieRecommender {
@Autowired
@Qualifier("main")
private MovieCatalog movieCatalog;
// ...
}
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(@Qualifier("main")MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
be equivalent to the following xml
-
@RestController
=@ResponseBody
+Controller
- 开启自动扫描,自动检测被
@Component, @Repository, @Service, @Controller
注解的类,自动注册到IOC容器中,如果没指定name属性的值,自动注册的bean的name为类名首字母小写
xml方式
@Service
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public SimpleMovieLister(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
@Repository
public class JpaMovieFinder implements MovieFinder {
// implementation elided for clarity
}
//javaConfig方式
@Configuration
@ComponentScan(basePackages = "org.example")
public class AppConfig {
...
}
AnnotationConfigApplicationContext
ClassPathXMLApplicationContext使用xml文件作为bean definition,AnnotationConfigApplicationContext使用注解
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}
-
@Respository @Service @Controller @Configuration
这几个注解的定义上都有@Component
注解,所以
可以检测到
xml配置和javaConfig配置结合使用
- xml为主,JavaConfig为辅
开启
或
,然后在代码中使用注解 - JavaConfig为主,xml为辅
使用@ImportResource
导入xml文件
@Configuration
@ImportResource("classpath:/com/acme/properties-config.xml")
public class AppConfig {
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource() {
return new DriverManagerDataSource(url, username, password);
}
}
xml文件
properties-config.xml
jdbc.properties文件
jdbc.properties
jdbc.url=jdbc:hsqldb:hsql://localhost/xdb
jdbc.username=sa
jdbc.password=
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
TransferService transferService = ctx.getBean(TransferService.class);
// ...
}
profile
不同的环境下(开发,测试,生产环境),不同的配置
- 开启profile
- 注解方式,如@Profile("development")
- xml方式,如
- 激活profile
- 代码方式
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles("development");
ctx.register(SomeConfig.class, StandaloneDataConfig.class, JndiDataConfig.class);
ctx.refresh();
- 系统变量方式
-Dspring.profiles.active="development"
Resource接口
表示资源,重要的方法有getInputStream()
,多个实现UrlResource
ClassPathResource,FileSystemResource,ServletContextResource
InputStreamResource,ByteArrayResource
ResourceLoader接口
方法为getResource(String location)
,所有的ApplicationContext的实现都实现了这个接口
Validator ValidationUtils
supports(Class c)
validate(Object target, Errors errors)
Converter
package org.springframework.core.convert.converter;
public interface Converter {
T convert(S source);
}
SpEL
AOP
- concepts
- Aspect
- Advise(5种类型,在join point上要干的事)
- join point(通常是一个方法执行)
- pointcut(pointcut expression)
- target object(目标对象)
- Proxy(代理,jdk动态代理或cglib代理)
- weaving(织入的时机可以是编译,加载,运行时)
- Introduction(给被代理的对象引入额外的的接口实现)
- types of advise
- before
- after return(正常结束)
- after exception(异常退出)
- after(finally,无论哪种退出方式)
- around(环绕通知,前后都可以定义定制化的操作,可以选择是否进入join point的执行流程,也可以shortcut advised method execution)
AspectJ
- enable aspectj supports
- define aspect
- annotation-stytle
@Aspect @Component(为了能使用扫描器) - xml-style
pointcut expression format
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern?)
修饰符 返回值类型 全限定类名 方法名 参数 异常
,除了execution
操作符还可以使用within target annotation args bean
等操作符advise body可以获取advised method 的参数,返回值,异常
@Pointcut("com.xyz.myapp.SystemArchitecture.dataAccessOperation() && args(account,..)")
private void accountDataAccessOperation(Account account) {}
@Before("accountDataAccessOperation(account)")
public void validateAccount(Account account) {
// ...
}
- xml形式
...
...
Aspect代码
public void monitor(Object service) {
...
}
after return
...
corresponding to java code
public void doAccessCheck(Object retVal) {...}
- force to use cglib proxy
or
transation
- PlatformTransactionManager
public interface PlatformTransactionManager {
TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException;
void commit(TransactionStatus status) throws TransactionException;
void rollback(TransactionStatus status) throws TransactionException;
}
@Transactional @EnableTransactionManagement
advisor相当于只有一个方法的aspect
- 默认回滚策略是发生了未处理的unchecked exception,checked exception默认不回滚
配置回滚的异常
配置特定异常不回滚
- annotation-based transaction
@EnableTransactionManagement @Transactional
- programmatic transaction
- Using the TransactionTemplate
- Using the PlatformTransactionManager
spring mvc
- DispacherServlet
- HandlerMapping
- RequestMappingHandlerMapping
- SimpleUrlHandlerMapping
hello1
hello2
- BeanNameUrlHandlerMapping(bean的name作为url)
- HandlerAdapter
-
SimpleControllerHandlerAdapter(handler需要直接或间接实现Controller)
-
HttpRequestHandlerAdapter(handler需要直接或间接实现HttpRequestHandler,
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
,没有返回值,可以通过转发、重定向、直接response.getWriter().write()
)
- ViewResolver
- InternalResourceViewResolver
or
- DispatcherServlet.properties
如果用户没有配置springmvc全局配置文件,就会使用这个 - HandlerExceptionResolver
- HandlerInterceptor
参数绑定
@RequestParam @RequestHeader @CookieValue @PathVariable @ModelAttribute
- ResponseEntity RequestEntity包含header和body
- CORS
@CrossOrigin
FreeMarker模板引擎
Controller接口
- 可以实现Controller接口或继承AbstractController抽象类,或者带有@Controller注解的普通类
public interface Controller {
ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception;
}
AbstractController抽象类实现了Controller
缓存
- cache头信息, Cache-Control :max-age=cacheSeconds
- If-Modified-Since
- etag