diff --git a/spring/jpa/配置.md b/spring/jpa/配置.md new file mode 100644 index 0000000..5be9b7b --- /dev/null +++ b/spring/jpa/配置.md @@ -0,0 +1,5 @@ +```java +@EnableJpaAuditing +// JPA、审计,用于设置@CreatedBy@CreatedDate@LastModifiedBy@LastModifiedDate +``` + diff --git a/spring/springboot/SimpleApplicationEventMulticaster.md b/spring/springboot/SimpleApplicationEventMulticaster.md new file mode 100644 index 0000000..e69de29 diff --git a/spring/springboot/getSpringFactoriesInstances/1.ApplicationContextInitializer.md b/spring/springboot/getSpringFactoriesInstances/1.ApplicationContextInitializer.md new file mode 100644 index 0000000..cbfbcc0 --- /dev/null +++ b/spring/springboot/getSpringFactoriesInstances/1.ApplicationContextInitializer.md @@ -0,0 +1,12 @@ +```java +public interface ApplicationContextInitializer { + + /** + * Initialize the given application context. + * @param applicationContext the application to configure + */ + void initialize(C applicationContext); + +} +``` + diff --git a/spring/springboot/getSpringFactoriesInstances/2.ApplicationListener.md b/spring/springboot/getSpringFactoriesInstances/2.ApplicationListener.md new file mode 100644 index 0000000..59f6862 --- /dev/null +++ b/spring/springboot/getSpringFactoriesInstances/2.ApplicationListener.md @@ -0,0 +1,12 @@ +```java +public interface ApplicationListener extends EventListener { + + /** + * Handle an application event. + * @param event the event to respond to + */ + void onApplicationEvent(E event); + +} +``` + diff --git a/spring/springboot/getSpringFactoriesInstances/3.SpringApplicationRunListener.md b/spring/springboot/getSpringFactoriesInstances/3.SpringApplicationRunListener.md new file mode 100644 index 0000000..5f1585d --- /dev/null +++ b/spring/springboot/getSpringFactoriesInstances/3.SpringApplicationRunListener.md @@ -0,0 +1,60 @@ +```java +public interface SpringApplicationRunListener { + + /** + * Called immediately when the run method has first started. Can be used for very + * early initialization. + */ + void starting(); + + /** + * Called once the environment has been prepared, but before the + * {@link ApplicationContext} has been created. + * @param environment the environment + */ + void environmentPrepared(ConfigurableEnvironment environment); + + /** + * Called once the {@link ApplicationContext} has been created and prepared, but + * before sources have been loaded. + * @param context the application context + */ + void contextPrepared(ConfigurableApplicationContext context); + + /** + * Called once the application context has been loaded but before it has been + * refreshed. + * @param context the application context + */ + void contextLoaded(ConfigurableApplicationContext context); + + /** + * The context has been refreshed and the application has started but + * {@link CommandLineRunner CommandLineRunners} and {@link ApplicationRunner + * ApplicationRunners} have not been called. + * @param context the application context. + * @since 2.0.0 + */ + void started(ConfigurableApplicationContext context); + + /** + * Called immediately before the run method finishes, when the application context has + * been refreshed and all {@link CommandLineRunner CommandLineRunners} and + * {@link ApplicationRunner ApplicationRunners} have been called. + * @param context the application context. + * @since 2.0.0 + */ + void running(ConfigurableApplicationContext context); + + /** + * Called when a failure occurs when running the application. + * @param context the application context or {@code null} if a failure occurred before + * the context was created + * @param exception the failure + * @since 2.0.0 + */ + void failed(ConfigurableApplicationContext context, Throwable exception); + +} +``` + diff --git a/spring/springboot/getSpringFactoriesInstances/4.SpringBootExceptionReporter.md b/spring/springboot/getSpringFactoriesInstances/4.SpringBootExceptionReporter.md new file mode 100644 index 0000000..3c20fe8 --- /dev/null +++ b/spring/springboot/getSpringFactoriesInstances/4.SpringBootExceptionReporter.md @@ -0,0 +1,14 @@ +```java +public interface SpringBootExceptionReporter { + + /** + * Report a startup failure to the user. + * @param failure the source failure + * @return {@code true} if the failure was reported or {@code false} if default + * reporting should occur. + */ + boolean reportException(Throwable failure); + +} +``` + diff --git a/spring/springboot/getSpringFactoriesInstances/5.ConfigurableApplicationContext.md b/spring/springboot/getSpringFactoriesInstances/5.ConfigurableApplicationContext.md new file mode 100644 index 0000000..4488ec8 --- /dev/null +++ b/spring/springboot/getSpringFactoriesInstances/5.ConfigurableApplicationContext.md @@ -0,0 +1,208 @@ +```java +public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable { + + /** + * Any number of these characters are considered delimiters between + * multiple context config paths in a single String value. + * @see org.springframework.context.support.AbstractXmlApplicationContext#setConfigLocation + * @see org.springframework.web.context.ContextLoader#CONFIG_LOCATION_PARAM + * @see org.springframework.web.servlet.FrameworkServlet#setContextConfigLocation + */ + String CONFIG_LOCATION_DELIMITERS = ",; \t\n"; + + /** + * Name of the ConversionService bean in the factory. + * If none is supplied, default conversion rules apply. + * @since 3.0 + * @see org.springframework.core.convert.ConversionService + */ + String CONVERSION_SERVICE_BEAN_NAME = "conversionService"; + + /** + * Name of the LoadTimeWeaver bean in the factory. If such a bean is supplied, + * the context will use a temporary ClassLoader for type matching, in order + * to allow the LoadTimeWeaver to process all actual bean classes. + * @since 2.5 + * @see org.springframework.instrument.classloading.LoadTimeWeaver + */ + String LOAD_TIME_WEAVER_BEAN_NAME = "loadTimeWeaver"; + + /** + * Name of the {@link Environment} bean in the factory. + * @since 3.1 + */ + String ENVIRONMENT_BEAN_NAME = "environment"; + + /** + * Name of the System properties bean in the factory. + * @see java.lang.System#getProperties() + */ + String SYSTEM_PROPERTIES_BEAN_NAME = "systemProperties"; + + /** + * Name of the System environment bean in the factory. + * @see java.lang.System#getenv() + */ + String SYSTEM_ENVIRONMENT_BEAN_NAME = "systemEnvironment"; + + + /** + * Set the unique id of this application context. + * @since 3.0 + */ + void setId(String id); + + /** + * Set the parent of this application context. + *

Note that the parent shouldn't be changed: It should only be set outside + * a constructor if it isn't available when an object of this class is created, + * for example in case of WebApplicationContext setup. + * @param parent the parent context + * @see org.springframework.web.context.ConfigurableWebApplicationContext + */ + void setParent(@Nullable ApplicationContext parent); + + /** + * Set the {@code Environment} for this application context. + * @param environment the new environment + * @since 3.1 + */ + void setEnvironment(ConfigurableEnvironment environment); + + /** + * Return the {@code Environment} for this application context in configurable + * form, allowing for further customization. + * @since 3.1 + */ + @Override + ConfigurableEnvironment getEnvironment(); + + /** + * Add a new BeanFactoryPostProcessor that will get applied to the internal + * bean factory of this application context on refresh, before any of the + * bean definitions get evaluated. To be invoked during context configuration. + * @param postProcessor the factory processor to register + */ + void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor); + + /** + * Add a new ApplicationListener that will be notified on context events + * such as context refresh and context shutdown. + *

Note that any ApplicationListener registered here will be applied + * on refresh if the context is not active yet, or on the fly with the + * current event multicaster in case of a context that is already active. + * @param listener the ApplicationListener to register + * @see org.springframework.context.event.ContextRefreshedEvent + * @see org.springframework.context.event.ContextClosedEvent + */ + void addApplicationListener(ApplicationListener listener); + + /** + * Register the given protocol resolver with this application context, + * allowing for additional resource protocols to be handled. + *

Any such resolver will be invoked ahead of this context's standard + * resolution rules. It may therefore also override any default rules. + * @since 4.3 + */ + void addProtocolResolver(ProtocolResolver resolver); + + /** + * Load or refresh the persistent representation of the configuration, + * which might an XML file, properties file, or relational database schema. + *

As this is a startup method, it should destroy already created singletons + * if it fails, to avoid dangling resources. In other words, after invocation + * of that method, either all or no singletons at all should be instantiated. + * @throws BeansException if the bean factory could not be initialized + * @throws IllegalStateException if already initialized and multiple refresh + * attempts are not supported + */ + void refresh() throws BeansException, IllegalStateException; + + /** + * Register a shutdown hook with the JVM runtime, closing this context + * on JVM shutdown unless it has already been closed at that time. + *

This method can be called multiple times. Only one shutdown hook + * (at max) will be registered for each context instance. + * @see java.lang.Runtime#addShutdownHook + * @see #close() + */ + void registerShutdownHook(); + + /** + * Close this application context, releasing all resources and locks that the + * implementation might hold. This includes destroying all cached singleton beans. + *

Note: Does not invoke {@code close} on a parent context; + * parent contexts have their own, independent lifecycle. + *

This method can be called multiple times without side effects: Subsequent + * {@code close} calls on an already closed context will be ignored. + */ + @Override + void close(); + + /** + * Determine whether this application context is active, that is, + * whether it has been refreshed at least once and has not been closed yet. + * @return whether the context is still active + * @see #refresh() + * @see #close() + * @see #getBeanFactory() + */ + boolean isActive(); + + /** + * Return the internal bean factory of this application context. + * Can be used to access specific functionality of the underlying factory. + *

Note: Do not use this to post-process the bean factory; singletons + * will already have been instantiated before. Use a BeanFactoryPostProcessor + * to intercept the BeanFactory setup process before beans get touched. + *

Generally, this internal factory will only be accessible while the context + * is active, that is, in-between {@link #refresh()} and {@link #close()}. + * The {@link #isActive()} flag can be used to check whether the context + * is in an appropriate state. + * @return the underlying bean factory + * @throws IllegalStateException if the context does not hold an internal + * bean factory (usually if {@link #refresh()} hasn't been called yet or + * if {@link #close()} has already been called) + * @see #isActive() + * @see #refresh() + * @see #close() + * @see #addBeanFactoryPostProcessor + */ + ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException; + +} +``` + + + + + + + +我的妹妹师晓玲在做毕业设计,数组使用明确吗? + +词性: 名词, 疑问词,动词 + +第一列 第二列 + +名词 我,妹妹,师晓玲,毕业设计,数组 + +疑问词 明确吗 + +动词 做,使用 + + + +第一列 第二列 第三列 + +名词 疑问词 动词 + +我 明确吗 做 + +妹妹 使用 + +师晓玲 + +毕业设计 + +数组 diff --git a/spring/springboot/springboot01.md b/spring/springboot/springboot01.md new file mode 100644 index 0000000..47b1e6c --- /dev/null +++ b/spring/springboot/springboot01.md @@ -0,0 +1,255 @@ +## 启动简单流程 + +1. allowing for further customization +1. 创建一个依赖启动类的实例ApplicationContext +2. 注册一个CommandLinePropertySource用来解析参数,并应用在容器中 +3. 刷新应用上下文,加载单例。 +4. 处理CommandLineRunner + +一次实例化类 + +1. ApplicationContextInitializer +2. ApplicationListener +3. SpringApplicationRunListener + +# 启动 + +```java + public static void main(String[] args) { + SpringApplication.run(PlantformApplication.class, args); + } +``` + +# 运行 + +```java + /** + * Static helper that can be used to run a {@link SpringApplication} from the + * specified source using default settings. + * @param primarySource the primary source to load + * @param args the application arguments (usually passed from a Java main method) + * @return the running {@link ApplicationContext} + */ + public static ConfigurableApplicationContext run(Class primarySource, + String... args) { + return run(new Class[] { primarySource }, args); + } +``` + +```java + /** + * Static helper that can be used to run a {@link SpringApplication} from the + * specified sources using default settings and user supplied arguments. + * @param primarySources the primary sources to load + * @param args the application arguments (usually passed from a Java main method) + * @return the running {@link ApplicationContext} + */ + public static ConfigurableApplicationContext run(Class[] primarySources, + String[] args) { + return new SpringApplication(primarySources).run(args); + } +``` + +## 创建对象 + +```java +public SpringApplication(Class... primarySources) { + this(null, primarySources); +} + public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) { + this.resourceLoader = resourceLoader; + Assert.notNull(primarySources, "PrimarySources must not be null"); + this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources)); + this.webApplicationType = WebApplicationType.deduceFromClasspath(); + setInitializers((Collection) getSpringFactoriesInstances( + ApplicationContextInitializer.class)); + setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); + this.mainApplicationClass = deduceMainApplicationClass(); + } + + private Collection getSpringFactoriesInstances(Class type, + Class[] parameterTypes, Object... args) { + ClassLoader classLoader = getClassLoader(); + // Use names and ensure unique to protect against duplicates + Set names = new LinkedHashSet<>( + SpringFactoriesLoader.loadFactoryNames(type, classLoader)); + List instances = createSpringFactoriesInstances(type, parameterTypes, + classLoader, args, names); + AnnotationAwareOrderComparator.sort(instances); + return instances; + } +``` + +## run + +```java + public ConfigurableApplicationContext run(String... args) { + StopWatch stopWatch = new StopWatch(); + stopWatch.start(); + ConfigurableApplicationContext context = null; + Collection exceptionReporters = new ArrayList<>(); + configureHeadlessProperty(); + SpringApplicationRunListeners listeners = getRunListeners(args); + listeners.starting(); + try { + ApplicationArguments applicationArguments = new DefaultApplicationArguments( + args); + ConfigurableEnvironment environment = prepareEnvironment(listeners, + applicationArguments); + configureIgnoreBeanInfo(environment); + Banner printedBanner = printBanner(environment); + context = createApplicationContext(); + exceptionReporters = getSpringFactoriesInstances( + SpringBootExceptionReporter.class, + new Class[] { ConfigurableApplicationContext.class }, context); + prepareContext(context, environment, listeners, applicationArguments, + printedBanner); + refreshContext(context); + afterRefresh(context, applicationArguments); + stopWatch.stop(); + if (this.logStartupInfo) { + new StartupInfoLogger(this.mainApplicationClass) + .logStarted(getApplicationLog(), stopWatch); + } + listeners.started(context); + callRunners(context, applicationArguments); + } + catch (Throwable ex) { + handleRunFailure(context, ex, exceptionReporters, listeners); + throw new IllegalStateException(ex); + } + + try { + listeners.running(context); + } + catch (Throwable ex) { + handleRunFailure(context, ex, exceptionReporters, null); + throw new IllegalStateException(ex); + } + return context; + } + + private SpringApplicationRunListeners getRunListeners(String[] args) { + Class[] types = new Class[] { SpringApplication.class, String[].class }; + return new SpringApplicationRunListeners(logger, getSpringFactoriesInstances( + SpringApplicationRunListener.class, types, this, args)); + } + private ConfigurableEnvironment prepareEnvironment( + SpringApplicationRunListeners listeners, + ApplicationArguments applicationArguments) { + // Create and configure the environment + ConfigurableEnvironment environment = getOrCreateEnvironment(); + configureEnvironment(environment, applicationArguments.getSourceArgs()); + listeners.environmentPrepared(environment); + bindToSpringApplication(environment); + if (!this.isCustomEnvironment) { + environment = new EnvironmentConverter(getClassLoader()) + .convertEnvironmentIfNecessary(environment, deduceEnvironmentClass()); + } + ConfigurationPropertySources.attach(environment); + return environment; + } + private Collection getSpringFactoriesInstances(Class type, + Class[] parameterTypes, Object... args) { + ClassLoader classLoader = getClassLoader(); + // Use names and ensure unique to protect against duplicates + Set names = new LinkedHashSet<>( + SpringFactoriesLoader.loadFactoryNames(type, classLoader)); + List instances = createSpringFactoriesInstances(type, parameterTypes, + classLoader, args, names); + AnnotationAwareOrderComparator.sort(instances); + return instances; + } + private void prepareContext(ConfigurableApplicationContext context, + ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, + ApplicationArguments applicationArguments, Banner printedBanner) { + context.setEnvironment(environment); + postProcessApplicationContext(context); + applyInitializers(context); + listeners.contextPrepared(context); + if (this.logStartupInfo) { + logStartupInfo(context.getParent() == null); + logStartupProfileInfo(context); + } + // Add boot specific singleton beans + ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); + beanFactory.registerSingleton("springApplicationArguments", applicationArguments); + if (printedBanner != null) { + beanFactory.registerSingleton("springBootBanner", printedBanner); + } + if (beanFactory instanceof DefaultListableBeanFactory) { + ((DefaultListableBeanFactory) beanFactory) + .setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding); + } + // Load the sources + Set sources = getAllSources(); + Assert.notEmpty(sources, "Sources must not be empty"); + load(context, sources.toArray(new Object[0])); + listeners.contextLoaded(context); + } + + public void refresh() throws BeansException, IllegalStateException { + synchronized (this.startupShutdownMonitor) { + // Prepare this context for refreshing. + prepareRefresh(); + + // Tell the subclass to refresh the internal bean factory. + ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); + + // Prepare the bean factory for use in this context. + prepareBeanFactory(beanFactory); + + try { + // Allows post-processing of the bean factory in context subclasses. + postProcessBeanFactory(beanFactory); + + // Invoke factory processors registered as beans in the context. + invokeBeanFactoryPostProcessors(beanFactory); + + // Register bean processors that intercept bean creation. + registerBeanPostProcessors(beanFactory); + + // Initialize message source for this context. + initMessageSource(); + + // Initialize event multicaster for this context. + initApplicationEventMulticaster(); + + // Initialize other special beans in specific context subclasses. + onRefresh(); + + // Check for listener beans and register them. + registerListeners(); + + // Instantiate all remaining (non-lazy-init) singletons. + finishBeanFactoryInitialization(beanFactory); + + // Last step: publish corresponding event. + finishRefresh(); + } + + catch (BeansException ex) { + if (logger.isWarnEnabled()) { + logger.warn("Exception encountered during context initialization - " + + "cancelling refresh attempt: " + ex); + } + + // Destroy already created singletons to avoid dangling resources. + destroyBeans(); + + // Reset 'active' flag. + cancelRefresh(ex); + + // Propagate exception to caller. + throw ex; + } + + finally { + // Reset common introspection caches in Spring's core, since we + // might not ever need metadata for singleton beans anymore... + resetCommonCaches(); + } + } + } +``` + diff --git a/spring/springboot/启动.md b/spring/springboot/启动.md index b33a4dd..65c00af 100644 --- a/spring/springboot/启动.md +++ b/spring/springboot/启动.md @@ -1,3 +1,13 @@ +## 启动shell脚本 + +```shell +Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$69797ab5] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) +``` + + + + + ```java SpringApplication.run(PrimarySources.class, args); ``` @@ -74,3 +84,7 @@ listeners.starting() DefaultApplicationArguments (new SimpleCommandLineArgsParser()).parse(args) ``` +```java + +``` + diff --git a/spring/事务手动回滚.md b/spring/事务手动回滚.md new file mode 100644 index 0000000..4ddae82 --- /dev/null +++ b/spring/事务手动回滚.md @@ -0,0 +1,24 @@ + + + @Service + public class UserServiceImpl implements UserService { + @Autowired + private DataSourceTransactionManager transactionManager; + @Override + @Transactional + public void save(User user) { + DefaultTransactionDefinition def = new DefaultTransactionDefinition(); + // explicitly setting the transaction name is something that can only be done programmatically + def.setName("SomeTxName"); + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); + + TransactionStatus status = transactionManager.getTransaction(def); + try { + // execute your business logic here + //db operation + } catch (Exception ex) { + transactionManager.rollback(status); + throw ex; + } + } + } \ No newline at end of file diff --git a/从放弃到入门系列.md b/从放弃到入门系列.md index ce0b9dc..f728704 100644 --- a/从放弃到入门系列.md +++ b/从放弃到入门系列.md @@ -23,4 +23,5 @@ 19. 用心做好每件事,每个人每件事,都值得被认真对待。 19. 做个有意义的主键。 19. 制定规范,一不变应万变。 -19. 能在内存中操作,就不去数据库。 \ No newline at end of file +19. 能在内存中操作,就不去数据库。 +19. 遵守规范,争取在里面添加一条,或者找出返例。 \ No newline at end of file