import{_ as p}from"./plugin-vue_export-helper.21dcd24c.js";import{r as o,o as c,c as i,a as s,b as t,d as n,e}from"./app.615e41d8.js";const l={},r=s("h1",{id:"\u9762\u6E23\u9006\u88AD-spring-\u5FC5\u770B",tabindex:"-1"},[s("a",{class:"header-anchor",href:"#\u9762\u6E23\u9006\u88AD-spring-\u5FC5\u770B","aria-hidden":"true"},"#"),n(" \u9762\u6E23\u9006\u88AD\uFF08Spring\uFF09\u5FC5\u770B\u{1F44D}")],-1),u=n("\u56FE\u6587\u8BE6\u89E3 35 \u9053Spring\u9762\u8BD5\u9AD8\u9891\u9898\uFF0C\u8FD9\u6B21\u540A\u6253\u9762\u8BD5\u5B98\uFF0C\u6211\u89C9\u5F97\u7A33\u4E86\uFF08\u624B\u52A8 dog\uFF09\u3002\u6574\u7406\uFF1A\u6C89\u9ED8\u738B\u4E8C\uFF0C\u6233"),d={href:"https://mp.weixin.qq.com/s/EQge6DmgIqYITM3mAxkatg",target:"_blank",rel:"noopener noreferrer"},k=n("\u8F6C\u8F7D\u94FE\u63A5"),v=n("\uFF0C\u4F5C\u8005\uFF1A\u4E09\u5206\u6076\uFF0C\u6233"),m={href:"https://mp.weixin.qq.com/s/Y17S85ntHm_MLTZMJdtjQQ",target:"_blank",rel:"noopener noreferrer"},b=n("\u539F\u6587\u94FE\u63A5"),g=n("\u3002"),f=e(`
\u4E00\u53E5\u8BDD\u6982\u62EC\uFF1ASpring \u662F\u4E00\u4E2A\u8F7B\u91CF\u7EA7\u3001\u975E\u5165\u4FB5\u5F0F\u7684\u63A7\u5236\u53CD\u8F6C (IoC) \u548C\u9762\u5411\u5207\u9762 (AOP) \u7684\u6846\u67B6\u3002
2003 \u5E74\uFF0C\u4E00\u4E2A\u97F3\u4E50\u5BB6 Rod Johnson \u51B3\u5B9A\u53D1\u5C55\u4E00\u4E2A\u8F7B\u91CF\u7EA7\u7684 Java \u5F00\u53D1\u6846\u67B6\uFF0CSpring
\u4F5C\u4E3A Java \u6218\u573A\u7684\u9F99\u9A91\u5175\u6E10\u6E10\u5D1B\u8D77\uFF0C\u5E76\u6DD8\u6C70\u4E86EJB
\u8FD9\u4E2A\u4F20\u7EDF\u7684\u91CD\u88C5\u9A91\u5175\u3002
\u5230\u4E86\u73B0\u5728\uFF0C\u4F01\u4E1A\u7EA7\u5F00\u53D1\u7684\u6807\u914D\u57FA\u672C\u5C31\u662F Spring5 + Spring Boot 2 + JDK 8
Spring \u6709\u54EA\u4E9B\u7279\u6027\u5462\uFF1F
Spring \u6709\u5F88\u591A\u4F18\u70B9\uFF1A
Spring \u7684\u6838\u5FC3\u5C31\u662F\u4E00\u4E2A\u5927\u7684\u5DE5\u5382\u5BB9\u5668\uFF0C\u53EF\u4EE5\u7EF4\u62A4\u6240\u6709\u5BF9\u8C61\u7684\u521B\u5EFA\u548C\u4F9D\u8D56\u5173\u7CFB\uFF0CSpring \u5DE5\u5382\u7528\u4E8E\u751F\u6210 Bean\uFF0C\u5E76\u4E14\u7BA1\u7406 Bean \u7684\u751F\u547D\u5468\u671F\uFF0C\u5B9E\u73B0\u9AD8\u5185\u805A\u4F4E\u8026\u5408\u7684\u8BBE\u8BA1\u7406\u5FF5\u3002
Spring \u63D0\u4F9B\u4E86\u9762\u5411\u5207\u9762\u7F16\u7A0B\uFF0C\u53EF\u4EE5\u65B9\u4FBF\u7684\u5B9E\u73B0\u5BF9\u7A0B\u5E8F\u8FDB\u884C\u6743\u9650\u62E6\u622A\u3001\u8FD0\u884C\u76D1\u63A7\u7B49\u5207\u9762\u529F\u80FD\u3002
\u652F\u6301\u901A\u8FC7\u914D\u7F6E\u5C31\u6765\u5B8C\u6210\u5BF9\u4E8B\u52A1\u7684\u7BA1\u7406\uFF0C\u800C\u4E0D\u9700\u8981\u901A\u8FC7\u786C\u7F16\u7801\u7684\u65B9\u5F0F\uFF0C\u4EE5\u524D\u91CD\u590D\u7684\u4E00\u4E9B\u4E8B\u52A1\u63D0\u4EA4\u3001\u56DE\u6EDA\u7684 JDBC \u4EE3\u7801\uFF0C\u90FD\u53EF\u4EE5\u4E0D\u7528\u81EA\u5DF1\u5199\u4E86\u3002
Spring \u5BF9 Junit \u63D0\u4F9B\u652F\u6301\uFF0C\u53EF\u4EE5\u901A\u8FC7\u6CE8\u89E3\u5FEB\u6377\u5730\u6D4B\u8BD5 Spring \u7A0B\u5E8F\u3002
\u65B9\u4FBF\u96C6\u6210\u5404\u79CD\u4F18\u79C0\u6846\u67B6\uFF0CSpring \u4E0D\u6392\u65A5\u5404\u79CD\u4F18\u79C0\u7684\u5F00\u6E90\u6846\u67B6\uFF0C\u5176\u5185\u90E8\u63D0\u4F9B\u4E86\u5BF9\u5404\u79CD\u4F18\u79C0\u6846\u67B6\uFF08\u5982\uFF1AStruts\u3001Hibernate\u3001MyBatis\u3001Quartz \u7B49\uFF09\u7684\u76F4\u63A5\u652F\u6301\u3002
Spring \u5BF9 JavaEE \u5F00\u53D1\u4E2D\u975E\u5E38\u96BE\u7528\u7684\u4E00\u4E9B API\uFF08JDBC\u3001JavaMail\u3001\u8FDC\u7A0B\u8C03\u7528\u7B49\uFF09\u90FD\u63D0\u4F9B\u4E86\u6A21\u677F\u5316\u7684\u5C01\u88C5\uFF0C\u8FD9\u4E9B\u5C01\u88C5 API \u7684\u63D0\u4F9B\u4F7F\u5F97\u5E94\u7528\u96BE\u5EA6\u5927\u5927\u964D\u4F4E\u3002
Spring \u6846\u67B6\u662F\u5206\u6A21\u5757\u5B58\u5728\uFF0C\u9664\u4E86\u6700\u6838\u5FC3\u7684Spring Core Container
\u662F\u5FC5\u8981\u6A21\u5757\u4E4B\u5916\uFF0C\u5176\u4ED6\u6A21\u5757\u90FD\u662F\u53EF\u9009
\uFF0C\u5927\u7EA6\u6709 20 \u591A\u4E2A\u6A21\u5757\u3002
\u6700\u4E3B\u8981\u7684\u4E03\u5927\u6A21\u5757\uFF1A
Spring \u6709\u5F88\u591A\u6A21\u5757\uFF0C\u751A\u81F3\u5E7F\u4E49\u7684 SpringBoot\u3001SpringCloud \u4E5F\u7B97\u662F Spring \u7684\u4E00\u90E8\u5206\uFF0C\u6211\u4EEC\u6765\u5206\u6A21\u5757\uFF0C\u6309\u529F\u80FD\u6765\u770B\u4E00\u4E0B\u4E00\u4E9B\u5E38\u7528\u7684\u6CE8\u89E3\uFF1A
Web:
@RequestMapping(\u201C/hello/{name}\u201D)
\u7533\u660E\u7684\u8DEF\u5F84\uFF0C\u5C06\u6CE8\u89E3\u653E\u5728\u53C2\u6570\u4E2D\u524D\uFF0C\u5373\u53EF\u83B7\u53D6\u8BE5\u503C\uFF0C\u901A\u5E38\u4F5C\u4E3A Restful \u7684\u63A5\u53E3\u5B9E\u73B0\u65B9\u6CD5\u3002\u5BB9\u5668:
#{} \u8DDF \\\${}
\u4E24\u4E2A\u65B9\u5F0F\u3002\u4E00\u822C\u5C06 SpringbBoot \u4E2D\u7684 application.properties \u914D\u7F6E\u7684\u5C5E\u6027\u503C\u8D4B\u503C\u7ED9\u53D8\u91CF\u3002@Bean(initMethod=\u201Dinit\u201D,destroyMethod=\u201Ddestroy\u201D)
\u5B9A\u4E49\uFF0C\u5728\u6784\u9020\u4E4B\u540E\u6267\u884C init\uFF0C\u5728\u9500\u6BC1\u4E4B\u524D\u6267\u884C destroy\u3002AOP:
@After
\uFF1A\u5728\u65B9\u6CD5\u6267\u884C\u4E4B\u540E\u6267\u884C\uFF08\u65B9\u6CD5\u4E0A\uFF09\u3002@Before
\uFF1A \u5728\u65B9\u6CD5\u6267\u884C\u4E4B\u524D\u6267\u884C\uFF08\u65B9\u6CD5\u4E0A\uFF09\u3002@Around
\uFF1A \u5728\u65B9\u6CD5\u6267\u884C\u4E4B\u524D\u4E0E\u4E4B\u540E\u6267\u884C\uFF08\u65B9\u6CD5\u4E0A\uFF09\u3002@PointCut
\uFF1A \u58F0\u660E\u5207\u70B9 \u5728 java \u914D\u7F6E\u7C7B\u4E2D\u4F7F\u7528@EnableAspectJAutoProxy \u6CE8\u89E3\u5F00\u542F Spring \u5BF9 AspectJ \u4EE3\u7406\u7684\u652F\u6301\uFF08\u7C7B\u4E0A\uFF09\u3002\u4E8B\u52A1\uFF1A
Spring \u6846\u67B6\u4E2D\u5E7F\u6CDB\u4F7F\u7528\u4E86\u4E0D\u540C\u7C7B\u578B\u7684\u8BBE\u8BA1\u6A21\u5F0F\uFF0C\u4E0B\u9762\u6211\u4EEC\u6765\u770B\u770B\u5230\u5E95\u6709\u54EA\u4E9B\u8BBE\u8BA1\u6A21\u5F0F?
Java \u662F\u9762\u5411\u5BF9\u8C61\u7684\u7F16\u7A0B\u8BED\u8A00\uFF0C\u4E00\u4E2A\u4E2A\u5B9E\u4F8B\u5BF9\u8C61\u76F8\u4E92\u5408\u4F5C\u7EC4\u6210\u4E86\u4E1A\u52A1\u903B\u8F91\uFF0C\u539F\u6765\uFF0C\u6211\u4EEC\u90FD\u662F\u5728\u4EE3\u7801\u91CC\u521B\u5EFA\u5BF9\u8C61\u548C\u5BF9\u8C61\u7684\u4F9D\u8D56\u3002
\u6240\u8C13\u7684IOC\uFF08\u63A7\u5236\u53CD\u8F6C\uFF09\uFF1A\u5C31\u662F\u7531\u5BB9\u5668\u6765\u8D1F\u8D23\u63A7\u5236\u5BF9\u8C61\u7684\u751F\u547D\u5468\u671F\u548C\u5BF9\u8C61\u95F4\u7684\u5173\u7CFB\u3002\u4EE5\u524D\u662F\u6211\u4EEC\u60F3\u8981\u4EC0\u4E48\uFF0C\u5C31\u81EA\u5DF1\u521B\u5EFA\u4EC0\u4E48\uFF0C\u73B0\u5728\u662F\u6211\u4EEC\u9700\u8981\u4EC0\u4E48\uFF0C\u5BB9\u5668\u5C31\u7ED9\u6211\u4EEC\u9001\u6765\u4EC0\u4E48\u3002
\u4E5F\u5C31\u662F\u8BF4\uFF0C\u63A7\u5236\u5BF9\u8C61\u751F\u547D\u5468\u671F\u7684\u4E0D\u518D\u662F\u5F15\u7528\u5B83\u7684\u5BF9\u8C61\uFF0C\u800C\u662F\u5BB9\u5668\u3002\u5BF9\u5177\u4F53\u5BF9\u8C61\uFF0C\u4EE5\u524D\u662F\u5B83\u63A7\u5236\u5176\u5B83\u5BF9\u8C61\uFF0C\u73B0\u5728\u6240\u6709\u5BF9\u8C61\u90FD\u88AB\u5BB9\u5668\u63A7\u5236\uFF0C\u6240\u4EE5\u8FD9\u5C31\u53EB\u63A7\u5236\u53CD\u8F6C\u3002
DI\uFF08\u4F9D\u8D56\u6CE8\u5165\uFF09\uFF1A\u6307\u7684\u662F\u5BB9\u5668\u5728\u5B9E\u4F8B\u5316\u5BF9\u8C61\u7684\u65F6\u5019\u628A\u5B83\u4F9D\u8D56\u7684\u7C7B\u6CE8\u5165\u7ED9\u5B83\u3002\u6709\u7684\u8BF4\u6CD5 IOC \u548C DI \u662F\u4E00\u56DE\u4E8B\uFF0C\u6709\u7684\u8BF4\u6CD5\u662F IOC \u662F\u601D\u60F3\uFF0CDI \u662F IOC \u7684\u5B9E\u73B0\u3002
\u4E3A\u4EC0\u4E48\u8981\u4F7F\u7528 IOC \u5462\uFF1F
\u6700\u4E3B\u8981\u7684\u662F\u4E24\u4E2A\u5B57\u89E3\u8026\uFF0C\u786C\u7F16\u7801\u4F1A\u9020\u6210\u5BF9\u8C61\u95F4\u7684\u8FC7\u5EA6\u8026\u5408\uFF0C\u4F7F\u7528 IOC \u4E4B\u540E\uFF0C\u6211\u4EEC\u53EF\u4EE5\u4E0D\u7528\u5173\u5FC3\u5BF9\u8C61\u95F4\u7684\u4F9D\u8D56\uFF0C\u4E13\u5FC3\u5F00\u53D1\u5E94\u7528\u5C31\u884C\u3002
PS:\u8FD9\u9053\u9898\u8001\u4E09\u5728\u9762\u8BD5\u4E2D\u88AB\u95EE\u5230\u8FC7\uFF0C\u95EE\u6CD5\u662F\u201C\u4F60\u6709\u81EA\u5DF1\u5B9E\u73B0\u8FC7\u7B80\u5355\u7684 Spring \u5417\uFF1F\u201D
Spring \u7684 IOC \u672C\u8D28\u5C31\u662F\u4E00\u4E2A\u5927\u5DE5\u5382\uFF0C\u6211\u4EEC\u60F3\u60F3\u4E00\u4E2A\u5DE5\u5382\u662F\u600E\u4E48\u8FD0\u884C\u7684\u5462\uFF1F
\u751F\u4EA7\u4EA7\u54C1\uFF1A\u4E00\u4E2A\u5DE5\u5382\u6700\u6838\u5FC3\u7684\u529F\u80FD\u5C31\u662F\u751F\u4EA7\u4EA7\u54C1\u3002\u5728 Spring \u91CC\uFF0C\u4E0D\u7528 Bean \u81EA\u5DF1\u6765\u5B9E\u4F8B\u5316\uFF0C\u800C\u662F\u4EA4\u7ED9 Spring\uFF0C\u5E94\u8BE5\u600E\u4E48\u5B9E\u73B0\u5462\uFF1F\u2014\u2014\u7B54\u6848\u6BEB\u65E0\u7591\u95EE\uFF0C\u53CD\u5C04\u3002
\u90A3\u4E48\u8FD9\u4E2A\u5382\u5B50\u7684\u751F\u4EA7\u7BA1\u7406\u662F\u600E\u4E48\u505A\u7684\uFF1F\u4F60\u5E94\u8BE5\u4E5F\u77E5\u9053\u2014\u2014\u5DE5\u5382\u6A21\u5F0F\u3002
\u5E93\u5B58\u4EA7\u54C1\uFF1A\u5DE5\u5382\u4E00\u822C\u90FD\u662F\u6709\u5E93\u623F\u7684\uFF0C\u7528\u6765\u5E93\u5B58\u4EA7\u54C1\uFF0C\u6BD5\u7ADF\u751F\u4EA7\u7684\u4EA7\u54C1\u4E0D\u80FD\u7ACB\u9A6C\u5C31\u62C9\u8D70\u3002Spring \u6211\u4EEC\u90FD\u77E5\u9053\u662F\u4E00\u4E2A\u5BB9\u5668\uFF0C\u8FD9\u4E2A\u5BB9\u5668\u91CC\u5B58\u7684\u5C31\u662F\u5BF9\u8C61\uFF0C\u4E0D\u80FD\u6BCF\u6B21\u6765\u53D6\u5BF9\u8C61\uFF0C\u90FD\u5F97\u73B0\u573A\u6765\u53CD\u5C04\u521B\u5EFA\u5BF9\u8C61\uFF0C\u5F97\u628A\u521B\u5EFA\u51FA\u7684\u5BF9\u8C61\u5B58\u8D77\u6765\u3002
\u8BA2\u5355\u5904\u7406\uFF1A\u8FD8\u6709\u6700\u91CD\u8981\u7684\u4E00\u70B9\uFF0C\u5DE5\u5382\u6839\u636E\u4EC0\u4E48\u6765\u63D0\u4F9B\u4EA7\u54C1\u5462\uFF1F\u8BA2\u5355\u3002\u8FD9\u4E9B\u8BA2\u5355\u53EF\u80FD\u4E94\u82B1\u516B\u95E8\uFF0C\u6709\u7EBF\u4E0A\u7B7E\u7B7E\u7684\u3001\u6709\u5230\u5DE5\u5382\u7B7E\u7684\u3001\u8FD8\u6709\u5DE5\u5382\u9500\u552E\u4E0A\u95E8\u7B7E\u7684\u2026\u2026\u6700\u540E\u7ECF\u8FC7\u5904\u7406\uFF0C\u6307\u5BFC\u5DE5\u5382\u7684\u51FA\u8D27\u3002
\u5728 Spring \u91CC\uFF0C\u4E5F\u6709\u8FD9\u6837\u7684\u8BA2\u5355\uFF0C\u5B83\u5C31\u662F\u6211\u4EEC bean \u7684\u5B9A\u4E49\u548C\u4F9D\u8D56\u5173\u7CFB\uFF0C\u53EF\u4EE5\u662F xml \u5F62\u5F0F\uFF0C\u4E5F\u53EF\u4EE5\u662F\u6211\u4EEC\u6700\u719F\u6089\u7684\u6CE8\u89E3\u5F62\u5F0F\u3002
\u6211\u4EEC\u7B80\u5355\u5730\u5B9E\u73B0\u4E00\u4E2A mini \u7248\u7684 Spring IOC\uFF1A
Bean \u5B9A\u4E49\uFF1A
Bean \u901A\u8FC7\u4E00\u4E2A\u914D\u7F6E\u6587\u4EF6\u5B9A\u4E49\uFF0C\u628A\u5B83\u89E3\u6790\u6210\u4E00\u4E2A\u7C7B\u578B\u3002
beans.properties
\u5077\u61D2\uFF0C\u8FD9\u91CC\u76F4\u63A5\u7528\u4E86\u6700\u65B9\u4FBF\u89E3\u6790\u7684 properties\uFF0C\u8FD9\u91CC\u76F4\u63A5\u7528\u4E00\u4E2A<key,value>
\u7C7B\u578B\u7684\u914D\u7F6E\u6765\u4EE3\u8868 Bean \u7684\u5B9A\u4E49\uFF0C\u5176\u4E2D key \u662F beanName\uFF0Cvalue \u662F class
userDao:cn.fighter3.bean.UserDao
BeanDefinition.java
bean \u5B9A\u4E49\u7C7B\uFF0C\u914D\u7F6E\u6587\u4EF6\u4E2D bean \u5B9A\u4E49\u5BF9\u5E94\u7684\u5B9E\u4F53
public class BeanDefinition {
private String beanName;
private Class beanClass;
//\u7701\u7565getter\u3001setter
}
ResourceLoader.java
\u8D44\u6E90\u52A0\u8F7D\u5668\uFF0C\u7528\u6765\u5B8C\u6210\u914D\u7F6E\u6587\u4EF6\u4E2D\u914D\u7F6E\u7684\u52A0\u8F7D
public class ResourceLoader {
public static Map<String, BeanDefinition> getResource() {
Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>(16);
Properties properties = new Properties();
try {
InputStream inputStream = ResourceLoader.class.getResourceAsStream("/beans.properties");
properties.load(inputStream);
Iterator<String> it = properties.stringPropertyNames().iterator();
while (it.hasNext()) {
String key = it.next();
String className = properties.getProperty(key);
BeanDefinition beanDefinition = new BeanDefinition();
beanDefinition.setBeanName(key);
Class clazz = Class.forName(className);
beanDefinition.setBeanClass(clazz);
beanDefinitionMap.put(key, beanDefinition);
}
inputStream.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return beanDefinitionMap;
}
}
BeanRegister.java
\u5BF9\u8C61\u6CE8\u518C\u5668\uFF0C\u8FD9\u91CC\u7528\u4E8E\u5355\u4F8B bean \u7684\u7F13\u5B58\uFF0C\u6211\u4EEC\u5927\u5E45\u7B80\u5316\uFF0C\u9ED8\u8BA4\u6240\u6709 bean \u90FD\u662F\u5355\u4F8B\u7684\u3002\u53EF\u4EE5\u770B\u5230\u6240\u8C13\u5355\u4F8B\u6CE8\u518C\uFF0C\u4E5F\u5F88\u7B80\u5355\uFF0C\u4E0D\u8FC7\u662F\u5F80 HashMap \u91CC\u5B58\u5BF9\u8C61\u3002
public class BeanRegister {
//\u5355\u4F8BBean\u7F13\u5B58
private Map<String, Object> singletonMap = new HashMap<>(32);
/**
* \u83B7\u53D6\u5355\u4F8BBean
*
* @param beanName bean\u540D\u79F0
* @return
*/
public Object getSingletonBean(String beanName) {
return singletonMap.get(beanName);
}
/**
* \u6CE8\u518C\u5355\u4F8Bbean
*
* @param beanName
* @param bean
*/
public void registerSingletonBean(String beanName, Object bean) {
if (singletonMap.containsKey(beanName)) {
return;
}
singletonMap.put(beanName, bean);
}
}
BeanFactory.java
\u5BF9\u8C61\u5DE5\u5382\uFF0C\u6211\u4EEC\u6700\u6838\u5FC3\u7684\u4E00\u4E2A\u7C7B\uFF0C\u5728\u5B83\u521D\u59CB\u5316\u7684\u65F6\u5019\uFF0C\u521B\u5EFA\u4E86 bean \u6CE8\u518C\u5668\uFF0C\u5B8C\u6210\u4E86\u8D44\u6E90\u7684\u52A0\u8F7D\u3002
\u83B7\u53D6 bean \u7684\u65F6\u5019\uFF0C\u5148\u4ECE\u5355\u4F8B\u7F13\u5B58\u4E2D\u53D6\uFF0C\u5982\u679C\u6CA1\u6709\u53D6\u5230\uFF0C\u5C31\u521B\u5EFA\u5E76\u6CE8\u518C\u4E00\u4E2A bean
public class BeanFactory {
private Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>();
private BeanRegister beanRegister;
public BeanFactory() {
//\u521B\u5EFAbean\u6CE8\u518C\u5668
beanRegister = new BeanRegister();
//\u52A0\u8F7D\u8D44\u6E90
this.beanDefinitionMap = new ResourceLoader().getResource();
}
/**
* \u83B7\u53D6bean
*
* @param beanName bean\u540D\u79F0
* @return
*/
public Object getBean(String beanName) {
//\u4ECEbean\u7F13\u5B58\u4E2D\u53D6
Object bean = beanRegister.getSingletonBean(beanName);
if (bean != null) {
return bean;
}
//\u6839\u636Ebean\u5B9A\u4E49\uFF0C\u521B\u5EFAbean
return createBean(beanDefinitionMap.get(beanName));
}
/**
* \u521B\u5EFABean
*
* @param beanDefinition bean\u5B9A\u4E49
* @return
*/
private Object createBean(BeanDefinition beanDefinition) {
try {
Object bean = beanDefinition.getBeanClass().newInstance();
//\u7F13\u5B58bean
beanRegister.registerSingletonBean(beanDefinition.getBeanName(), bean);
return bean;
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}
\u6D4B\u8BD5
UserDao.java
\u6211\u4EEC\u7684 Bean \u7C7B\uFF0C\u5F88\u7B80\u5355
public class UserDao {
public void queryUserInfo(){
System.out.println("A good man.");
}
}
\u5355\u5143\u6D4B\u8BD5
public class ApiTest {
@Test
public void test_BeanFactory() {
//1.\u521B\u5EFAbean\u5DE5\u5382(\u540C\u65F6\u5B8C\u6210\u4E86\u52A0\u8F7D\u8D44\u6E90\u3001\u521B\u5EFA\u6CE8\u518C\u5355\u4F8Bbean\u6CE8\u518C\u5668\u7684\u64CD\u4F5C)
BeanFactory beanFactory = new BeanFactory();
//2.\u7B2C\u4E00\u6B21\u83B7\u53D6bean\uFF08\u901A\u8FC7\u53CD\u5C04\u521B\u5EFAbean\uFF0C\u7F13\u5B58bean\uFF09
UserDao userDao1 = (UserDao) beanFactory.getBean("userDao");
userDao1.queryUserInfo();
//3.\u7B2C\u4E8C\u6B21\u83B7\u53D6bean\uFF08\u4ECE\u7F13\u5B58\u4E2D\u83B7\u53D6bean\uFF09
UserDao userDao2 = (UserDao) beanFactory.getBean("userDao");
userDao2.queryUserInfo();
}
}
\u8FD0\u884C\u7ED3\u679C
A good man.
A good man.
\u81F3\u6B64\uFF0C\u6211\u4EEC\u4E00\u4E2A\u4E5E\u4E10+\u7834\u8239\u7248\u7684 Spring \u5C31\u5B8C\u6210\u4E86\uFF0C\u4EE3\u7801\u4E5F\u6BD4\u8F83\u5B8C\u6574\uFF0C\u6709\u6761\u4EF6\u7684\u53EF\u4EE5\u8DD1\u4E00\u4E0B\u3002
PS:\u56E0\u4E3A\u65F6\u95F4+\u7BC7\u5E45\u7684\u9650\u5236\uFF0C\u8FD9\u4E2A demo \u6BD4\u8F83\u7B80\u964B\uFF0C\u6CA1\u6709\u9762\u5411\u63A5\u53E3\u3001\u6CA1\u6709\u89E3\u8026\u3001\u8FB9\u754C\u68C0\u67E5\u3001\u5F02\u5E38\u5904\u7406\u2026\u2026\u5065\u58EE\u6027\u3001\u6269\u5C55\u6027\u90FD\u6709\u5F88\u5927\u7684\u4E0D\u8DB3\uFF0C\u611F\u5174\u8DA3\u53EF\u4EE5\u5B66\u4E60\u53C2\u8003[15]\u3002
\u53EF\u4EE5\u8FD9\u4E48\u5F62\u5BB9\uFF0CBeanFactory \u662F Spring \u7684\u201C\u5FC3\u810F\u201D\uFF0CApplicantContext \u662F\u5B8C\u6574\u7684\u201C\u8EAB\u8EAF\u201D\u3002
BeanFactory \u662F\u7C7B\u7684\u901A\u7528\u5DE5\u5382\uFF0C\u53EF\u4EE5\u521B\u5EFA\u5E76\u7BA1\u7406\u5404\u79CD\u7C7B\u7684\u5BF9\u8C61\u3002
Spring \u4E3A BeanFactory \u63D0\u4F9B\u4E86\u5F88\u591A\u79CD\u5B9E\u73B0\uFF0C\u6700\u5E38\u7528\u7684\u662F XmlBeanFactory\uFF0C\u4F46\u5728 Spring 3.2 \u4E2D\u5DF2\u88AB\u5E9F\u5F03\uFF0C\u5EFA\u8BAE\u4F7F\u7528 XmlBeanDefinitionReader\u3001DefaultListableBeanFactory\u3002
BeanFactory \u63A5\u53E3\u4F4D\u4E8E\u7C7B\u7ED3\u6784\u6811\u7684\u9876\u7AEF\uFF0C\u5B83\u6700\u4E3B\u8981\u7684\u65B9\u6CD5\u5C31\u662F getBean(String var1)\uFF0C\u8FD9\u4E2A\u65B9\u6CD5\u4ECE\u5BB9\u5668\u4E2D\u8FD4\u56DE\u7279\u5B9A\u540D\u79F0\u7684 Bean\u3002
BeanFactory \u7684\u529F\u80FD\u901A\u8FC7\u5176\u5B83\u7684\u63A5\u53E3\u5F97\u5230\u4E86\u4E0D\u65AD\u7684\u6269\u5C55\uFF0C\u6BD4\u5982 AbstractAutowireCapableBeanFactory \u5B9A\u4E49\u4E86\u5C06\u5BB9\u5668\u4E2D\u7684 Bean \u6309\u7167\u67D0\u79CD\u89C4\u5219\uFF08\u6BD4\u5982\u6309\u540D\u5B57\u5339\u914D\u3001\u6309\u7C7B\u578B\u5339\u914D\u7B49\uFF09\u8FDB\u884C\u81EA\u52A8\u88C5\u914D\u7684\u65B9\u6CD5\u3002
\u8FD9\u91CC\u770B\u4E00\u4E2A XMLBeanFactory\uFF08\u5DF2\u8FC7\u671F\uFF09 \u83B7\u53D6 bean \u7684\u4F8B\u5B50\uFF1A
public class HelloWorldApp{
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory (new ClassPathResource("beans.xml"));
HelloWorld obj = (HelloWorld) factory.getBean("helloWorld");
obj.getMessage();
}
}
ApplicationContext \u7531 BeanFactory \u6D3E\u751F\u800C\u6765\uFF0C\u63D0\u4F9B\u4E86\u66F4\u591A\u9762\u5411\u5B9E\u9645\u5E94\u7528\u7684\u529F\u80FD\u3002\u53EF\u4EE5\u8FD9\u4E48\u8BF4\uFF0C\u4F7F\u7528 BeanFactory \u5C31\u662F\u624B\u52A8\u6863\uFF0C\u4F7F\u7528 ApplicationContext \u5C31\u662F\u81EA\u52A8\u6863\u3002
ApplicationContext \u7EE7\u627F\u4E86 HierachicalBeanFactory \u548C ListableBeanFactory \u63A5\u53E3\uFF0C\u5728\u6B64\u57FA\u7840\u4E0A\uFF0C\u8FD8\u901A\u8FC7\u5176\u4ED6\u7684\u63A5\u53E3\u6269\u5C55\u4E86 BeanFactory \u7684\u529F\u80FD\uFF0C\u5305\u62EC\uFF1A
Bean instantiation/wiring
Bean \u7684\u5B9E\u4F8B\u5316/\u4E32\u8054
\u81EA\u52A8\u7684 BeanPostProcessor \u6CE8\u518C
\u81EA\u52A8\u7684 BeanFactoryPostProcessor \u6CE8\u518C
\u65B9\u4FBF\u7684 MessageSource \u8BBF\u95EE\uFF08i18n\uFF09
ApplicationEvent \u7684\u53D1\u5E03\u4E0E BeanFactory \u61D2\u52A0\u8F7D\u7684\u65B9\u5F0F\u4E0D\u540C\uFF0C\u5B83\u662F\u9884\u52A0\u8F7D\uFF0C\u6240\u4EE5\uFF0C\u6BCF\u4E00\u4E2A bean \u90FD\u5728 ApplicationContext \u542F\u52A8\u4E4B\u540E\u5B9E\u4F8B\u5316
\u8FD9\u662F ApplicationContext \u7684\u4F7F\u7528\u4F8B\u5B50\uFF1A
public class HelloWorldApp{
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
ApplicationContext \u5305\u542B BeanFactory \u7684\u6240\u6709\u7279\u6027\uFF0C\u901A\u5E38\u63A8\u8350\u4F7F\u7528\u524D\u8005\u3002
PS\uFF1A\u8FD9\u9053\u9898\u8001\u4E09\u9762\u8BD5\u88AB\u95EE\u5230\u8FC7
Spring \u7684 IOC \u5BB9\u5668\u5DE5\u4F5C\u7684\u8FC7\u7A0B\uFF0C\u5176\u5B9E\u53EF\u4EE5\u5212\u5206\u4E3A\u4E24\u4E2A\u9636\u6BB5\uFF1A\u5BB9\u5668\u542F\u52A8\u9636\u6BB5\u548CBean \u5B9E\u4F8B\u5316\u9636\u6BB5\u3002
\u5176\u4E2D\u5BB9\u5668\u542F\u52A8\u9636\u6BB5\u4E3B\u8981\u505A\u7684\u5DE5\u4F5C\u662F\u52A0\u8F7D\u548C\u89E3\u6790\u914D\u7F6E\u6587\u4EF6\uFF0C\u4FDD\u5B58\u5230\u5BF9\u5E94\u7684 Bean \u5B9A\u4E49\u4E2D\u3002
\u5BB9\u5668\u542F\u52A8\u5F00\u59CB\uFF0C\u9996\u5148\u4F1A\u901A\u8FC7\u67D0\u79CD\u9014\u5F84\u52A0\u8F7D Congiguration MetaData\uFF0C\u5728\u5927\u90E8\u5206\u60C5\u51B5\u4E0B\uFF0C\u5BB9\u5668\u9700\u8981\u4F9D\u8D56\u67D0\u4E9B\u5DE5\u5177\u7C7B\uFF08BeanDefinitionReader\uFF09\u5BF9\u52A0\u8F7D\u7684 Congiguration MetaData \u8FDB\u884C\u89E3\u6790\u548C\u5206\u6790\uFF0C\u5E76\u5C06\u5206\u6790\u540E\u7684\u4FE1\u606F\u7EC4\u4E3A\u76F8\u5E94\u7684 BeanDefinition\u3002
\u6700\u540E\u628A\u8FD9\u4E9B\u4FDD\u5B58\u4E86 Bean \u5B9A\u4E49\u5FC5\u8981\u4FE1\u606F\u7684 BeanDefinition\uFF0C\u6CE8\u518C\u5230\u76F8\u5E94\u7684 BeanDefinitionRegistry\uFF0C\u8FD9\u6837\u5BB9\u5668\u542F\u52A8\u5C31\u5B8C\u6210\u4E86\u3002
\u5728 Spring \u4E2D\uFF0C\u57FA\u672C\u5BB9\u5668 BeanFactory \u548C\u6269\u5C55\u5BB9\u5668 ApplicationContext \u7684\u5B9E\u4F8B\u5316\u65F6\u673A\u4E0D\u592A\u4E00\u6837\uFF0CBeanFactory \u91C7\u7528\u7684\u662F\u5EF6\u8FDF\u521D\u59CB\u5316\u7684\u65B9\u5F0F\uFF0C\u4E5F\u5C31\u662F\u53EA\u6709\u5728\u7B2C\u4E00\u6B21 getBean()\u7684\u65F6\u5019\uFF0C\u624D\u4F1A\u5B9E\u4F8B\u5316 Bean\uFF1BApplicationContext \u542F\u52A8\u4E4B\u540E\u4F1A\u5B9E\u4F8B\u5316\u6240\u6709\u7684 Bean \u5B9A\u4E49\u3002
Spring IOC \u4E2D Bean \u7684\u751F\u547D\u5468\u671F\u5927\u81F4\u5206\u4E3A\u56DB\u4E2A\u9636\u6BB5\uFF1A\u5B9E\u4F8B\u5316\uFF08Instantiation\uFF09\u3001\u5C5E\u6027\u8D4B\u503C\uFF08Populate\uFF09\u3001\u521D\u59CB\u5316\uFF08Initialization\uFF09\u3001\u9500\u6BC1\uFF08Destruction\uFF09\u3002
\u6211\u4EEC\u518D\u6765\u770B\u4E00\u4E2A\u7A0D\u5FAE\u8BE6\u7EC6\u4E00\u4E9B\u7684\u8FC7\u7A0B\uFF1A
\u7B80\u5355\u603B\u7ED3\u4E00\u4E0B\uFF0CBean \u751F\u547D\u5468\u671F\u91CC\u521D\u59CB\u5316\u7684\u8FC7\u7A0B\u76F8\u5BF9\u6B65\u9AA4\u4F1A\u591A\u4E00\u4E9B\uFF0C\u6BD4\u5982\u524D\u7F6E\u3001\u540E\u7F6E\u7684\u5904\u7406\u3002
\u6700\u540E\u901A\u8FC7\u4E00\u4E2A\u5B9E\u4F8B\u6765\u770B\u4E00\u4E0B\u5177\u4F53\u7684\u7EC6\u8282\uFF1A
PersonBean
\u7C7B\uFF0C\u5B9E\u73B0DisposableBean
,InitializingBean
, BeanFactoryAware
, BeanNameAware
\u8FD9 4 \u4E2A\u63A5\u53E3\uFF0C\u540C\u65F6\u8FD8\u6709\u81EA\u5B9A\u4E49\u7684init-method
\u548Cdestroy-method
\u3002public class PersonBean implements InitializingBean, BeanFactoryAware, BeanNameAware, DisposableBean {
/**
* \u8EAB\u4EFD\u8BC1\u53F7
*/
private Integer no;
/**
* \u59D3\u540D
*/
private String name;
public PersonBean() {
System.out.println("1.\u8C03\u7528\u6784\u9020\u65B9\u6CD5\uFF1A\u6211\u51FA\u751F\u4E86\uFF01");
}
public Integer getNo() {
return no;
}
public void setNo(Integer no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println("2.\u8BBE\u7F6E\u5C5E\u6027\uFF1A\u6211\u7684\u540D\u5B57\u53EB"+name);
}
@Override
public void setBeanName(String s) {
System.out.println("3.\u8C03\u7528BeanNameAware#setBeanName\u65B9\u6CD5:\u6211\u8981\u4E0A\u5B66\u4E86\uFF0C\u8D77\u4E86\u4E2A\u5B66\u540D");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("4.\u8C03\u7528BeanFactoryAware#setBeanFactory\u65B9\u6CD5\uFF1A\u9009\u597D\u5B66\u6821\u4E86");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("6.InitializingBean#afterPropertiesSet\u65B9\u6CD5\uFF1A\u5165\u5B66\u767B\u8BB0");
}
public void init() {
System.out.println("7.\u81EA\u5B9A\u4E49init\u65B9\u6CD5\uFF1A\u52AA\u529B\u4E0A\u5B66ing");
}
@Override
public void destroy() throws Exception {
System.out.println("9.DisposableBean#destroy\u65B9\u6CD5\uFF1A\u5E73\u6DE1\u7684\u4E00\u751F\u843D\u5E55\u4E86");
}
public void destroyMethod() {
System.out.println("10.\u81EA\u5B9A\u4E49destroy\u65B9\u6CD5:\u7761\u4E86\uFF0C\u522B\u60F3\u53EB\u9192\u6211");
}
public void work(){
System.out.println("Bean\u4F7F\u7528\u4E2D\uFF1A\u5DE5\u4F5C\uFF0C\u53EA\u6709\u5BF9\u793E\u4F1A\u6CA1\u6709\u7528\u7684\u4EBA\u624D\u653E\u5047\u3002\u3002");
}
}
MyBeanPostProcessor
\u5B9E\u73B0BeanPostProcessor
\u63A5\u53E3\u3002public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("5.BeanPostProcessor.postProcessBeforeInitialization\u65B9\u6CD5\uFF1A\u5230\u5B66\u6821\u62A5\u540D\u5566");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("8.BeanPostProcessor#postProcessAfterInitialization\u65B9\u6CD5\uFF1A\u7EC8\u4E8E\u6BD5\u4E1A\uFF0C\u62FF\u5230\u6BD5\u4E1A\u8BC1\u5566\uFF01");
return bean;
}
}
init-method
\u548Cdestroy-method
\u5C5E\u6027<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="myBeanPostProcessor" class="cn.fighter3.spring.life.MyBeanPostProcessor" />
<bean name="personBean" class="cn.fighter3.spring.life.PersonBean"
init-method="init" destroy-method="destroyMethod">
<property name="idNo" value= "80669865"/>
<property name="name" value="\u5F20\u94C1\u94A2" />
</bean>
</beans>
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
PersonBean personBean = (PersonBean) context.getBean("personBean");
personBean.work();
((ClassPathXmlApplicationContext) context).destroy();
}
}
1.\u8C03\u7528\u6784\u9020\u65B9\u6CD5\uFF1A\u6211\u51FA\u751F\u4E86\uFF01
2.\u8BBE\u7F6E\u5C5E\u6027\uFF1A\u6211\u7684\u540D\u5B57\u53EB\u5F20\u94C1\u94A2
3.\u8C03\u7528BeanNameAware#setBeanName\u65B9\u6CD5:\u6211\u8981\u4E0A\u5B66\u4E86\uFF0C\u8D77\u4E86\u4E2A\u5B66\u540D
4.\u8C03\u7528BeanFactoryAware#setBeanFactory\u65B9\u6CD5\uFF1A\u9009\u597D\u5B66\u6821\u4E86
5.BeanPostProcessor#postProcessBeforeInitialization\u65B9\u6CD5\uFF1A\u5230\u5B66\u6821\u62A5\u540D\u5566
6.InitializingBean#afterPropertiesSet\u65B9\u6CD5\uFF1A\u5165\u5B66\u767B\u8BB0
7.\u81EA\u5B9A\u4E49init\u65B9\u6CD5\uFF1A\u52AA\u529B\u4E0A\u5B66ing
8.BeanPostProcessor#postProcessAfterInitialization\u65B9\u6CD5\uFF1A\u7EC8\u4E8E\u6BD5\u4E1A\uFF0C\u62FF\u5230\u6BD5\u4E1A\u8BC1\u5566\uFF01
Bean\u4F7F\u7528\u4E2D\uFF1A\u5DE5\u4F5C\uFF0C\u53EA\u6709\u5BF9\u793E\u4F1A\u6CA1\u6709\u7528\u7684\u4EBA\u624D\u653E\u5047\u3002\u3002
9.DisposableBean#destroy\u65B9\u6CD5\uFF1A\u5E73\u6DE1\u7684\u4E00\u751F\u843D\u5E55\u4E86
10.\u81EA\u5B9A\u4E49destroy\u65B9\u6CD5:\u7761\u4E86\uFF0C\u522B\u60F3\u53EB\u9192\u6211
\u5173\u4E8E\u6E90\u7801\uFF0CBean \u521B\u5EFA\u8FC7\u7A0B\u53EF\u4EE5\u67E5\u770BAbstractBeanFactory#doGetBean
\u65B9\u6CD5\uFF0C\u5728\u8FD9\u4E2A\u65B9\u6CD5\u91CC\u53EF\u4EE5\u770B\u5230 Bean \u7684\u5B9E\u4F8B\u5316\uFF0C\u8D4B\u503C\u3001\u521D\u59CB\u5316\u7684\u8FC7\u7A0B\uFF0C\u81F3\u4E8E\u6700\u7EC8\u7684\u9500\u6BC1\uFF0C\u53EF\u4EE5\u770B\u770BConfigurableApplicationContext#close()
\u3002
\u6709\u4E09\u79CD\u65B9\u5F0F\uFF1A\u76F4\u63A5\u7F16\u7801\u65B9\u5F0F\u3001\u914D\u7F6E\u6587\u4EF6\u65B9\u5F0F\u3001\u6CE8\u89E3\u65B9\u5F0F\u3002
Spring \u652F\u6301\u6784\u9020\u65B9\u6CD5\u6CE8\u5165\u3001\u5C5E\u6027\u6CE8\u5165\u3001\u5DE5\u5382\u65B9\u6CD5\u6CE8\u5165,\u5176\u4E2D\u5DE5\u5382\u65B9\u6CD5\u6CE8\u5165\uFF0C\u53C8\u53EF\u4EE5\u5206\u4E3A\u9759\u6001\u5DE5\u5382\u65B9\u6CD5\u6CE8\u5165\u548C\u975E\u9759\u6001\u5DE5\u5382\u65B9\u6CD5\u6CE8\u5165\u3002
\u6784\u9020\u65B9\u6CD5\u6CE8\u5165
\u901A\u8FC7\u8C03\u7528\u7C7B\u7684\u6784\u9020\u65B9\u6CD5\uFF0C\u5C06\u63A5\u53E3\u5B9E\u73B0\u7C7B\u901A\u8FC7\u6784\u9020\u65B9\u6CD5\u53D8\u91CF\u4F20\u5165
public CatDaoImpl(String message){
this. message = message;
}
<bean id="CatDaoImpl" class="com.CatDaoImpl">
<constructor-arg value=" message "></constructor-arg>
</bean>
\u5C5E\u6027\u6CE8\u5165
\u901A\u8FC7 Setter \u65B9\u6CD5\u5B8C\u6210\u8C03\u7528\u7C7B\u6240\u9700\u4F9D\u8D56\u7684\u6CE8\u5165
public class Id {
private int id;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
}
<bean id="id" class="com.id ">
<property name="id" value="123"></property>
</bean>
\u5DE5\u5382\u65B9\u6CD5\u6CE8\u5165
\u9759\u6001\u5DE5\u5382\u6CE8\u5165
\u9759\u6001\u5DE5\u5382\u987E\u540D\u601D\u4E49\uFF0C\u5C31\u662F\u901A\u8FC7\u8C03\u7528\u9759\u6001\u5DE5\u5382\u7684\u65B9\u6CD5\u6765\u83B7\u53D6\u81EA\u5DF1\u9700\u8981\u7684\u5BF9\u8C61\uFF0C\u4E3A\u4E86\u8BA9 Spring \u7BA1\u7406\u6240\u6709\u5BF9\u8C61\uFF0C\u6211\u4EEC\u4E0D\u80FD\u76F4\u63A5\u901A\u8FC7"\u5DE5\u7A0B\u7C7B.\u9759\u6001\u65B9\u6CD5()"\u6765\u83B7\u53D6\u5BF9\u8C61\uFF0C\u800C\u662F\u4F9D\u7136\u901A\u8FC7 Spring \u6CE8\u5165\u7684\u5F62\u5F0F\u83B7\u53D6\uFF1A
public class DaoFactory { //\u9759\u6001\u5DE5\u5382
public static final FactoryDao getStaticFactoryDaoImpl(){
return new StaticFacotryDaoImpl();
}
}
public class SpringAction {
//\u6CE8\u5165\u5BF9\u8C61
private FactoryDao staticFactoryDao;
//\u6CE8\u5165\u5BF9\u8C61\u7684 set \u65B9\u6CD5
public void setStaticFactoryDao(FactoryDao staticFactoryDao) {
this.staticFactoryDao = staticFactoryDao;
}
}
//factory-method="getStaticFactoryDaoImpl"\u6307\u5B9A\u8C03\u7528\u54EA\u4E2A\u5DE5\u5382\u65B9\u6CD5
<bean name="springAction" class=" SpringAction" >
<!--\u4F7F\u7528\u9759\u6001\u5DE5\u5382\u7684\u65B9\u6CD5\u6CE8\u5165\u5BF9\u8C61,\u5BF9\u5E94\u4E0B\u9762\u7684\u914D\u7F6E\u6587\u4EF6-->
<property name="staticFactoryDao" ref="staticFactoryDao"></property>
</bean>
<!--\u6B64\u5904\u83B7\u53D6\u5BF9\u8C61\u7684\u65B9\u5F0F\u662F\u4ECE\u5DE5\u5382\u7C7B\u4E2D\u83B7\u53D6\u9759\u6001\u65B9\u6CD5-->
<bean name="staticFactoryDao" class="DaoFactory"
factory-method="getStaticFactoryDaoImpl"></bean>
\u975E\u9759\u6001\u5DE5\u5382\u6CE8\u5165
\u975E\u9759\u6001\u5DE5\u5382\uFF0C\u4E5F\u53EB\u5B9E\u4F8B\u5DE5\u5382\uFF0C\u610F\u601D\u662F\u5DE5\u5382\u65B9\u6CD5\u4E0D\u662F\u9759\u6001\u7684\uFF0C\u6240\u4EE5\u6211\u4EEC\u9700\u8981\u9996\u5148 new \u4E00\u4E2A\u5DE5\u5382\u5B9E\u4F8B\uFF0C\u518D\u8C03\u7528\u666E\u901A\u7684\u5B9E\u4F8B\u65B9\u6CD5\u3002
//\u975E\u9759\u6001\u5DE5\u5382
public class DaoFactory {
public FactoryDao getFactoryDaoImpl(){
return new FactoryDaoImpl();
}
}
public class SpringAction {
//\u6CE8\u5165\u5BF9\u8C61
private FactoryDao factoryDao;
public void setFactoryDao(FactoryDao factoryDao) {
this.factoryDao = factoryDao;
}
}
<bean name="springAction" class="SpringAction">
<!--\u4F7F\u7528\u975E\u9759\u6001\u5DE5\u5382\u7684\u65B9\u6CD5\u6CE8\u5165\u5BF9\u8C61,\u5BF9\u5E94\u4E0B\u9762\u7684\u914D\u7F6E\u6587\u4EF6-->
<property name="factoryDao" ref="factoryDao"></property>
</bean>
<!--\u6B64\u5904\u83B7\u53D6\u5BF9\u8C61\u7684\u65B9\u5F0F\u662F\u4ECE\u5DE5\u5382\u7C7B\u4E2D\u83B7\u53D6\u5B9E\u4F8B\u65B9\u6CD5-->
<bean name="daoFactory" class="com.DaoFactory"></bean>
<bean name="factoryDao" factory-bean="daoFactory" factory-method="getFactoryDaoImpl"></bean>
\u4EC0\u4E48\u662F\u81EA\u52A8\u88C5\u914D\uFF1F
Spring IOC \u5BB9\u5668\u77E5\u9053\u6240\u6709 Bean \u7684\u914D\u7F6E\u4FE1\u606F\uFF0C\u6B64\u5916\uFF0C\u901A\u8FC7 Java \u53CD\u5C04\u673A\u5236\u8FD8\u53EF\u4EE5\u83B7\u77E5\u5B9E\u73B0\u7C7B\u7684\u7ED3\u6784\u4FE1\u606F\uFF0C\u5982\u6784\u9020\u65B9\u6CD5\u7684\u7ED3\u6784\u3001\u5C5E\u6027\u7B49\u4FE1\u606F\u3002\u638C\u63E1\u6240\u6709 Bean \u7684\u8FD9\u4E9B\u4FE1\u606F\u540E\uFF0CSpring IOC \u5BB9\u5668\u5C31\u53EF\u4EE5\u6309\u7167\u67D0\u79CD\u89C4\u5219\u5BF9\u5BB9\u5668\u4E2D\u7684 Bean \u8FDB\u884C\u81EA\u52A8\u88C5\u914D\uFF0C\u800C\u65E0\u987B\u901A\u8FC7\u663E\u5F0F\u7684\u65B9\u5F0F\u8FDB\u884C\u4F9D\u8D56\u914D\u7F6E\u3002
Spring \u63D0\u4F9B\u7684\u8FD9\u79CD\u65B9\u5F0F\uFF0C\u53EF\u4EE5\u6309\u7167\u67D0\u4E9B\u89C4\u5219\u8FDB\u884C Bean \u7684\u81EA\u52A8\u88C5\u914D\uFF0C<bean>
\u5143\u7D20\u63D0\u4F9B\u4E86\u4E00\u4E2A\u6307\u5B9A\u81EA\u52A8\u88C5\u914D\u7C7B\u578B\u7684\u5C5E\u6027\uFF1Aautowire="<\u81EA\u52A8\u88C5\u914D\u7C7B\u578B>"
Spring \u63D0\u4F9B\u4E86\u54EA\u51E0\u79CD\u81EA\u52A8\u88C5\u914D\u7C7B\u578B\uFF1F
Spring \u63D0\u4F9B\u4E86 4 \u79CD\u81EA\u52A8\u88C5\u914D\u7C7B\u578B\uFF1A
Spring \u7684 Bean \u4E3B\u8981\u652F\u6301\u4E94\u79CD\u4F5C\u7528\u57DF\uFF1A
\u4EE5\u4E0B\u4E09\u4E2A\u4F5C\u7528\u57DF\u4E8E\u53EA\u5728 Web \u5E94\u7528\u4E2D\u9002\u7528\uFF1A
\u9996\u5148\u7ED3\u8BBA\u5728\u8FD9\uFF1ASpring \u4E2D\u7684\u5355\u4F8B Bean\u4E0D\u662F\u7EBF\u7A0B\u5B89\u5168\u7684\u3002
\u56E0\u4E3A\u5355\u4F8B Bean\uFF0C\u662F\u5168\u5C40\u53EA\u6709\u4E00\u4E2A Bean\uFF0C\u6240\u6709\u7EBF\u7A0B\u5171\u4EAB\u3002\u5982\u679C\u8BF4\u5355\u4F8B Bean\uFF0C\u662F\u4E00\u4E2A\u65E0\u72B6\u6001\u7684\uFF0C\u4E5F\u5C31\u662F\u7EBF\u7A0B\u4E2D\u7684\u64CD\u4F5C\u4E0D\u4F1A\u5BF9 Bean \u4E2D\u7684\u6210\u5458\u53D8\u91CF\u6267\u884C\u67E5\u8BE2\u4EE5\u5916\u7684\u64CD\u4F5C\uFF0C\u90A3\u4E48\u8FD9\u4E2A\u5355\u4F8B Bean \u662F\u7EBF\u7A0B\u5B89\u5168\u7684\u3002\u6BD4\u5982 Spring mvc \u7684 Controller\u3001Service\u3001Dao \u7B49\uFF0C\u8FD9\u4E9B Bean \u5927\u591A\u662F\u65E0\u72B6\u6001\u7684\uFF0C\u53EA\u5173\u6CE8\u4E8E\u65B9\u6CD5\u672C\u8EAB\u3002
\u5047\u5982\u8FD9\u4E2A Bean \u662F\u6709\u72B6\u6001\u7684\uFF0C\u4E5F\u5C31\u662F\u4F1A\u5BF9 Bean \u4E2D\u7684\u6210\u5458\u53D8\u91CF\u8FDB\u884C\u5199\u64CD\u4F5C\uFF0C\u90A3\u4E48\u53EF\u80FD\u5C31\u5B58\u5728\u7EBF\u7A0B\u5B89\u5168\u7684\u95EE\u9898\u3002
\u5355\u4F8B Bean \u7EBF\u7A0B\u5B89\u5168\u95EE\u9898\u600E\u4E48\u89E3\u51B3\u5462\uFF1F
\u5E38\u89C1\u7684\u6709\u8FD9\u4E48\u4E9B\u89E3\u51B3\u529E\u6CD5\uFF1A
\u5C06 Bean \u5B9A\u4E49\u4E3A\u591A\u4F8B
\u8FD9\u6837\u6BCF\u4E00\u4E2A\u7EBF\u7A0B\u8BF7\u6C42\u8FC7\u6765\u90FD\u4F1A\u521B\u5EFA\u4E00\u4E2A\u65B0\u7684 Bean\uFF0C\u4F46\u662F\u8FD9\u6837\u5BB9\u5668\u5C31\u4E0D\u597D\u7BA1\u7406 Bean\uFF0C\u4E0D\u80FD\u8FD9\u4E48\u529E\u3002
\u5728 Bean \u5BF9\u8C61\u4E2D\u5C3D\u91CF\u907F\u514D\u5B9A\u4E49\u53EF\u53D8\u7684\u6210\u5458\u53D8\u91CF
\u524A\u8DB3\u9002\u5C65\u4E86\u5C5E\u4E8E\u662F\uFF0C\u4E5F\u4E0D\u80FD\u8FD9\u4E48\u5E72\u3002
\u5C06 Bean \u4E2D\u7684\u6210\u5458\u53D8\u91CF\u4FDD\u5B58\u5728 ThreadLocal \u4E2D \u2B50
\u6211\u4EEC\u77E5\u9053 ThredLoca \u80FD\u4FDD\u8BC1\u591A\u7EBF\u7A0B\u4E0B\u53D8\u91CF\u7684\u9694\u79BB\uFF0C\u53EF\u4EE5\u5728\u7C7B\u4E2D\u5B9A\u4E49\u4E00\u4E2A ThreadLocal \u6210\u5458\u53D8\u91CF\uFF0C\u5C06\u9700\u8981\u7684\u53EF\u53D8\u6210\u5458\u53D8\u91CF\u4FDD\u5B58\u5728 ThreadLocal \u91CC\uFF0C\u8FD9\u662F\u63A8\u8350\u7684\u4E00\u79CD\u65B9\u5F0F\u3002
\u4EC0\u4E48\u662F\u5FAA\u73AF\u4F9D\u8D56\uFF1F
Spring \u5FAA\u73AF\u4F9D\u8D56\uFF1A\u7B80\u5355\u8BF4\u5C31\u662F\u81EA\u5DF1\u4F9D\u8D56\u81EA\u5DF1\uFF0C\u6216\u8005\u548C\u522B\u7684 Bean \u76F8\u4E92\u4F9D\u8D56\u3002
\u53EA\u6709\u5355\u4F8B\u7684 Bean \u624D\u5B58\u5728\u5FAA\u73AF\u4F9D\u8D56\u7684\u60C5\u51B5\uFF0C\u539F\u578B(Prototype)\u60C5\u51B5\u4E0B\uFF0CSpring \u4F1A\u76F4\u63A5\u629B\u51FA\u5F02\u5E38\u3002\u539F\u56E0\u5F88\u7B80\u5355\uFF0CAB \u5FAA\u73AF\u4F9D\u8D56\uFF0CA \u5B9E\u4F8B\u5316\u7684\u65F6\u5019\uFF0C\u53D1\u73B0\u4F9D\u8D56 B\uFF0C\u521B\u5EFA B \u5B9E\u4F8B\uFF0C\u521B\u5EFA B \u7684\u65F6\u5019\u53D1\u73B0\u9700\u8981 A\uFF0C\u521B\u5EFA A1 \u5B9E\u4F8B\u2026\u2026\u65E0\u9650\u5957\u5A03\uFF0C\u76F4\u63A5\u628A\u7CFB\u7EDF\u5E72\u57AE\u3002
Spring \u53EF\u4EE5\u89E3\u51B3\u54EA\u4E9B\u60C5\u51B5\u7684\u5FAA\u73AF\u4F9D\u8D56\uFF1F
Spring \u4E0D\u652F\u6301\u57FA\u4E8E\u6784\u9020\u5668\u6CE8\u5165\u7684\u5FAA\u73AF\u4F9D\u8D56\uFF0C\u4F46\u662F\u5047\u5982 AB \u5FAA\u73AF\u4F9D\u8D56\uFF0C\u5982\u679C\u4E00\u4E2A\u662F\u6784\u9020\u5668\u6CE8\u5165\uFF0C\u4E00\u4E2A\u662F setter \u6CE8\u5165\u5462\uFF1F
\u770B\u770B\u51E0\u79CD\u60C5\u5F62\uFF1A
\u7B2C\u56DB\u79CD\u53EF\u4EE5\u800C\u7B2C\u4E94\u79CD\u4E0D\u53EF\u4EE5\u7684\u539F\u56E0\u662F Spring \u5728\u521B\u5EFA Bean \u65F6\u9ED8\u8BA4\u4F1A\u6839\u636E\u81EA\u7136\u6392\u5E8F\u8FDB\u884C\u521B\u5EFA\uFF0C\u6240\u4EE5 A \u4F1A\u5148\u4E8E B \u8FDB\u884C\u521B\u5EFA\u3002
\u6240\u4EE5\u7B80\u5355\u603B\u7ED3\uFF0C\u5F53\u5FAA\u73AF\u4F9D\u8D56\u7684\u5B9E\u4F8B\u90FD\u91C7\u7528 setter \u65B9\u6CD5\u6CE8\u5165\u7684\u65F6\u5019\uFF0CSpring \u53EF\u4EE5\u652F\u6301\uFF0C\u90FD\u91C7\u7528\u6784\u9020\u5668\u6CE8\u5165\u7684\u65F6\u5019\uFF0C\u4E0D\u652F\u6301\uFF0C\u6784\u9020\u5668\u6CE8\u5165\u548C setter \u6CE8\u5165\u540C\u65F6\u5B58\u5728\u7684\u65F6\u5019\uFF0C\u770B\u5929\u3002
PS\uFF1A\u5176\u5B9E\u6B63\u786E\u7B54\u6848\u662F\u5F00\u53D1\u4EBA\u5458\u505A\u597D\u8BBE\u8BA1\uFF0C\u522B\u8BA9 Bean \u5FAA\u73AF\u4F9D\u8D56\uFF0C\u4F46\u662F\u6CA1\u529E\u6CD5\uFF0C\u9762\u8BD5\u5B98\u4E0D\u60F3\u542C\u8FD9\u4E2A\u3002
\u6211\u4EEC\u90FD\u77E5\u9053\uFF0C\u5355\u4F8B Bean \u521D\u59CB\u5316\u5B8C\u6210\uFF0C\u8981\u7ECF\u5386\u4E09\u6B65\uFF1A
\u6CE8\u5165\u5C31\u53D1\u751F\u5728\u7B2C\u4E8C\u6B65\uFF0C\u5C5E\u6027\u8D4B\u503C\uFF0C\u7ED3\u5408\u8FD9\u4E2A\u8FC7\u7A0B\uFF0CSpring \u901A\u8FC7\u4E09\u7EA7\u7F13\u5B58\u89E3\u51B3\u4E86\u5FAA\u73AF\u4F9D\u8D56\uFF1A
Map<String,Object>
singletonObjects\uFF0C\u5355\u4F8B\u6C60\uFF0C\u7528\u4E8E\u4FDD\u5B58\u5B9E\u4F8B\u5316\u3001\u5C5E\u6027\u8D4B\u503C\uFF08\u6CE8\u5165\uFF09\u3001\u521D\u59CB\u5316\u5B8C\u6210\u7684 bean \u5B9E\u4F8BMap<String,Object>
earlySingletonObjects\uFF0C\u65E9\u671F\u66DD\u5149\u5BF9\u8C61\uFF0C\u7528\u4E8E\u4FDD\u5B58\u5B9E\u4F8B\u5316\u5B8C\u6210\u7684 bean \u5B9E\u4F8BMap<String,ObjectFactory<?>>
singletonFactories\uFF0C\u65E9\u671F\u66DD\u5149\u5BF9\u8C61\u5DE5\u5382\uFF0C\u7528\u4E8E\u4FDD\u5B58 bean \u521B\u5EFA\u5DE5\u5382\uFF0C\u4EE5\u4FBF\u4E8E\u540E\u9762\u6269\u5C55\u6709\u673A\u4F1A\u521B\u5EFA\u4EE3\u7406\u5BF9\u8C61\u3002\u6211\u4EEC\u6765\u770B\u4E00\u4E0B\u4E09\u7EA7\u7F13\u5B58\u89E3\u51B3\u5FAA\u73AF\u4F9D\u8D56\u7684\u8FC7\u7A0B\uFF1A
\u5F53 A\u3001B \u4E24\u4E2A\u7C7B\u53D1\u751F\u5FAA\u73AF\u4F9D\u8D56\u65F6\uFF1A
A \u5B9E\u4F8B\u7684\u521D\u59CB\u5316\u8FC7\u7A0B\uFF1A
A \u6CE8\u2F0A\u5C5E\u6027\u65F6\uFF0C\u53D1\u73B0\u4F9D\u8D56 B\uFF0C\u6B64\u65F6 B \u8FD8\u6CA1\u6709\u88AB\u521B\u5EFA\u51FA\u6765\uFF0C\u6240\u4EE5\u53BB\u5B9E\u4F8B\u5316 B
\u540C\u6837\uFF0CB \u6CE8\u2F0A\u5C5E\u6027\u65F6\u53D1\u73B0\u4F9D\u8D56 A\uFF0C\u5B83\u5C31\u4F1A\u4ECE\u7F13\u5B58\u91CC\u627E A \u5BF9\u8C61\u3002\u4F9D\u6B21\u4ECE\u2F00\u7EA7\u5230\u4E09\u7EA7\u7F13\u5B58\u67E5\u8BE2 A\uFF0C\u4ECE\u4E09\u7EA7\u7F13\u5B58\u901A\u8FC7\u5BF9\u8C61\u2F2F\u2F1A\u62FF\u5230 A\uFF0C\u53D1\u73B0 A \u867D\u7136\u4E0D\u592A\u5B8C\u5584\uFF0C\u4F46\u662F\u5B58\u5728\uFF0C\u628A A \u653E\u2F0A\u2F06\u7EA7\u7F13\u5B58\uFF0C\u540C\u65F6\u5220\u9664\u4E09\u7EA7\u7F13\u5B58\u4E2D\u7684 A\uFF0C\u6B64\u65F6\uFF0CB \u5DF2\u7ECF\u5B9E\u4F8B\u5316\u5E76\u4E14\u521D\u59CB\u5316\u5B8C\u6210\uFF0C\u628A B \u653E\u5165\u2F00\u7EA7\u7F13\u5B58\u3002
\u63A5\u7740 A \u7EE7\u7EED\u5C5E\u6027\u8D4B\u503C\uFF0C\u987A\u5229\u4ECE\u2F00\u7EA7\u7F13\u5B58\u62FF\u5230\u5B9E\u4F8B\u5316\u4E14\u521D\u59CB\u5316\u5B8C\u6210\u7684 B \u5BF9\u8C61\uFF0CA \u5BF9\u8C61\u521B\u5EFA\u4E5F\u5B8C\u6210\uFF0C\u5220\u9664\u2F06\u7EA7\u7F13\u5B58\u4E2D\u7684 A\uFF0C\u540C\u65F6\u628A A \u653E\u2F0A\u2F00\u7EA7\u7F13\u5B58
\u6700\u540E\uFF0C\u2F00\u7EA7\u7F13\u5B58\u4E2D\u4FDD\u5B58\u7740\u5B9E\u4F8B\u5316\u3001\u521D\u59CB\u5316\u90FD\u5B8C\u6210\u7684 A\u3001B \u5BF9\u8C61
\u6240\u4EE5\uFF0C\u6211\u4EEC\u5C31\u77E5\u9053\u4E3A\u4EC0\u4E48 Spring \u80FD\u89E3\u51B3 setter \u6CE8\u5165\u7684\u5FAA\u73AF\u4F9D\u8D56\u4E86\uFF0C\u56E0\u4E3A\u5B9E\u4F8B\u5316\u548C\u5C5E\u6027\u8D4B\u503C\u662F\u5206\u5F00\u7684\uFF0C\u6240\u4EE5\u91CC\u9762\u6709\u64CD\u4F5C\u7684\u7A7A\u95F4\u3002\u5982\u679C\u90FD\u662F\u6784\u9020\u5668\u6CE8\u5165\u7684\u5316\uFF0C\u90A3\u4E48\u90FD\u5F97\u5728\u5B9E\u4F8B\u5316\u8FD9\u4E00\u6B65\u5B8C\u6210\u6CE8\u5165\uFF0C\u6240\u4EE5\u81EA\u7136\u662F\u65E0\u6CD5\u652F\u6301\u4E86\u3002
\u4E0D\u884C\uFF0C\u4E3B\u8981\u662F\u4E3A\u4E86\u2F63\u6210\u4EE3\u7406\u5BF9\u8C61\u3002\u5982\u679C\u662F\u6CA1\u6709\u4EE3\u7406\u7684\u60C5\u51B5\u4E0B\uFF0C\u4F7F\u7528\u4E8C\u7EA7\u7F13\u5B58\u89E3\u51B3\u5FAA\u73AF\u4F9D\u8D56\u4E5F\u662F OK \u7684\u3002\u4F46\u662F\u5982\u679C\u5B58\u5728\u4EE3\u7406\uFF0C\u4E09\u7EA7\u6CA1\u6709\u95EE\u9898\uFF0C\u4E8C\u7EA7\u5C31\u4E0D\u884C\u4E86\u3002
\u56E0\u4E3A\u4E09\u7EA7\u7F13\u5B58\u4E2D\u653E\u7684\u662F\u2F63\u6210\u5177\u4F53\u5BF9\u8C61\u7684\u533F\u540D\u5185\u90E8\u7C7B\uFF0C\u83B7\u53D6 Object \u7684\u65F6\u5019\uFF0C\u5B83\u53EF\u4EE5\u2F63\u6210\u4EE3\u7406\u5BF9\u8C61\uFF0C\u4E5F\u53EF\u4EE5\u8FD4\u56DE\u666E\u901A\u5BF9\u8C61\u3002\u4F7F\u2F64\u4E09\u7EA7\u7F13\u5B58\u4E3B\u8981\u662F\u4E3A\u4E86\u4FDD\u8BC1\u4E0D\u7BA1\u4EC0\u4E48\u65F6\u5019\u4F7F\u2F64\u7684\u90FD\u662F\u2F00\u4E2A\u5BF9\u8C61\u3002
\u5047\u8BBE\u53EA\u6709\u2F06\u7EA7\u7F13\u5B58\u7684\u60C5\u51B5\uFF0C\u5F80\u2F06\u7EA7\u7F13\u5B58\u4E2D\u653E\u7684\u663E\u793A\u2F00\u4E2A\u666E\u901A\u7684 Bean \u5BF9\u8C61\uFF0CBean \u521D\u59CB\u5316\u8FC7\u7A0B\u4E2D\uFF0C\u901A\u8FC7 BeanPostProcessor \u53BB\u2F63\u6210\u4EE3\u7406\u5BF9\u8C61\u4E4B\u540E\uFF0C\u8986\u76D6\u6389\u2F06\u7EA7\u7F13\u5B58\u4E2D\u7684\u666E\u901A Bean \u5BF9\u8C61\uFF0C\u90A3\u4E48\u53EF\u80FD\u5C31\u5BFC\u81F4\u53D6\u5230\u7684 Bean \u5BF9\u8C61\u4E0D\u4E00\u81F4\u4E86\u3002
\u5B9E\u73B0@Autowired \u7684\u5173\u952E\u662F\uFF1AAutowiredAnnotationBeanPostProcessor
\u5728 Bean \u7684\u521D\u59CB\u5316\u9636\u6BB5\uFF0C\u4F1A\u901A\u8FC7 Bean \u540E\u7F6E\u5904\u7406\u5668\u6765\u8FDB\u884C\u4E00\u4E9B\u524D\u7F6E\u548C\u540E\u7F6E\u7684\u5904\u7406\u3002
\u5B9E\u73B0@Autowired \u7684\u529F\u80FD\uFF0C\u4E5F\u662F\u901A\u8FC7\u540E\u7F6E\u5904\u7406\u5668\u6765\u5B8C\u6210\u7684\u3002\u8FD9\u4E2A\u540E\u7F6E\u5904\u7406\u5668\u5C31\u662F AutowiredAnnotationBeanPostProcessor\u3002
Spring \u5728\u521B\u5EFA bean \u7684\u8FC7\u7A0B\u4E2D\uFF0C\u6700\u7EC8\u4F1A\u8C03\u7528\u5230 doCreateBean()\u65B9\u6CD5\uFF0C\u5728 doCreateBean()\u65B9\u6CD5\u4E2D\u4F1A\u8C03\u7528 populateBean()\u65B9\u6CD5\uFF0C\u6765\u4E3A bean \u8FDB\u884C\u5C5E\u6027\u586B\u5145\uFF0C\u5B8C\u6210\u81EA\u52A8\u88C5\u914D\u7B49\u5DE5\u4F5C\u3002
\u5728 populateBean()\u65B9\u6CD5\u4E2D\u4E00\u5171\u8C03\u7528\u4E86\u4E24\u6B21\u540E\u7F6E\u5904\u7406\u5668\uFF0C\u7B2C\u4E00\u6B21\u662F\u4E3A\u4E86\u5224\u65AD\u662F\u5426\u9700\u8981\u5C5E\u6027\u586B\u5145\uFF0C\u5982\u679C\u4E0D\u9700\u8981\u8FDB\u884C\u5C5E\u6027\u586B\u5145\uFF0C\u90A3\u4E48\u5C31\u4F1A\u76F4\u63A5\u8FDB\u884C return\uFF0C\u5982\u679C\u9700\u8981\u8FDB\u884C\u5C5E\u6027\u586B\u5145\uFF0C\u90A3\u4E48\u65B9\u6CD5\u5C31\u4F1A\u7EE7\u7EED\u5411\u4E0B\u6267\u884C\uFF0C\u540E\u9762\u4F1A\u8FDB\u884C\u7B2C\u4E8C\u6B21\u540E\u7F6E\u5904\u7406\u5668\u7684\u8C03\u7528\uFF0C\u8FD9\u4E2A\u65F6\u5019\uFF0C\u5C31\u4F1A\u8C03\u7528\u5230 AutowiredAnnotationBeanPostProcessor \u7684 postProcessPropertyValues()\u65B9\u6CD5\uFF0C\u5728\u8BE5\u65B9\u6CD5\u4E2D\u5C31\u4F1A\u8FDB\u884C@Autowired \u6CE8\u89E3\u7684\u89E3\u6790\uFF0C\u7136\u540E\u5B9E\u73B0\u81EA\u52A8\u88C5\u914D\u3002
/**
* \u5C5E\u6027\u8D4B\u503C
**/
protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
//\u2026\u2026\u2026\u2026
if (hasInstAwareBpps) {
if (pvs == null) {
pvs = mbd.getPropertyValues();
}
PropertyValues pvsToUse;
for(Iterator var9 = this.getBeanPostProcessorCache().instantiationAware.iterator(); var9.hasNext(); pvs = pvsToUse) {
InstantiationAwareBeanPostProcessor bp = (InstantiationAwareBeanPostProcessor)var9.next();
pvsToUse = bp.postProcessProperties((PropertyValues)pvs, bw.getWrappedInstance(), beanName);
if (pvsToUse == null) {
if (filteredPds == null) {
filteredPds = this.filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
}
//\u6267\u884C\u540E\u5904\u7406\u5668\uFF0C\u586B\u5145\u5C5E\u6027\uFF0C\u5B8C\u6210\u81EA\u52A8\u88C5\u914D
//\u8C03\u7528InstantiationAwareBeanPostProcessor\u7684postProcessPropertyValues()\u65B9\u6CD5
pvsToUse = bp.postProcessPropertyValues((PropertyValues)pvs, filteredPds, bw.getWrappedInstance(), beanName);
if (pvsToUse == null) {
return;
}
}
}
}
//\u2026\u2026\u2026\u2026
}
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {
//@Autowired\u6CE8\u89E3\u3001@Inject\u548C@Value\u6CE8\u89E3\u7684\u5C5E\u6027\u548C\u65B9\u6CD5
InjectionMetadata metadata = this.findAutowiringMetadata(beanName, bean.getClass(), pvs);
try {
//\u5C5E\u6027\u586B\u5145
metadata.inject(bean, beanName, pvs);
return pvs;
} catch (BeanCreationException var6) {
throw var6;
} catch (Throwable var7) {
throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", var7);
}
}
AOP\uFF1A\u9762\u5411\u5207\u9762\u7F16\u7A0B\u3002\u7B80\u5355\u8BF4\uFF0C\u5C31\u662F\u628A\u4E00\u4E9B\u4E1A\u52A1\u903B\u8F91\u4E2D\u7684\u76F8\u540C\u7684\u4EE3\u7801\u62BD\u53D6\u5230\u4E00\u4E2A\u72EC\u7ACB\u7684\u6A21\u5757\u4E2D\uFF0C\u8BA9\u4E1A\u52A1\u903B\u8F91\u66F4\u52A0\u6E05\u723D\u3002
\u5177\u4F53\u6765\u8BF4\uFF0C\u5047\u5982\u6211\u73B0\u5728\u8981 crud \u5199\u4E00\u5806\u4E1A\u52A1\uFF0C\u53EF\u662F\u5982\u4F55\u4E1A\u52A1\u4EE3\u7801\u524D\u540E\u524D\u540E\u8FDB\u884C\u6253\u5370\u65E5\u5FD7\u548C\u53C2\u6570\u7684\u6821\u9A8C\u5462\uFF1F
\u6211\u4EEC\u53EF\u4EE5\u628A\u65E5\u5FD7\u8BB0\u5F55
\u548C\u6570\u636E\u6821\u9A8C
\u53EF\u91CD\u7528\u7684\u529F\u80FD\u6A21\u5757\u5206\u79BB\u51FA\u6765\uFF0C\u7136\u540E\u5728\u7A0B\u5E8F\u7684\u6267\u884C\u7684\u5408\u9002\u7684\u5730\u65B9\u52A8\u6001\u5730\u690D\u5165\u8FD9\u4E9B\u4EE3\u7801\u5E76\u6267\u884C\u3002\u8FD9\u6837\u5C31\u7B80\u5316\u4E86\u4EE3\u7801\u7684\u4E66\u5199\u3002
\u4E1A\u52A1\u903B\u8F91\u4EE3\u7801\u4E2D\u6CA1\u6709\u53C2\u548C\u901A\u7528\u903B\u8F91\u7684\u4EE3\u7801\uFF0C\u4E1A\u52A1\u6A21\u5757\u66F4\u7B80\u6D01\uFF0C\u53EA\u5305\u542B\u6838\u5FC3\u4E1A\u52A1\u4EE3\u7801\u3002\u5B9E\u73B0\u4E86\u4E1A\u52A1\u903B\u8F91\u548C\u901A\u7528\u903B\u8F91\u7684\u4EE3\u7801\u5206\u79BB\uFF0C\u4FBF\u4E8E\u7EF4\u62A4\u548C\u5347\u7EA7\uFF0C\u964D\u4F4E\u4E86\u4E1A\u52A1\u903B\u8F91\u548C\u901A\u7528\u903B\u8F91\u7684\u8026\u5408\u6027\u3002
AOP \u53EF\u4EE5\u5C06\u904D\u5E03\u5E94\u7528\u5404\u5904\u7684\u529F\u80FD\u5206\u79BB\u51FA\u6765\u5F62\u6210\u53EF\u91CD\u7528\u7684\u7EC4\u4EF6\u3002\u5728\u7F16\u8BD1\u671F\u95F4\u3001\u88C5\u8F7D\u671F\u95F4\u6216\u8FD0\u884C\u671F\u95F4\u5B9E\u73B0\u5728\u4E0D\u4FEE\u6539\u6E90\u4EE3\u7801\u7684\u60C5\u51B5\u4E0B\u7ED9\u7A0B\u5E8F\u52A8\u6001\u6DFB\u52A0\u529F\u80FD\u3002\u4ECE\u800C\u5B9E\u73B0\u5BF9\u4E1A\u52A1\u903B\u8F91\u7684\u9694\u79BB\uFF0C\u63D0\u9AD8\u4EE3\u7801\u7684\u6A21\u5757\u5316\u80FD\u529B\u3002
AOP \u7684\u6838\u5FC3\u5176\u5B9E\u5C31\u662F\u52A8\u6001\u4EE3\u7406\uFF0C\u5982\u679C\u662F\u5B9E\u73B0\u4E86\u63A5\u53E3\u7684\u8BDD\u5C31\u4F1A\u4F7F\u7528 JDK \u52A8\u6001\u4EE3\u7406\uFF0C\u5426\u5219\u4F7F\u7528 CGLIB \u4EE3\u7406\uFF0C\u4E3B\u8981\u5E94\u7528\u4E8E\u5904\u7406\u4E00\u4E9B\u5177\u6709\u6A2A\u5207\u6027\u8D28\u7684\u7CFB\u7EDF\u7EA7\u670D\u52A1\uFF0C\u5982\u65E5\u5FD7\u6536\u96C6\u3001\u4E8B\u52A1\u7BA1\u7406\u3001\u5B89\u5168\u68C0\u67E5\u3001\u7F13\u5B58\u3001\u5BF9\u8C61\u6C60\u7BA1\u7406\u7B49\u3002
AOP \u6709\u54EA\u4E9B\u6838\u5FC3\u6982\u5FF5\uFF1F
\u5207\u9762\uFF08Aspect\uFF09\uFF1A\u7C7B\u662F\u5BF9\u7269\u4F53\u7279\u5F81\u7684\u62BD\u8C61\uFF0C\u5207\u9762\u5C31\u662F\u5BF9\u6A2A\u5207\u5173\u6CE8\u70B9\u7684\u62BD\u8C61
\u8FDE\u63A5\u70B9\uFF08Joinpoint\uFF09\uFF1A\u88AB\u62E6\u622A\u5230\u7684\u70B9\uFF0C\u56E0\u4E3A Spring \u53EA\u652F\u6301\u65B9\u6CD5\u7C7B\u578B\u7684\u8FDE\u63A5\u70B9\uFF0C\u6240\u4EE5\u5728 Spring \u4E2D\u8FDE\u63A5\u70B9\u6307\u7684\u5C31\u662F\u88AB\u62E6\u622A\u5230\u7684\u65B9\u6CD5\uFF0C\u5B9E\u9645\u4E0A\u8FDE\u63A5\u70B9\u8FD8\u53EF\u4EE5\u662F\u5B57\u6BB5\u6216\u8005\u6784\u9020\u5668
\u5207\u70B9\uFF08Pointcut\uFF09\uFF1A\u5BF9\u8FDE\u63A5\u70B9\u8FDB\u884C\u62E6\u622A\u7684\u5B9A\u4F4D
\u901A\u77E5\uFF08Advice\uFF09\uFF1A\u6240\u8C13\u901A\u77E5\u6307\u7684\u5C31\u662F\u6307\u62E6\u622A\u5230\u8FDE\u63A5\u70B9\u4E4B\u540E\u8981\u6267\u884C\u7684\u4EE3\u7801\uFF0C\u4E5F\u53EF\u4EE5\u79F0\u4F5C\u589E\u5F3A
\u76EE\u6807\u5BF9\u8C61 \uFF08Target\uFF09\uFF1A\u4EE3\u7406\u7684\u76EE\u6807\u5BF9\u8C61
\u7EC7\u5165\uFF08Weabing\uFF09\uFF1A\u7EC7\u5165\u662F\u5C06\u589E\u5F3A\u6DFB\u52A0\u5230\u76EE\u6807\u7C7B\u7684\u5177\u4F53\u8FDE\u63A5\u70B9\u4E0A\u7684\u8FC7\u7A0B\u3002
\u7F16\u8BD1\u671F\u7EC7\u5165\uFF1A\u5207\u9762\u5728\u76EE\u6807\u7C7B\u7F16\u8BD1\u65F6\u88AB\u7EC7\u5165
\u7C7B\u52A0\u8F7D\u671F\u7EC7\u5165\uFF1A\u5207\u9762\u5728\u76EE\u6807\u7C7B\u52A0\u8F7D\u5230 JVM \u65F6\u88AB\u7EC7\u5165\u3002\u9700\u8981\u7279\u6B8A\u7684\u7C7B\u52A0\u8F7D\u5668\uFF0C\u5B83\u53EF\u4EE5\u5728\u76EE\u6807\u7C7B\u88AB\u5F15\u5165\u5E94\u7528\u4E4B\u524D\u589E\u5F3A\u8BE5\u76EE\u6807\u7C7B\u7684\u5B57\u8282\u7801\u3002
\u8FD0\u884C\u671F\u7EC7\u5165\uFF1A\u5207\u9762\u5728\u5E94\u7528\u8FD0\u884C\u7684\u67D0\u4E2A\u65F6\u523B\u88AB\u7EC7\u5165\u3002\u4E00\u822C\u60C5\u51B5\u4E0B\uFF0C\u5728\u7EC7\u5165\u5207\u9762\u65F6\uFF0CAOP \u5BB9\u5668\u4F1A\u4E3A\u76EE\u6807\u5BF9\u8C61\u52A8\u6001\u5730\u521B\u5EFA\u4E00\u4E2A\u4EE3\u7406\u5BF9\u8C61\u3002SpringAOP \u5C31\u662F\u4EE5\u8FD9\u79CD\u65B9\u5F0F\u7EC7\u5165\u5207\u9762\u3002
Spring \u91C7\u7528\u8FD0\u884C\u671F\u7EC7\u5165\uFF0C\u800C AspectJ \u91C7\u7528\u7F16\u8BD1\u671F\u7EC7\u5165\u548C\u7C7B\u52A0\u8F7D\u5668\u7EC7\u5165\u3002
\u5F15\u4ECB\uFF08introduction\uFF09\uFF1A\u5F15\u4ECB\u662F\u4E00\u79CD\u7279\u6B8A\u7684\u589E\u5F3A\uFF0C\u53EF\u4EE5\u52A8\u6001\u5730\u4E3A\u7C7B\u6DFB\u52A0\u4E00\u4E9B\u5C5E\u6027\u548C\u65B9\u6CD5
AOP \u6709\u54EA\u4E9B\u73AF\u7ED5\u65B9\u5F0F\uFF1F
AOP \u4E00\u822C\u6709 5 \u79CD\u73AF\u7ED5\u65B9\u5F0F\uFF1A
\u591A\u4E2A\u5207\u9762\u7684\u60C5\u51B5\u4E0B\uFF0C\u53EF\u4EE5\u901A\u8FC7 @Order \u6307\u5B9A\u5148\u540E\u987A\u5E8F\uFF0C\u6570\u5B57\u8D8A\u5C0F\uFF0C\u4F18\u5148\u7EA7\u8D8A\u9AD8\u3002
PS\uFF1A\u8FD9\u9053\u9898\u8001\u4E09\u7684\u540C\u4E8B\u9762\u8BD5\u5019\u9009\u4EBA\u7684\u65F6\u5019\u95EE\u5230\u4E86\uFF0C\u5019\u9009\u4EBA\u8BF4\u4E86\u4E00\u5806 AOP \u539F\u7406\uFF0C\u540C\u4E8B\u5C31\u52BF\u6765\u4E00\u53E5\uFF0C\u4F60\u80FD\u73B0\u573A\u5199\u4E00\u4E0B AOP \u7684\u5E94\u7528\u5417\uFF1F\u7ED3\u679C\u2014\u2014\u573A\u9762\u4E00\u5EA6\u5F88\u5C34\u5C2C\u3002\u867D\u7136\u6211\u5BF9\u9762\u8BD5\u5199\u8FD9\u79CD\u767E\u5EA6\u5C31\u80FD\u51FA\u6765\u7684\u4E1C\u897F\u6301\u4FDD\u7559\u610F\u89C1\uFF0C\u4F46\u662F\u8FD8\u662F\u52A0\u4E0A\u4E86\u8FD9\u4E00\u95EE\uFF0C\u6BD5\u7ADF\u62DB\u4EBA\u6700\u540E\u90FD\u662F\u8981\u64B8\u4EE3\u7801\u7684\u3002
\u8FD9\u91CC\u7ED9\u51FA\u4E00\u4E2A\u5C0F\u4F8B\u5B50\uFF0CSpringBoot \u9879\u76EE\u4E2D\uFF0C\u5229\u7528 AOP \u6253\u5370\u63A5\u53E3\u7684\u5165\u53C2\u548C\u51FA\u53C2\u65E5\u5FD7\uFF0C\u4EE5\u53CA\u6267\u884C\u65F6\u95F4\uFF0C\u8FD8\u662F\u6BD4\u8F83\u5FEB\u6377\u7684\u3002
\u5F15\u5165\u4F9D\u8D56\uFF1A\u5F15\u5165 AOP \u4F9D\u8D56
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
\u81EA\u5B9A\u4E49\u6CE8\u89E3\uFF1A\u81EA\u5B9A\u4E49\u4E00\u4E2A\u6CE8\u89E3\u4F5C\u4E3A\u5207\u70B9
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface WebLog {
}
\u914D\u7F6E AOP \u5207\u9762\uFF1A
@Aspect\uFF1A\u6807\u8BC6\u5207\u9762
@Pointcut\uFF1A\u8BBE\u7F6E\u5207\u70B9\uFF0C\u8FD9\u91CC\u4EE5\u81EA\u5B9A\u4E49\u6CE8\u89E3\u4E3A\u5207\u70B9\uFF0C\u5B9A\u4E49\u5207\u70B9\u6709\u5F88\u591A\u5176\u5B83\u79CD\u65B9\u5F0F\uFF0C\u81EA\u5B9A\u4E49\u6CE8\u89E3\u662F\u6BD4\u8F83\u5E38\u7528\u7684\u4E00\u79CD\u3002
@Before\uFF1A\u5728\u5207\u70B9\u4E4B\u524D\u7EC7\u5165\uFF0C\u6253\u5370\u4E86\u4E00\u4E9B\u5165\u53C2\u4FE1\u606F
@Around\uFF1A\u73AF\u7ED5\u5207\u70B9\uFF0C\u6253\u5370\u8FD4\u56DE\u53C2\u6570\u548C\u63A5\u53E3\u6267\u884C\u65F6\u95F4
@Aspect
@Component
public class WebLogAspect {
private final static Logger logger = LoggerFactory.getLogger(WebLogAspect.class);
/**
* \u4EE5\u81EA\u5B9A\u4E49 @WebLog \u6CE8\u89E3\u4E3A\u5207\u70B9
**/
@Pointcut("@annotation(cn.fighter3.spring.aop_demo.WebLog)")
public void webLog() {}
/**
* \u5728\u5207\u70B9\u4E4B\u524D\u7EC7\u5165
*/
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// \u5F00\u59CB\u6253\u5370\u8BF7\u6C42\u65E5\u5FD7
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// \u6253\u5370\u8BF7\u6C42\u76F8\u5173\u53C2\u6570
logger.info("========================================== Start ==========================================");
// \u6253\u5370\u8BF7\u6C42 url
logger.info("URL : {}", request.getRequestURL().toString());
// \u6253\u5370 Http method
logger.info("HTTP Method : {}", request.getMethod());
// \u6253\u5370\u8C03\u7528 controller \u7684\u5168\u8DEF\u5F84\u4EE5\u53CA\u6267\u884C\u65B9\u6CD5
logger.info("Class Method : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
// \u6253\u5370\u8BF7\u6C42\u7684 IP
logger.info("IP : {}", request.getRemoteAddr());
// \u6253\u5370\u8BF7\u6C42\u5165\u53C2
logger.info("Request Args : {}",new ObjectMapper().writeValueAsString(joinPoint.getArgs()));
}
/**
* \u5728\u5207\u70B9\u4E4B\u540E\u7EC7\u5165
* @throws Throwable
*/
@After("webLog()")
public void doAfter() throws Throwable {
// \u7ED3\u675F\u540E\u6253\u4E2A\u5206\u9694\u7EBF\uFF0C\u65B9\u4FBF\u67E5\u770B
logger.info("=========================================== End ===========================================");
}
/**
* \u73AF\u7ED5
*/
@Around("webLog()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//\u5F00\u59CB\u65F6\u95F4
long startTime = System.currentTimeMillis();
Object result = proceedingJoinPoint.proceed();
// \u6253\u5370\u51FA\u53C2
logger.info("Response Args : {}", new ObjectMapper().writeValueAsString(result));
// \u6267\u884C\u8017\u65F6
logger.info("Time-Consuming : {} ms", System.currentTimeMillis() - startTime);
return result;
}
}
\u4F7F\u7528\uFF1A\u53EA\u9700\u8981\u5728\u63A5\u53E3\u4E0A\u52A0\u4E0A\u81EA\u5B9A\u4E49\u6CE8\u89E3
@GetMapping("/hello")
@WebLog(desc = "\u8FD9\u662F\u4E00\u4E2A\u6B22\u8FCE\u63A5\u53E3")
public String hello(String name){
return "Hello "+name;
}
JDK \u52A8\u6001\u4EE3\u7406
CgLib \u52A8\u6001\u4EE3\u7406
\u6211\u4EEC\u6765\u770B\u4E00\u4E2A\u5E38\u89C1\u7684\u5C0F\u573A\u666F\uFF0C\u5BA2\u670D\u4E2D\u8F6C\uFF0C\u89E3\u51B3\u7528\u6237\u95EE\u9898\uFF1A
JDK \u52A8\u6001\u4EE3\u7406\u5B9E\u73B0\uFF1A
\u63A5\u53E3
public interface ISolver {
void solve();
}
\u76EE\u6807\u7C7B:\u9700\u8981\u5B9E\u73B0\u5BF9\u5E94\u63A5\u53E3
public class Solver implements ISolver {
@Override
public void solve() {
System.out.println("\u75AF\u72C2\u6389\u5934\u53D1\u89E3\u51B3\u95EE\u9898\u2026\u2026");
}
}
\u6001\u4EE3\u7406\u5DE5\u5382:ProxyFactory\uFF0C\u76F4\u63A5\u7528\u53CD\u5C04\u65B9\u5F0F\u751F\u6210\u4E00\u4E2A\u76EE\u6807\u5BF9\u8C61\u7684\u4EE3\u7406\u5BF9\u8C61\uFF0C\u8FD9\u91CC\u7528\u4E86\u4E00\u4E2A\u533F\u540D\u5185\u90E8\u7C7B\u65B9\u5F0F\u91CD\u5199 InvocationHandler \u65B9\u6CD5\uFF0C\u5B9E\u73B0\u63A5\u53E3\u91CD\u5199\u4E5F\u5DEE\u4E0D\u591A
public class ProxyFactory {
// \u7EF4\u62A4\u4E00\u4E2A\u76EE\u6807\u5BF9\u8C61
private Object target;
public ProxyFactory(Object target) {
this.target = target;
}
// \u4E3A\u76EE\u6807\u5BF9\u8C61\u751F\u6210\u4EE3\u7406\u5BF9\u8C61
public Object getProxyInstance() {
return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("\u8BF7\u95EE\u6709\u4EC0\u4E48\u53EF\u4EE5\u5E2E\u5230\u60A8\uFF1F");
// \u8C03\u7528\u76EE\u6807\u5BF9\u8C61\u65B9\u6CD5
Object returnValue = method.invoke(target, args);
System.out.println("\u95EE\u9898\u5DF2\u7ECF\u89E3\u51B3\u5566\uFF01");
return null;
}
});
}
}
\u5BA2\u6237\u7AEF\uFF1AClient\uFF0C\u751F\u6210\u4E00\u4E2A\u4EE3\u7406\u5BF9\u8C61\u5B9E\u4F8B\uFF0C\u901A\u8FC7\u4EE3\u7406\u5BF9\u8C61\u8C03\u7528\u76EE\u6807\u5BF9\u8C61\u65B9\u6CD5
public class Client {
public static void main(String[] args) {
//\u76EE\u6807\u5BF9\u8C61:\u7A0B\u5E8F\u5458
ISolver developer = new Solver();
//\u4EE3\u7406\uFF1A\u5BA2\u670D\u5C0F\u59D0\u59D0
ISolver csProxy = (ISolver) new ProxyFactory(developer).getProxyInstance();
//\u76EE\u6807\u65B9\u6CD5\uFF1A\u89E3\u51B3\u95EE\u9898
csProxy.solve();
}
}
Cglib \u52A8\u6001\u4EE3\u7406\u5B9E\u73B0\uFF1A
\u76EE\u6807\u7C7B\uFF1ASolver\uFF0C\u8FD9\u91CC\u76EE\u6807\u7C7B\u4E0D\u7528\u518D\u5B9E\u73B0\u63A5\u53E3\u3002
public class Solver {
public void solve() {
System.out.println("\u75AF\u72C2\u6389\u5934\u53D1\u89E3\u51B3\u95EE\u9898\u2026\u2026");
}
}
\u52A8\u6001\u4EE3\u7406\u5DE5\u5382\uFF1A
public class ProxyFactory implements MethodInterceptor {
//\u7EF4\u62A4\u4E00\u4E2A\u76EE\u6807\u5BF9\u8C61
private Object target;
public ProxyFactory(Object target) {
this.target = target;
}
//\u4E3A\u76EE\u6807\u5BF9\u8C61\u751F\u6210\u4EE3\u7406\u5BF9\u8C61
public Object getProxyInstance() {
//\u5DE5\u5177\u7C7B
Enhancer en = new Enhancer();
//\u8BBE\u7F6E\u7236\u7C7B
en.setSuperclass(target.getClass());
//\u8BBE\u7F6E\u56DE\u8C03\u51FD\u6570
en.setCallback(this);
//\u521B\u5EFA\u5B50\u7C7B\u5BF9\u8C61\u4EE3\u7406
return en.create();
}
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println("\u8BF7\u95EE\u6709\u4EC0\u4E48\u53EF\u4EE5\u5E2E\u5230\u60A8\uFF1F");
// \u6267\u884C\u76EE\u6807\u5BF9\u8C61\u7684\u65B9\u6CD5
Object returnValue = method.invoke(target, args);
System.out.println("\u95EE\u9898\u5DF2\u7ECF\u89E3\u51B3\u5566\uFF01");
return null;
}
}
\u5BA2\u6237\u7AEF\uFF1AClient
public class Client {
public static void main(String[] args) {
//\u76EE\u6807\u5BF9\u8C61:\u7A0B\u5E8F\u5458
Solver developer = new Solver();
//\u4EE3\u7406\uFF1A\u5BA2\u670D\u5C0F\u59D0\u59D0
Solver csProxy = (Solver) new ProxyFactory(developer).getProxyInstance();
//\u76EE\u6807\u65B9\u6CD5\uFF1A\u89E3\u51B3\u95EE\u9898
csProxy.solve();
}
}
Spring AOP
Spring AOP \u5C5E\u4E8E\u8FD0\u884C\u65F6\u589E\u5F3A
\uFF0C\u4E3B\u8981\u5177\u6709\u5982\u4E0B\u7279\u70B9\uFF1A
\u57FA\u4E8E\u52A8\u6001\u4EE3\u7406\u6765\u5B9E\u73B0\uFF0C\u9ED8\u8BA4\u5982\u679C\u4F7F\u7528\u63A5\u53E3\u7684\uFF0C\u7528 JDK \u63D0\u4F9B\u7684\u52A8\u6001\u4EE3\u7406\u5B9E\u73B0\uFF0C\u5982\u679C\u662F\u65B9\u6CD5\u5219\u4F7F\u7528 CGLIB \u5B9E\u73B0
Spring AOP \u9700\u8981\u4F9D\u8D56 IOC \u5BB9\u5668\u6765\u7BA1\u7406\uFF0C\u5E76\u4E14\u53EA\u80FD\u4F5C\u7528\u4E8E Spring \u5BB9\u5668\uFF0C\u4F7F\u7528\u7EAF Java \u4EE3\u7801\u5B9E\u73B0
\u5728\u6027\u80FD\u4E0A\uFF0C\u7531\u4E8E Spring AOP \u662F\u57FA\u4E8E\u52A8\u6001\u4EE3\u7406\u6765\u5B9E\u73B0\u7684\uFF0C\u5728\u5BB9\u5668\u542F\u52A8\u65F6\u9700\u8981\u751F\u6210\u4EE3\u7406\u5B9E\u4F8B\uFF0C\u5728\u65B9\u6CD5\u8C03\u7528\u4E0A\u4E5F\u4F1A\u589E\u52A0\u6808\u7684\u6DF1\u5EA6\uFF0C\u4F7F\u5F97 Spring AOP \u7684\u6027\u80FD\u4E0D\u5982 AspectJ \u7684\u90A3\u4E48\u597D\u3002
Spring AOP \u81F4\u529B\u4E8E\u89E3\u51B3\u4F01\u4E1A\u7EA7\u5F00\u53D1\u4E2D\u6700\u666E\u904D\u7684 AOP(\u65B9\u6CD5\u7EC7\u5165)\u3002
AspectJ
AspectJ \u662F\u4E00\u4E2A\u6613\u7528\u7684\u529F\u80FD\u5F3A\u5927\u7684 AOP \u6846\u67B6\uFF0C\u5C5E\u4E8E\u7F16\u8BD1\u65F6\u589E\u5F3A
\uFF0C \u53EF\u4EE5\u5355\u72EC\u4F7F\u7528\uFF0C\u4E5F\u53EF\u4EE5\u6574\u5408\u5230\u5176\u5B83\u6846\u67B6\u4E2D\uFF0C\u662F AOP \u7F16\u7A0B\u7684\u5B8C\u5168\u89E3\u51B3\u65B9\u6848\u3002AspectJ \u9700\u8981\u7528\u5230\u5355\u72EC\u7684\u7F16\u8BD1\u5668 ajc\u3002
AspectJ \u5C5E\u4E8E\u9759\u6001\u7EC7\u5165\uFF0C\u901A\u8FC7\u4FEE\u6539\u4EE3\u7801\u6765\u5B9E\u73B0\uFF0C\u5728\u5B9E\u9645\u8FD0\u884C\u4E4B\u524D\u5C31\u5B8C\u6210\u4E86\u7EC7\u5165\uFF0C\u6240\u4EE5\u8BF4\u5B83\u751F\u6210\u7684\u7C7B\u662F\u6CA1\u6709\u989D\u5916\u8FD0\u884C\u65F6\u5F00\u9500\u7684\uFF0C\u4E00\u822C\u6709\u5982\u4E0B\u51E0\u4E2A\u7EC7\u5165\u7684\u65F6\u673A\uFF1A
\u7F16\u8BD1\u671F\u7EC7\u5165\uFF08Compile-time weaving\uFF09\uFF1A\u5982\u7C7B A \u4F7F\u7528 AspectJ \u6DFB\u52A0\u4E86\u4E00\u4E2A\u5C5E\u6027\uFF0C\u7C7B B \u5F15\u7528\u4E86\u5B83\uFF0C\u8FD9\u4E2A\u573A\u666F\u5C31\u9700\u8981\u7F16\u8BD1\u671F\u7684\u65F6\u5019\u5C31\u8FDB\u884C\u7EC7\u5165\uFF0C\u5426\u5219\u6CA1\u6CD5\u7F16\u8BD1\u7C7B B\u3002
\u7F16\u8BD1\u540E\u7EC7\u5165\uFF08Post-compile weaving\uFF09\uFF1A\u4E5F\u5C31\u662F\u5DF2\u7ECF\u751F\u6210\u4E86 .class \u6587\u4EF6\uFF0C\u6216\u5DF2\u7ECF\u6253\u6210 jar \u5305\u4E86\uFF0C\u8FD9\u79CD\u60C5\u51B5\u6211\u4EEC\u9700\u8981\u589E\u5F3A\u5904\u7406\u7684\u8BDD\uFF0C\u5C31\u8981\u7528\u5230\u7F16\u8BD1\u540E\u7EC7\u5165\u3002
\u7C7B\u52A0\u8F7D\u540E\u7EC7\u5165\uFF08Load-time weaving\uFF09\uFF1A\u6307\u7684\u662F\u5728\u52A0\u8F7D\u7C7B\u7684\u65F6\u5019\u8FDB\u884C\u7EC7\u5165\uFF0C\u8981\u5B9E\u73B0\u8FD9\u4E2A\u65F6\u671F\u7684\u7EC7\u5165\uFF0C\u6709\u51E0\u79CD\u5E38\u89C1\u7684\u65B9\u6CD5
\u6574\u4F53\u5BF9\u6BD4\u5982\u4E0B\uFF1A
Spring \u4E8B\u52A1\u7684\u672C\u8D28\u5176\u5B9E\u5C31\u662F\u6570\u636E\u5E93\u5BF9\u4E8B\u52A1\u7684\u652F\u6301\uFF0C\u6CA1\u6709\u6570\u636E\u5E93\u7684\u4E8B\u52A1\u652F\u6301\uFF0CSpring \u662F\u65E0\u6CD5\u63D0\u4F9B\u4E8B\u52A1\u529F\u80FD\u7684\u3002Spring \u53EA\u63D0\u4F9B\u7EDF\u4E00\u4E8B\u52A1\u7BA1\u7406\u63A5\u53E3\uFF0C\u5177\u4F53\u5B9E\u73B0\u90FD\u662F\u7531\u5404\u6570\u636E\u5E93\u81EA\u5DF1\u5B9E\u73B0\uFF0C\u6570\u636E\u5E93\u4E8B\u52A1\u7684\u63D0\u4EA4\u548C\u56DE\u6EDA\u662F\u901A\u8FC7\u6570\u636E\u5E93\u81EA\u5DF1\u7684\u4E8B\u52A1\u673A\u5236\u5B9E\u73B0\u3002
Spring \u652F\u6301\u7F16\u7A0B\u5F0F\u4E8B\u52A1
\u7BA1\u7406\u548C\u58F0\u660E\u5F0F
\u4E8B\u52A1\u7BA1\u7406\u4E24\u79CD\u65B9\u5F0F\uFF1A
\u7F16\u7A0B\u5F0F\u4E8B\u52A1\u7BA1\u7406\u4F7F\u7528 TransactionTemplate\uFF0C\u9700\u8981\u663E\u5F0F\u6267\u884C\u4E8B\u52A1\u3002
\u58F0\u660E\u5F0F\u4E8B\u52A1
\u58F0\u660E\u5F0F\u4E8B\u52A1\u7BA1\u7406\u5EFA\u7ACB\u5728 AOP \u4E4B\u4E0A\u7684\u3002\u5176\u672C\u8D28\u662F\u901A\u8FC7 AOP \u529F\u80FD\uFF0C\u5BF9\u65B9\u6CD5\u524D\u540E\u8FDB\u884C\u62E6\u622A\uFF0C\u5C06\u4E8B\u52A1\u5904\u7406\u7684\u529F\u80FD\u7F16\u7EC7\u5230\u62E6\u622A\u7684\u65B9\u6CD5\u4E2D\uFF0C\u4E5F\u5C31\u662F\u5728\u76EE\u6807\u65B9\u6CD5\u5F00\u59CB\u4E4B\u524D\u542F\u52A8\u4E00\u4E2A\u4E8B\u52A1\uFF0C\u5728\u6267\u884C\u5B8C\u76EE\u6807\u65B9\u6CD5\u4E4B\u540E\u6839\u636E\u6267\u884C\u60C5\u51B5\u63D0\u4EA4\u6216\u8005\u56DE\u6EDA\u4E8B\u52A1
\u4F18\u70B9\u662F\u4E0D\u9700\u8981\u5728\u4E1A\u52A1\u903B\u8F91\u4EE3\u7801\u4E2D\u63BA\u6742\u4E8B\u52A1\u7BA1\u7406\u7684\u4EE3\u7801\uFF0C\u53EA\u9700\u5728\u914D\u7F6E\u6587\u4EF6\u4E2D\u505A\u76F8\u5173\u7684\u4E8B\u52A1\u89C4\u5219\u58F0\u660E\u6216\u901A\u8FC7 @Transactional \u6CE8\u89E3\u7684\u65B9\u5F0F\uFF0C\u4FBF\u53EF\u4EE5\u5C06\u4E8B\u52A1\u89C4\u5219\u5E94\u7528\u5230\u4E1A\u52A1\u903B\u8F91\u4E2D\uFF0C\u51CF\u5C11\u4E1A\u52A1\u4EE3\u7801\u7684\u6C61\u67D3\u3002\u552F\u4E00\u4E0D\u8DB3\u5730\u65B9\u662F\uFF0C\u6700\u7EC6\u7C92\u5EA6\u53EA\u80FD\u4F5C\u7528\u5230\u65B9\u6CD5\u7EA7\u522B\uFF0C\u65E0\u6CD5\u505A\u5230\u50CF\u7F16\u7A0B\u5F0F\u4E8B\u52A1\u90A3\u6837\u53EF\u4EE5\u4F5C\u7528\u5230\u4EE3\u7801\u5757\u7EA7\u522B\u3002
Spring \u7684\u63A5\u53E3 TransactionDefinition \u4E2D\u5B9A\u4E49\u4E86\u8868\u793A\u9694\u79BB\u7EA7\u522B\u7684\u5E38\u91CF\uFF0C\u5F53\u7136\u5176\u5B9E\u4E3B\u8981\u8FD8\u662F\u5BF9\u5E94\u6570\u636E\u5E93\u7684\u4E8B\u52A1\u9694\u79BB\u7EA7\u522B\uFF1A
Spring \u4E8B\u52A1\u7684\u4F20\u64AD\u673A\u5236\u8BF4\u7684\u662F\uFF0C\u5F53\u591A\u4E2A\u4E8B\u52A1\u540C\u65F6\u5B58\u5728\u7684\u65F6\u5019\u2014\u2014\u4E00\u822C\u6307\u7684\u662F\u591A\u4E2A\u4E8B\u52A1\u65B9\u6CD5\u76F8\u4E92\u8C03\u7528\u65F6\uFF0CSpring \u5982\u4F55\u5904\u7406\u8FD9\u4E9B\u4E8B\u52A1\u7684\u884C\u4E3A\u3002
\u4E8B\u52A1\u4F20\u64AD\u673A\u5236\u662F\u4F7F\u7528\u7B80\u5355\u7684 ThreadLocal \u5B9E\u73B0\u7684\uFF0C\u6240\u4EE5\uFF0C\u5982\u679C\u8C03\u7528\u7684\u65B9\u6CD5\u662F\u5728\u65B0\u7EBF\u7A0B\u8C03\u7528\u7684\uFF0C\u4E8B\u52A1\u4F20\u64AD\u5B9E\u9645\u4E0A\u662F\u4F1A\u5931\u6548\u7684\u3002
Spring \u9ED8\u8BA4\u7684\u4E8B\u52A1\u4F20\u64AD\u884C\u4E3A\u662F PROPAFATION_REQUIRED\uFF0C\u5B83\u9002\u5408\u7EDD\u5927\u591A\u6570\u60C5\u51B5\uFF0C\u5982\u679C\u591A\u4E2A ServiceX#methodX()\u90FD\u5DE5\u4F5C\u5728\u4E8B\u52A1\u73AF\u5883\u4E0B\uFF08\u5747\u88AB Spring \u4E8B\u52A1\u589E\u5F3A\uFF09\uFF0C\u4E14\u7A0B\u5E8F\u4E2D\u5B58\u5728\u8C03\u7528\u94FE Service1#method1()->Service2#method2()->Service3#method3()
\uFF0C\u90A3\u4E48\u8FD9 3 \u4E2A\u670D\u52A1\u7C7B\u7684\u4E09\u4E2A\u65B9\u6CD5\u901A\u8FC7 Spring \u7684\u4E8B\u52A1\u4F20\u64AD\u673A\u5236\u90FD\u5DE5\u4F5C\u5728\u540C\u4E00\u4E2A\u4E8B\u52A1\u4E2D\u3002
\u5C31\u662F\u901A\u8FC7 AOP/\u52A8\u6001\u4EE3\u7406\u3002
\u5728 Bean \u521D\u59CB\u5316\u9636\u6BB5\u521B\u5EFA\u4EE3\u7406\u5BF9\u8C61\uFF1ASpring \u5BB9\u5668\u5728\u521D\u59CB\u5316\u6BCF\u4E2A\u5355\u4F8B bean \u7684\u65F6\u5019\uFF0C\u4F1A\u904D\u5386\u5BB9\u5668\u4E2D\u7684\u6240\u6709 BeanPostProcessor \u5B9E\u73B0\u7C7B\uFF0C\u5E76\u6267\u884C\u5176 postProcessAfterInitialization \u65B9\u6CD5\uFF0C\u5728\u6267\u884C AbstractAutoProxyCreator \u7C7B\u7684 postProcessAfterInitialization \u65B9\u6CD5\u65F6\u4F1A\u904D\u5386\u5BB9\u5668\u4E2D\u6240\u6709\u7684\u5207\u9762\uFF0C\u67E5\u627E\u4E0E\u5F53\u524D\u5B9E\u4F8B\u5316 bean \u5339\u914D\u7684\u5207\u9762\uFF0C\u8FD9\u91CC\u4F1A\u83B7\u53D6\u4E8B\u52A1\u5C5E\u6027\u5207\u9762\uFF0C\u67E5\u627E@Transactional \u6CE8\u89E3\u53CA\u5176\u5C5E\u6027\u503C\uFF0C\u7136\u540E\u6839\u636E\u5F97\u5230\u7684\u5207\u9762\u521B\u5EFA\u4E00\u4E2A\u4EE3\u7406\u5BF9\u8C61\uFF0C\u9ED8\u8BA4\u662F\u4F7F\u7528 JDK \u52A8\u6001\u4EE3\u7406\u521B\u5EFA\u4EE3\u7406\uFF0C\u5982\u679C\u76EE\u6807\u7C7B\u662F\u63A5\u53E3\uFF0C\u5219\u4F7F\u7528 JDK \u52A8\u6001\u4EE3\u7406\uFF0C\u5426\u5219\u4F7F\u7528 Cglib\u3002
\u5728\u6267\u884C\u76EE\u6807\u65B9\u6CD5\u65F6\u8FDB\u884C\u4E8B\u52A1\u589E\u5F3A\u64CD\u4F5C\uFF1A\u5F53\u901A\u8FC7\u4EE3\u7406\u5BF9\u8C61\u8C03\u7528 Bean \u65B9\u6CD5\u7684\u65F6\u5019\uFF0C\u4F1A\u89E6\u53D1\u5BF9\u5E94\u7684 AOP \u589E\u5F3A\u62E6\u622A\u5668\uFF0C\u58F0\u660E\u5F0F\u4E8B\u52A1\u662F\u4E00\u79CD\u73AF\u7ED5\u589E\u5F3A\uFF0C\u5BF9\u5E94\u63A5\u53E3\u4E3AMethodInterceptor
\uFF0C\u4E8B\u52A1\u589E\u5F3A\u5BF9\u8BE5\u63A5\u53E3\u7684\u5B9E\u73B0\u4E3ATransactionInterceptor
\uFF0C\u7C7B\u56FE\u5982\u4E0B\uFF1A
\u4E8B\u52A1\u62E6\u622A\u5668TransactionInterceptor
\u5728invoke
\u65B9\u6CD5\u4E2D\uFF0C\u901A\u8FC7\u8C03\u7528\u7236\u7C7BTransactionAspectSupport
\u7684invokeWithinTransaction
\u65B9\u6CD5\u8FDB\u884C\u4E8B\u52A1\u5904\u7406\uFF0C\u5305\u62EC\u5F00\u542F\u4E8B\u52A1\u3001\u4E8B\u52A1\u63D0\u4EA4\u3001\u5F02\u5E38\u56DE\u6EDA\u3002
1\u3001@Transactional \u5E94\u7528\u5728\u975E public \u4FEE\u9970\u7684\u65B9\u6CD5\u4E0A
\u5982\u679C Transactional \u6CE8\u89E3\u5E94\u7528\u5728\u975E public \u4FEE\u9970\u7684\u65B9\u6CD5\u4E0A\uFF0CTransactional \u5C06\u4F1A\u5931\u6548\u3002
\u662F\u56E0\u4E3A\u5728 Spring AOP \u4EE3\u7406\u65F6\uFF0CTransactionInterceptor \uFF08\u4E8B\u52A1\u62E6\u622A\u5668\uFF09\u5728\u76EE\u6807\u65B9\u6CD5\u6267\u884C\u524D\u540E\u8FDB\u884C\u62E6\u622A\uFF0CDynamicAdvisedInterceptor\uFF08CglibAopProxy \u7684\u5185\u90E8\u7C7B\uFF09\u7684 intercept \u65B9\u6CD5 \u6216 JdkDynamicAopProxy \u7684 invoke \u65B9\u6CD5\u4F1A\u95F4\u63A5\u8C03\u7528 AbstractFallbackTransactionAttributeSource \u7684 computeTransactionAttribute\u65B9\u6CD5\uFF0C\u83B7\u53D6 Transactional \u6CE8\u89E3\u7684\u4E8B\u52A1\u914D\u7F6E\u4FE1\u606F\u3002
protected TransactionAttribute computeTransactionAttribute(Method method,
Class<?> targetClass) {
// Don't allow no-public methods as required.
if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
return null;
}
\u6B64\u65B9\u6CD5\u4F1A\u68C0\u67E5\u76EE\u6807\u65B9\u6CD5\u7684\u4FEE\u9970\u7B26\u662F\u5426\u4E3A public\uFF0C\u4E0D\u662F public \u5219\u4E0D\u4F1A\u83B7\u53D6@Transactional \u7684\u5C5E\u6027\u914D\u7F6E\u4FE1\u606F\u3002
2\u3001@Transactional \u6CE8\u89E3\u5C5E\u6027 propagation \u8BBE\u7F6E\u9519\u8BEF
3\u3001@Transactional \u6CE8\u89E3\u5C5E\u6027 rollbackFor \u8BBE\u7F6E\u9519\u8BEF
rollbackFor \u53EF\u4EE5\u6307\u5B9A\u80FD\u591F\u89E6\u53D1\u4E8B\u52A1\u56DE\u6EDA\u7684\u5F02\u5E38\u7C7B\u578B\u3002Spring \u9ED8\u8BA4\u629B\u51FA\u4E86\u672A\u68C0\u67E5 unchecked \u5F02\u5E38\uFF08\u7EE7\u627F\u81EA RuntimeException \u7684\u5F02\u5E38\uFF09\u6216\u8005 Error \u624D\u56DE\u6EDA\u4E8B\u52A1\uFF0C\u5176\u4ED6\u5F02\u5E38\u4E0D\u4F1A\u89E6\u53D1\u56DE\u6EDA\u4E8B\u52A1\u3002
// \u5E0C\u671B\u81EA\u5B9A\u4E49\u7684\u5F02\u5E38\u53EF\u4EE5\u8FDB\u884C\u56DE\u6EDA
@Transactional(propagation= Propagation.REQUIRED,rollbackFor= MyException.class
\u82E5\u5728\u76EE\u6807\u65B9\u6CD5\u4E2D\u629B\u51FA\u7684\u5F02\u5E38\u662F rollbackFor \u6307\u5B9A\u7684\u5F02\u5E38\u7684\u5B50\u7C7B\uFF0C\u4E8B\u52A1\u540C\u6837\u4F1A\u56DE\u6EDA\u3002
4\u3001\u540C\u4E00\u4E2A\u7C7B\u4E2D\u65B9\u6CD5\u8C03\u7528\uFF0C\u5BFC\u81F4@Transactional \u5931\u6548
\u5F00\u53D1\u4E2D\u907F\u514D\u4E0D\u4E86\u4F1A\u5BF9\u540C\u4E00\u4E2A\u7C7B\u91CC\u9762\u7684\u65B9\u6CD5\u8C03\u7528\uFF0C\u6BD4\u5982\u6709\u4E00\u4E2A\u7C7B Test\uFF0C\u5B83\u7684\u4E00\u4E2A\u65B9\u6CD5 A\uFF0CA \u518D\u8C03\u7528\u672C\u7C7B\u7684\u65B9\u6CD5 B\uFF08\u4E0D\u8BBA\u65B9\u6CD5 B \u662F\u7528 public \u8FD8\u662F private \u4FEE\u9970\uFF09\uFF0C\u4F46\u65B9\u6CD5 A \u6CA1\u6709\u58F0\u660E\u6CE8\u89E3\u4E8B\u52A1\uFF0C\u800C B \u65B9\u6CD5\u6709\u3002\u5219\u5916\u90E8\u8C03\u7528\u65B9\u6CD5 A \u4E4B\u540E\uFF0C\u65B9\u6CD5 B \u7684\u4E8B\u52A1\u662F\u4E0D\u4F1A\u8D77\u4F5C\u7528\u7684\u3002\u8FD9\u4E5F\u662F\u7ECF\u5E38\u72AF\u9519\u8BEF\u7684\u4E00\u4E2A\u5730\u65B9\u3002
\u90A3\u4E3A\u5565\u4F1A\u51FA\u73B0\u8FD9\u79CD\u60C5\u51B5\uFF1F\u5176\u5B9E\u8FD9\u8FD8\u662F\u7531\u4E8E\u4F7F\u7528 Spring AOP \u4EE3\u7406\u9020\u6210\u7684\uFF0C\u56E0\u4E3A\u53EA\u6709\u5F53\u4E8B\u52A1\u65B9\u6CD5\u88AB\u5F53\u524D\u7C7B\u4EE5\u5916\u7684\u4EE3\u7801\u8C03\u7528\u65F6\uFF0C\u624D\u4F1A\u7531 Spring \u751F\u6210\u7684\u4EE3\u7406\u5BF9\u8C61\u6765\u7BA1\u7406\u3002
//@Transactional
@GetMapping("/test")
private Integer A() throws Exception {
CityInfoDict cityInfoDict = new CityInfoDict();
cityInfoDict.setCityName("2");
/**
* B \u63D2\u5165\u5B57\u6BB5\u4E3A 3\u7684\u6570\u636E
*/
this.insertB();
/**
* A \u63D2\u5165\u5B57\u6BB5\u4E3A 2\u7684\u6570\u636E
*/
int insert = cityInfoDictMapper.insert(cityInfoDict);
return insert;
}
@Transactional()
public Integer insertB() throws Exception {
CityInfoDict cityInfoDict = new CityInfoDict();
cityInfoDict.setCityName("3");
cityInfoDict.setParentCityId(3);
return cityInfoDictMapper.insert(cityInfoDict);
}
\u8FD9\u79CD\u60C5\u51B5\u662F\u6700\u5E38\u89C1\u7684\u4E00\u79CD@Transactional \u6CE8\u89E3\u5931\u6548\u573A\u666F
@Transactional
private Integer A() throws Exception {
int insert = 0;
try {
CityInfoDict cityInfoDict = new CityInfoDict();
cityInfoDict.setCityName("2");
cityInfoDict.setParentCityId(2);
/**
* A \u63D2\u5165\u5B57\u6BB5\u4E3A 2\u7684\u6570\u636E
*/
insert = cityInfoDictMapper.insert(cityInfoDict);
/**
* B \u63D2\u5165\u5B57\u6BB5\u4E3A 3\u7684\u6570\u636E
*/
b.insertB();
} catch (Exception e) {
e.printStackTrace();
}
}
\u5982\u679C B \u65B9\u6CD5\u5185\u90E8\u629B\u4E86\u5F02\u5E38\uFF0C\u800C A \u65B9\u6CD5\u6B64\u65F6 try catch \u4E86 B \u65B9\u6CD5\u7684\u5F02\u5E38\uFF0C\u90A3\u8FD9\u4E2A\u4E8B\u52A1\u5C31\u4E0D\u80FD\u6B63\u5E38\u56DE\u6EDA\u4E86\uFF0C\u4F1A\u629B\u51FA\u5F02\u5E38\uFF1A
org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only
Spring MVC \u867D\u7136\u6574\u4F53\u6D41\u7A0B\u590D\u6742\uFF0C\u4F46\u662F\u5B9E\u9645\u5F00\u53D1\u4E2D\u5F88\u7B80\u5355\uFF0C\u5927\u90E8\u5206\u7684\u7EC4\u4EF6\u4E0D\u9700\u8981\u5F00\u53D1\u4EBA\u5458\u521B\u5EFA\u548C\u7BA1\u7406\uFF0C\u53EA\u9700\u8981\u901A\u8FC7\u914D\u7F6E\u6587\u4EF6\u7684\u65B9\u5F0F\u5B8C\u6210\u914D\u7F6E\u5373\u53EF\uFF0C\u771F\u6B63\u9700\u8981\u5F00\u53D1\u4EBA\u5458\u8FDB\u884C\u5904\u7406\u7684\u53EA\u6709 Handler\uFF08Controller\uFF09 \u3001View \u3001Model\u3002
\u5F53\u7136\u6211\u4EEC\u73B0\u5728\u5927\u90E8\u5206\u7684\u5F00\u53D1\u90FD\u662F\u524D\u540E\u7AEF\u5206\u79BB\uFF0CRestful \u98CE\u683C\u63A5\u53E3\uFF0C\u540E\u7AEF\u53EA\u9700\u8981\u8FD4\u56DE Json \u6570\u636E\u5C31\u884C\u4E86\u3002
PS:\u8FD9\u662F\u4E00\u9053\u5168\u65B0\u7684\u516B\u80A1\uFF0C\u6BD5\u7ADF ModelAndView \u8FD9\u79CD\u65B9\u5F0F\u5E94\u8BE5\u6CA1\u4EBA\u7528\u4E86\u5427\uFF1F\u73B0\u5728\u90FD\u662F\u524D\u540E\u7AEF\u5206\u79BB\u63A5\u53E3\uFF0C\u516B\u80A1\u4E5F\u8BE5\u66F4\u65B0\u6362\u4EE3\u4E86\u3002
\u6211\u4EEC\u90FD\u77E5\u9053 Restful \u63A5\u53E3\uFF0C\u54CD\u5E94\u683C\u5F0F\u662F json\uFF0C\u8FD9\u5C31\u7528\u5230\u4E86\u4E00\u4E2A\u5E38\u7528\u6CE8\u89E3\uFF1A@ResponseBody
@GetMapping("/user")
@ResponseBody
public User user(){
return new User(1,"\u5F20\u4E09");
}
\u52A0\u5165\u4E86\u8FD9\u4E2A\u6CE8\u89E3\u540E\uFF0C\u6574\u4F53\u7684\u6D41\u7A0B\u4E0A\u548C\u4F7F\u7528 ModelAndView \u5927\u4F53\u4E0A\u76F8\u540C\uFF0C\u4F46\u662F\u7EC6\u8282\u4E0A\u6709\u4E00\u4E9B\u4E0D\u540C\uFF1A
\u5BA2\u6237\u7AEF\u5411\u670D\u52A1\u7AEF\u53D1\u9001\u4E00\u6B21\u8BF7\u6C42\uFF0C\u8FD9\u4E2A\u8BF7\u6C42\u4F1A\u5148\u5230\u524D\u7AEF\u63A7\u5236\u5668 DispatcherServlet
DispatcherServlet \u63A5\u6536\u5230\u8BF7\u6C42\u540E\u4F1A\u8C03\u7528 HandlerMapping \u5904\u7406\u5668\u6620\u5C04\u5668\u3002\u7531\u6B64\u5F97\u77E5\uFF0C\u8BE5\u8BF7\u6C42\u8BE5\u7531\u54EA\u4E2A Controller \u6765\u5904\u7406
DispatcherServlet \u8C03\u7528 HandlerAdapter \u5904\u7406\u5668\u9002\u914D\u5668\uFF0C\u544A\u8BC9\u5904\u7406\u5668\u9002\u914D\u5668\u5E94\u8BE5\u8981\u53BB\u6267\u884C\u54EA\u4E2A Controller
Controller \u88AB\u5C01\u88C5\u6210\u4E86 ServletInvocableHandlerMethod\uFF0CHandlerAdapter \u5904\u7406\u5668\u9002\u914D\u5668\u53BB\u6267\u884C invokeAndHandle \u65B9\u6CD5\uFF0C\u5B8C\u6210\u5BF9 Controller \u7684\u8BF7\u6C42\u5904\u7406
HandlerAdapter \u6267\u884C\u5B8C\u5BF9 Controller \u7684\u8BF7\u6C42\uFF0C\u4F1A\u8C03\u7528 HandlerMethodReturnValueHandler \u53BB\u5904\u7406\u8FD4\u56DE\u503C\uFF0C\u4E3B\u8981\u7684\u8FC7\u7A0B\uFF1A
5.1. \u8C03\u7528 RequestResponseBodyMethodProcessor\uFF0C\u521B\u5EFA ServletServerHttpResponse\uFF08Spring \u5BF9\u539F\u751F ServerHttpResponse \u7684\u5C01\u88C5\uFF09\u5B9E\u4F8B
5.2.\u4F7F\u7528 HttpMessageConverter \u7684 write \u65B9\u6CD5\uFF0C\u5C06\u8FD4\u56DE\u503C\u5199\u5165 ServletServerHttpResponse \u7684 OutputStream \u8F93\u51FA\u6D41\u4E2D
5.3.\u5728\u5199\u5165\u7684\u8FC7\u7A0B\u4E2D\uFF0C\u4F1A\u4F7F\u7528 JsonGenerator\uFF08\u9ED8\u8BA4\u4F7F\u7528 Jackson \u6846\u67B6\uFF09\u5BF9\u8FD4\u56DE\u503C\u8FDB\u884C Json \u5E8F\u5217\u5316
\u6267\u884C\u5B8C\u8BF7\u6C42\u540E\uFF0C\u8FD4\u56DE\u7684 ModealAndView \u4E3A null\uFF0CServletServerHttpResponse \u91CC\u4E5F\u5DF2\u7ECF\u5199\u5165\u4E86\u54CD\u5E94\uFF0C\u6240\u4EE5\u4E0D\u7528\u5173\u5FC3 View \u7684\u5904\u7406
Spring Boot \u57FA\u4E8E Spring \u5F00\u53D1\uFF0CSpirng Boot \u672C\u8EAB\u5E76\u4E0D\u63D0\u4F9B Spring \u6846\u67B6\u7684\u6838\u5FC3\u7279\u6027\u4EE5\u53CA\u6269\u5C55\u529F\u80FD\uFF0C\u53EA\u662F\u7528\u4E8E\u5FEB\u901F\u3001\u654F\u6377\u5730\u5F00\u53D1\u65B0\u4E00\u4EE3\u57FA\u4E8E Spring \u6846\u67B6\u7684\u5E94\u7528\u7A0B\u5E8F\u3002\u5B83\u5E76\u4E0D\u662F\u7528\u6765\u66FF\u4EE3 Spring \u7684\u89E3\u51B3\u65B9\u6848\uFF0C\u800C\u662F\u548C Spring \u6846\u67B6\u7D27\u5BC6\u7ED3\u5408\u7528\u4E8E\u63D0\u5347 Spring \u5F00\u53D1\u8005\u4F53\u9A8C\u7684\u5DE5\u5177\u3002
Spring Boot \u4EE5\u7EA6\u5B9A\u5927\u4E8E\u914D\u7F6E
\u6838\u5FC3\u601D\u60F3\u5F00\u5C55\u5DE5\u4F5C\uFF0C\u76F8\u6BD4 Spring \u5177\u6709\u5982\u4E0B\u4F18\u52BF\uFF1A
SpringBoot \u5F00\u542F\u81EA\u52A8\u914D\u7F6E\u7684\u6CE8\u89E3\u662F@EnableAutoConfiguration
\uFF0C\u542F\u52A8\u7C7B\u4E0A\u7684\u6CE8\u89E3@SpringBootApplication
\u662F\u4E00\u4E2A\u590D\u5408\u6CE8\u89E3\uFF0C\u5305\u542B\u4E86@EnableAutoConfiguration\uFF1A
EnableAutoConfiguration
\u53EA\u662F\u4E00\u4E2A\u7B80\u5355\u7684\u6CE8\u89E3\uFF0C\u81EA\u52A8\u88C5\u914D\u6838\u5FC3\u529F\u80FD\u7684\u5B9E\u73B0\u5B9E\u9645\u662F\u901A\u8FC7 AutoConfigurationImportSelector
\u7C7B
@AutoConfigurationPackage //\u5C06main\u540C\u7EA7\u7684\u5305\u4E0B\u7684\u6240\u6709\u7EC4\u4EF6\u6CE8\u518C\u5230\u5BB9\u5668\u4E2D
@Import({AutoConfigurationImportSelector.class}) //\u52A0\u8F7D\u81EA\u52A8\u88C5\u914D\u7C7B xxxAutoconfiguration
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
AutoConfigurationImportSelector
\u5B9E\u73B0\u4E86ImportSelector
\u63A5\u53E3\uFF0C\u8FD9\u4E2A\u63A5\u53E3\u7684\u4F5C\u7528\u5C31\u662F\u6536\u96C6\u9700\u8981\u5BFC\u5165\u7684\u914D\u7F6E\u7C7B\uFF0C\u914D\u5408@Import(\uFF09
\u5C31\u53EF\u4EE5\u5C06\u76F8\u5E94\u7684\u7C7B\u5BFC\u5165\u5230 Spring \u5BB9\u5668\u4E2D
\u83B7\u53D6\u6CE8\u5165\u7C7B\u7684\u65B9\u6CD5\u662F selectImports()\uFF0C\u5B83\u5B9E\u9645\u8C03\u7528\u7684\u662FgetAutoConfigurationEntry
\uFF0C\u8FD9\u4E2A\u65B9\u6CD5\u662F\u83B7\u53D6\u81EA\u52A8\u88C5\u914D\u7C7B\u7684\u5173\u952E\uFF0C\u4E3B\u8981\u6D41\u7A0B\u53EF\u4EE5\u5206\u4E3A\u8FD9\u4E48\u51E0\u6B65\uFF1A
protected AutoConfigurationImportSelector.AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
if (!this.isEnabled(annotationMetadata)) {
return EMPTY_ENTRY;
} else {
//1.\u83B7\u53D6\u5230\u6CE8\u89E3\u7684\u5C5E\u6027
AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
//2.\u83B7\u53D6\u9700\u8981\u81EA\u52A8\u88C5\u914D\u7684\u6240\u6709\u914D\u7F6E\u7C7B\uFF0C\u8BFB\u53D6META-INF/spring.factories\uFF0C\u83B7\u53D6\u81EA\u52A8\u914D\u7F6E\u7C7B\u8DEF\u5F84
List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
//3.1.\u79FB\u9664\u91CD\u590D\u7684\u914D\u7F6E
configurations = this.removeDuplicates(configurations);
//3.2.\u5904\u7406\u9700\u8981\u6392\u9664\u7684\u914D\u7F6E
Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);
this.checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = this.getConfigurationClassFilter().filter(configurations);
this.fireAutoConfigurationImportEvents(configurations, exclusions);
return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions);
}
}
\u77E5\u9053\u4E86\u81EA\u52A8\u914D\u7F6E\u539F\u7406\uFF0C\u521B\u5EFA\u4E00\u4E2A\u81EA\u5B9A\u4E49 SpringBoot Starter \u4E5F\u5F88\u7B80\u5355\u3002
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
\u7F16\u5199\u914D\u7F6E\u6587\u4EF6
\u8FD9\u91CC\u5B9A\u4E49\u4E86\u5C5E\u6027\u914D\u7F6E\u7684\u524D\u7F00
@ConfigurationProperties(prefix = "hello")
public class HelloProperties {
private String name;
//\u7701\u7565getter\u3001setter
}
\u81EA\u52A8\u88C5\u914D
\u521B\u5EFA\u81EA\u52A8\u914D\u7F6E\u7C7B HelloPropertiesConfigure
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloPropertiesConfigure {
}
\u914D\u7F6E\u81EA\u52A8\u7C7B
\u5728/resources/META-INF/spring.factories
\u6587\u4EF6\u4E2D\u6DFB\u52A0\u81EA\u52A8\u914D\u7F6E\u7C7B\u8DEF\u5F84
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\\
cn.fighter3.demo.starter.configure.HelloPropertiesConfigure
\u6D4B\u8BD5
\u521B\u5EFA\u4E00\u4E2A\u5DE5\u7A0B\uFF0C\u5F15\u5165\u81EA\u5B9A\u4E49 starter \u4F9D\u8D56
<dependency>
<groupId>cn.fighter3</groupId>
<artifactId>demo-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
\u5728\u914D\u7F6E\u6587\u4EF6\u91CC\u6DFB\u52A0\u914D\u7F6E
hello.name=\u5F20\u4E09
\u6D4B\u8BD5\u7C7B
@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloTest {
@Autowired
HelloProperties helloProperties;
@Test
public void hello(){
System.out.println("\u4F60\u597D\uFF0C"+helloProperties.getName());
}
}
\u81F3\u6B64\uFF0C\u968F\u624B\u5199\u7684\u4E00\u4E2A\u81EA\u5B9A\u4E49 SpringBoot-Starter \u5C31\u5B8C\u6210\u4E86\uFF0C\u867D\u7136\u6BD4\u8F83\u7B80\u5355\uFF0C\u4F46\u662F\u5B8C\u6210\u4E86\u4E3B\u8981\u7684\u81EA\u52A8\u88C5\u914D\u7684\u80FD\u529B\u3002
SpringApplication \u8FD9\u4E2A\u7C7B\u4E3B\u8981\u505A\u4E86\u4EE5\u4E0B\u56DB\u4EF6\u4E8B\u60C5\uFF1A
SpringBoot \u542F\u52A8\u5927\u81F4\u6D41\u7A0B\u5982\u4E0B \uFF1A
SpringCloud \u662F Spring \u5B98\u65B9\u63A8\u51FA\u7684\u5FAE\u670D\u52A1\u6CBB\u7406\u6846\u67B6\u3002
\u4EC0\u4E48\u662F\u5FAE\u670D\u52A1\uFF1F
\u5FAE\u670D\u52A1\u67B6\u6784\u4E3B\u8981\u8981\u89E3\u51B3\u54EA\u4E9B\u95EE\u9898\uFF1F
\u6709\u54EA\u4E9B\u4E3B\u6D41\u5FAE\u670D\u52A1\u6846\u67B6\uFF1F
SpringCloud \u6709\u54EA\u4E9B\u6838\u5FC3\u7EC4\u4EF6\uFF1F
PS:\u5FAE\u670D\u52A1\u540E\u9762\u6709\u673A\u4F1A\u518D\u6269\u5C55\uFF0C\u5176\u5B9E\u9762\u8BD5\u4E00\u822C\u90FD\u662F\u7ED3\u5408\u9879\u76EE\u53BB\u95EE\u3002