当前位置: 首页 > news >正文

网站建设客网站游戏建设网站

网站建设客网站,游戏建设网站,dede 管理多个网站,58同城湛江网站建设#x1f440;Spring的核心模块 Spring Framework是一个功能强大的开源框架#xff0c;用于构建Java企业级应用程序。它提供了多个核心模块#xff0c;每个模块都提供不同的功能#xff0c;用于解决应用程序开发中的各种问题。以下是Spring Framework的核心模块的全面解析Spring的核心模块 Spring Framework是一个功能强大的开源框架用于构建Java企业级应用程序。它提供了多个核心模块每个模块都提供不同的功能用于解决应用程序开发中的各种问题。以下是Spring Framework的核心模块的全面解析 ✌1. spring-core spring-core是Spring Framework的核心模块之一提供了Spring框架的基本功能包括控制反转IoC和依赖注入DI的实现以及其他一些基础设施。它是其他许多Spring模块的基础为整个Spring生态系统提供了支持。以下是有关spring-core模块的详细解释 ✍1. 控制反转IoC和 依赖注入DI 控制反转IoC spring-core模块实现了IoC容器它是Spring框架的核心概念之一。IoC指的是将对象的创建和依赖关系的管理交给容器来处理而不是在代码中硬编码。Spring的IoC容器负责管理Bean的生命周期、依赖关系以及Bean的装配。 依赖注入DI spring-core模块通过依赖注入将Bean之间的依赖关系注入到容器中从而解耦了组件之间的紧密耦合。使用DI开发者可以通过构造函数、Setter方法或字段注入等方式将依赖关系注入到Bean中。 1. 定义Bean 在Spring Boot中您可以使用Component及其衍生注解如Service、Repository、Controller等来定义Bean。这些注解标记了一个类作为Spring容器中的一个Bean。例如 Service public class MyService {// ... }2. 依赖注入 Spring Boot通过IOC容器来实现依赖注入。您可以使用Autowired注解将依赖对象注入到需要它们的地方。例如在构造函数、Setter方法、字段上使用Autowired注解 Service public class MyService {private final AnotherService anotherService;Autowiredpublic MyService(AnotherService anotherService) {this.anotherService anotherService;}// ... }3. 配置注入 Spring Boot推荐使用Java配置类来配置Bean而不是XML配置文件。您可以使用Configuration注解标记配置类并在方法上使用Bean注解来定义Bean。例如 在Spring Boot中配置类是一种使用Java代码来配置应用程序的方式取代了传统的XML配置文件。配置类使用Configuration注解进行标记通常使用Bean注解来定义Bean以及其他配置相关的注解来实现各种功能。以下是关于配置类在Spring Boot中的详细解释和示例 创建配置类 首先您需要创建一个Java类并使用Configuration注解来标记它使其成为一个配置类。 import org.springframework.context.annotation.Configuration;Configuration public class AppConfig {// Configuration related methods will be defined here }定义Bean 在配置类中您可以使用Bean注解来定义Bean。方法的返回值类型将成为该Bean的类型方法名将成为该Bean的名称。 import org.springframework.context.annotation.Bean;Configuration public class AppConfig {Beanpublic MyService myService() {return new MyService();} }依赖注入其他Bean 在配置类中您可以使用Autowired注解来依赖注入其他Bean就像在其他普通的组件类中一样。 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;Configuration public class AppConfig {Autowiredprivate AnotherService anotherService;Beanpublic MyService myService() {return new MyService(anotherService);} }使用外部属性 配置类还可以使用Value注解来注入外部属性值或者使用ConfigurationProperties注解来绑定属性配置。 import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;Component ConfigurationProperties(prefix app.config) public class AppConfigProperties {private String greetingMessage;private int maxRetry;private int timeout;// Getter and setter methods for the properties } import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext;SpringBootApplication public class Application {Autowiredprivate AppConfigProperties appConfig;public static void main(String[] args) {ConfigurableApplicationContext context SpringApplication.run(Application.class, args);Application application context.getBean(Application.class);application.printConfig();}public void printConfig() {System.out.println(Greeting message: appConfig.getGreetingMessage());System.out.println(Max retry: appConfig.getMaxRetry());System.out.println(Timeout: appConfig.getTimeout());} } 组合配置类 多个配置类可以组合在一起形成一个综合的配置。Spring Boot会自动将这些配置类合并成一个应用程序上下文。 Configuration public class DatabaseConfig {// Define database-related beans and configurations }Configuration public class MessagingConfig {// Define messaging-related beans and configurations }Configuration Import({DatabaseConfig.class, MessagingConfig.class}) public class AppConfig {// Main configuration and other beans }import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class MainApp {public static void main(String[] args) {AnnotationConfigApplicationContext context new AnnotationConfigApplicationContext(AppConfig.class);// Get and use beans from the application context} }总结组合配置类是一种将多个配置类组合在一起的方法使配置更加清晰、模块化和可维护。通过创建主配置类并使用Import注解来导入其他配置类您可以在应用程序中有效地管理配置。这种方法有助于降低配置的复杂性并提高了应用程序的可扩展性和可维护性。 4. 属性注入 除了构造函数和Setter方法注入外您还可以使用Autowired注解在属性上进行依赖注入。这样Spring Boot会自动将依赖注入到属性中。 Service public class MyService {Autowiredprivate AnotherService anotherService;// ... }5. Qualifiers和Primary 如果存在多个实现相同接口的Bean您可以使用Qualifier注解指定要注入的具体Bean。另外您还可以使用Primary注解标记一个主要的Bean它将被首选用于注入。 当您在Spring Boot中使用依赖注入时可能会遇到多个相同类型的Bean的情况例如有多个实现了相同接口的类。在这种情况下为了告诉Spring容器应该注入哪个具体的Bean您可以使用Qualifier注解和Primary注解。 Qualifier 注解 Qualifier注解用于在存在多个相同类型的Bean时明确指定要注入的Bean的名称。通过指定Qualifier的值您可以告诉Spring容器应该注入哪个具体的Bean。 示例 Service public class MyService {private final AnotherService primaryAnotherService;private final AnotherService secondaryAnotherService;Autowiredpublic MyService(Qualifier(primary) AnotherService primaryAnotherService,Qualifier(secondary) AnotherService secondaryAnotherService) {this.primaryAnotherService primaryAnotherService;this.secondaryAnotherService secondaryAnotherService;}// ... }Primary 注解 Primary注解用于标记一个Bean为首选的Bean当存在多个相同类型的Bean时被标记为Primary的Bean将会被优先注入。如果没有指定Qualifier那么Primary注解的Bean将作为默认选择。 示例 Service Primary public class PrimaryAnotherService implements AnotherService {// ... }Service public class SecondaryAnotherService implements AnotherService {// ... }Service public class MyService {private final AnotherService anotherService;Autowiredpublic MyService(AnotherService anotherService) {this.anotherService anotherService;}// ... }总结在Spring Boot中当存在多个相同类型的Bean时您可以使用Qualifier注解来指定要注入的具体Bean。另外您还可以使用Primary注解来标记一个Bean为首选Bean优先注入。这些注解有助于在多个候选Bean之间进行选择从而满足您的依赖注入需求。 6. 可选依赖 如果依赖对象可能不存在您可以使用Autowired(required false)来标记注入的依赖为可选的。如果找不到匹配的Bean注入的字段将为null。 Service public class MyService {Autowired(required false)private AnotherService optionalAnotherService;// ... }7. 属性值注入 Spring Boot还支持将属性值注入到Bean中您可以使用Value注解来实现。这在配置文件中配置的属性值会注入到Bean的字段或方法参数中。 属性值注入是一种在Spring Boot中将外部属性值注入到Bean中的机制。这样您可以将配置信息从代码中分离出来使您的应用程序更加灵活和可配置。Spring Boot通过Value注解来实现属性值注入。 以下是属性值注入在Spring Boot中的详细解释和示例 在配置文件中定义属性值 首先在application.properties或application.yml配置文件中定义属性值。例如在application.properties文件中 app.greeting.messageHello from properties file!在Bean中使用 Value 注解 然后您可以在Bean的字段、构造函数参数、方法参数等地方使用Value注解来注入属性值。注解的值是从配置文件中读取的属性的名称。 示例 Service public class MyService {Value(${app.greeting.message})private String greetingMessage;public void printGreeting() {System.out.println(greetingMessage);} }使用 SpEL 表达式 Value注解还支持使用Spring表达式语言SpEL来计算属性值。您可以在注解中使用${expression}来引用SpEL表达式。 示例 Service public class MyService {Value(#{T(java.lang.Math).random()})private double randomNumber;public void printRandomNumber() {System.out.println(randomNumber);} }默认值 如果属性在配置文件中不存在您可以使用Value注解的defaultValue属性来设置默认值。 示例 Service public class MyService {Value(${app.nonexistent.property:Default Value})private String defaultValue;public void printDefaultValue() {System.out.println(defaultValue);} }总结属性值注入是Spring Boot中的一个重要特性通过使用Value注解您可以将外部属性值注入到Bean中从而使您的应用程序更具灵活性和可配置性。您可以从配置文件中读取属性值并使用SpEL表达式进行计算。另外您还可以设置默认值以防属性在配置文件中不存在。 8. 集合注入 如果需要注入多个相同类型的依赖您可以使用List、Set、Map等集合来进行集合注入。 在Spring Boot中集合注入是一种将多个相同类型的依赖对象注入到一个集合中的机制。这种机制非常适用于场景其中您需要处理多个实现了相同接口或父类的依赖对象。Spring Boot通过使用Autowired注解结合List、Set、Map等集合类型使集合注入变得非常方便。 以下是集合注入在Spring Boot中的详细解释和示例 定义多个实现类 首先假设您有多个实现了同一个接口的类例如 public interface Plugin {void execute(); }Service public class PluginA implements Plugin {// ... }Service public class PluginB implements Plugin {// ... }在Bean中使用集合注入 然后您可以在需要依赖注入这些实现类的Bean中使用Autowired注解结合集合类型如ListPlugin、SetPlugin、MapString, Plugin等进行集合注入。 示例 Service public class PluginManager {private final ListPlugin plugins;Autowiredpublic PluginManager(ListPlugin plugins) {this.plugins plugins;}public void executeAllPlugins() {for (Plugin plugin : plugins) {plugin.execute();}} }在上面的示例中PluginManager类通过构造函数注入了一个ListPlugin集合该集合包含了所有实现了Plugin接口的Bean。集合注入是Spring Boot中的一个强大特性它允许您将多个相同类型的依赖对象注入到一个集合中使您能够方便地处理多个实现了相同接口或父类的依赖。通过使用Autowired注解和集合类型您可以在Bean中轻松地实现集合注入从而更好地管理和处理依赖关系。 总结在Spring Boot应用程序中依赖注入是一种重要的设计模式通过使用Autowired、Qualifier、Value等注解您可以实现将依赖关系从代码中解耦从而使代码更具可维护性和可扩展性。结合Spring Boot的自动配置功能依赖注入可以更加便捷地实现使您能够更专注于业务逻辑的开发。 ✍2. BeanFactory 在Spring Boot中BeanFactory是Spring Framework的核心接口之一用于管理和维护应用程序中的Bean实例。它是Spring IoC容器的基础负责创建、初始化、装配和管理Bean。在Spring Boot中BeanFactory的实现主要是DefaultListableBeanFactory。以下是结合Spring Boot详细解释BeanFactory的重要概念和用法 1. BeanFactory接口 BeanFactory接口是Spring IoC容器的根接口用于从容器中获取Bean。 import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;Component public class MyComponent {private final BeanFactory beanFactory;Autowiredpublic MyComponent(BeanFactory beanFactory) {this.beanFactory beanFactory;}public void useBean() {MyService myService beanFactory.getBean(MyService.class);myService.doSomething();} }2. 默认实现 在Spring Boot中默认的BeanFactory实现是DefaultListableBeanFactory。 3. Bean的生命周期 BeanFactory负责管理Bean的生命周期如实例化、属性注入和初始化。这个过程会根据配置进行。 4. Bean的获取 使用getBean()方法从容器中获取Bean。 MyService myService beanFactory.getBean(MyService.class);5. 延迟初始化 默认情况下BeanFactory支持延迟初始化只有在需要时才创建Bean实例。 6. 扩展和自定义 您可以实现BeanPostProcessor接口来自定义Bean的创建和初始化过程。 import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.stereotype.Component;Component public class MyBeanPostProcessor implements BeanPostProcessor {// Override methods for customizing bean initialization }7. 注解配置 使用注解定义Bean。 import org.springframework.stereotype.Component;Component public class MyService {// Bean implementation }8. 注解扫描 使用注解扫描自动注册标记了Component的Bean。 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext;SpringBootApplication public class Application {public static void main(String[] args) {ConfigurableApplicationContext context SpringApplication.run(Application.class, args);MyService myService context.getBean(MyService.class);myService.doSomething();} }9. 自动装配 使用Autowired注解进行自动装配。 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;Component public class MyController {private final MyService myService;Autowiredpublic MyController(MyService myService) {this.myService myService;}// Controller logic }✍3. 如何使用BeanFactory 在Spring Boot中BeanFactory是Spring IoC容器的根接口。它提供了配置框架和基本功能如获取Bean的实例。 1. 注入BeanFactory: 我们可以通过实现BeanFactoryAware接口或者直接在Bean中注入BeanFactory来使用它。 Component public class ExampleBean implements BeanFactoryAware {private BeanFactory beanFactory;Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {this.beanFactory beanFactory;}public void doSomething() {// 使用beanFactoryAnotherBean anotherBean (AnotherBean) beanFactory.getBean(AnotherBean.class);anotherBean.doSomething();}}Component public class AnotherBean {public void doSomething() {System.out.println(AnotherBean doSomething 方法被调用);}}在这个示例中ExampleBean实现了BeanFactoryAware接口这样Spring容器会自动注入BeanFactory。然后在doSomething方法中我们使用beanFactory获取AnotherBean的实例并调用它的doSomething方法。 2. 使用Autowired注解: 我们可以使用Autowired注解直接在Bean中注入BeanFactory。 Component public class ExampleBean {Autowiredprivate BeanFactory beanFactory;public void doSomething() {// 使用beanFactoryAnotherBean anotherBean (AnotherBean) beanFactory.getBean(AnotherBean.class);anotherBean.doSomething();}}Component public class AnotherBean {public void doSomething() {System.out.println(AnotherBean doSomething 方法被调用);}}在这个示例中我们使用Autowired注解直接在ExampleBean中注入BeanFactory。然后在doSomething方法中我们使用beanFactory获取AnotherBean的实例并调用它的doSomething方法。 ✍4. ApplicationContext 当在Spring Boot中使用ApplicationContext时您可以遵循以下详细示例来了解每个概念和用法同时考虑了Spring Boot的自动配置和便利性 1. ApplicationContext接口 ApplicationContext是Spring IoC容器的更高级别接口除了BeanFactory的功能外还提供了更多的功能如国际化、事件传播等。 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component;Component public class MyComponent {private final ApplicationContext applicationContext;Autowiredpublic MyComponent(ApplicationContext applicationContext) {this.applicationContext applicationContext;}public void useBean() {MyService myService applicationContext.getBean(MyService.class);myService.doSomething();} }2. 默认实现 在Spring Boot中默认的ApplicationContext实现是AnnotationConfigApplicationContext用于基于注解的配置。 3. Bean的生命周期 ApplicationContext也负责管理Bean的生命周期与BeanFactory相同。 4. Bean的获取 使用getBean()方法从容器中获取Bean与BeanFactory相同。 MyService myService applicationContext.getBean(MyService.class);5. 延迟初始化 默认情况下ApplicationContext同样支持延迟初始化只有在需要时才创建Bean实例。 6. 扩展和自定义 您可以实现BeanPostProcessor接口等来自定义Bean的创建和初始化过程与BeanFactory相同。 7. 注解配置 使用注解定义Bean。 import org.springframework.stereotype.Component;Component public class MyService {// Bean implementation }8. 注解扫描 使用注解扫描自动注册标记了Component的Bean与BeanFactory相同。 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext;SpringBootApplication public class Application {public static void main(String[] args) {ConfigurableApplicationContext context SpringApplication.run(Application.class, args);MyService myService context.getBean(MyService.class);myService.doSomething();} }9. 自动装配 使用Autowired注解进行自动装配与BeanFactory相同。 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;Component public class MyController {private final MyService myService;Autowiredpublic MyController(MyService myService) {this.myService myService;}// Controller logic }通过这些详细示例您可以更深入地理解在Spring Boot中如何使用ApplicationContext来管理和使用Bean同时利用Spring Boot的自动配置和便利性。ApplicationContext提供了更多的功能如事件传播和国际化使得应用程序更加灵活和功能丰富。 ✍5. 如何使用ApplicationContext 在Spring Boot中ApplicationContext是一个中央接口它提供了应用程序运行时的配置信息。ApplicationContext是BeanFactory的子接口它添加了更多的企业级功能比如从一个属性文件中解析文本信息。 1. 注入ApplicationContext: 我们可以通过实现ApplicationContextAware接口或者直接在Bean中注入ApplicationContext来使用它。 Component public class ExampleBean implements ApplicationContextAware {private ApplicationContext applicationContext;Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext applicationContext;}public void doSomething() {// 使用applicationContextAnotherBean anotherBean (AnotherBean) applicationContext.getBean(AnotherBean.class);anotherBean.doSomething();}}Component public class AnotherBean {public void doSomething() {System.out.println(AnotherBean doSomething 方法被调用);}}在这个示例中ExampleBean实现了ApplicationContextAware接口这样Spring容器会自动注入ApplicationContext。然后在doSomething方法中我们使用applicationContext获取AnotherBean的实例并调用它的doSomething方法。 2. 使用Autowired注解: 我们可以使用Autowired注解直接在Bean中注入ApplicationContext。 Component public class ExampleBean {Autowiredprivate ApplicationContext applicationContext;public void doSomething() {// 使用applicationContextAnotherBean anotherBean (AnotherBean) applicationContext.getBean(AnotherBean.class);anotherBean.doSomething();}}Component public class AnotherBean {public void doSomething() {System.out.println(AnotherBean doSomething 方法被调用);}}在这个示例中我们使用Autowired注解直接在ExampleBean中注入ApplicationContext。然后在doSomething方法中我们使用applicationContext获取AnotherBean的实例并调用它的doSomething方法。 3. 使用ApplicationListener接口: 我们可以实现ApplicationListener接口来监听应用程序的事件。 Component public class ExampleBean implements ApplicationListenerContextRefreshedEvent {Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {System.out.println(应用程序上下文刷新);}}在这个示例中ExampleBean实现了ApplicationListener接口这样它会监听ContextRefreshedEvent事件。当应用程序上下文刷新时onApplicationEvent方法会被调用。 ✍4. Bean的生命周期 当然我会详细描述每一步的作用。 1. 实例化Bean: 这是Bean生命周期的第一步。Spring容器通过反射机制创建Bean的实例。public class ExampleBean {// ... }2. 设置Bean的属性: Spring容器将根据配置文件设置Bean的属性。这是通过调用Bean的setter方法完成的。public class ExampleBean {private String name;public void setName(String name) {this.name name;}// ... }3. 调用BeanNameAware的setBeanName方法: 如果Bean实现了BeanNameAware接口Spring容器将调用setBeanName方法传递Bean的ID。这允许Bean知道它在Spring容器中的ID。public class ExampleBean implements BeanNameAware {private String beanName;Overridepublic void setBeanName(String name) {this.beanName name;}// ... }4. 调用BeanFactoryAware的setBeanFactory方法: 如果Bean实现了BeanFactoryAware接口Spring容器将调用setBeanFactory方法传递自身。这允许Bean知道它所属的容器。public class ExampleBean implements BeanFactoryAware {private BeanFactory beanFactory;Overridepublic void setBeanFactory(BeanFactory beanFactory) {this.beanFactory beanFactory;}// ... }5. 调用ApplicationContextAware的setApplicationContext方法: 如果Bean实现了ApplicationContextAware接口Spring容器将调用setApplicationContext方法传递自身。这允许Bean知道它所属的应用上下文。public class ExampleBean implements ApplicationContextAware {private ApplicationContext applicationContext;Overridepublic void setApplicationContext(ApplicationContext applicationContext) {this.applicationContext applicationContext;}// ... }6. 调用BeanPostProcessor的postProcessBeforeInitialization方法: 如果Bean实现了BeanPostProcessor接口Spring容器将在初始化方法之前调用postProcessBeforeInitialization方法。这允许对Bean的属性进行更改。public class CustomBeanPostProcessor implements BeanPostProcessor {Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {// ...return bean;}// ... }7. 调用InitializingBean的afterPropertiesSet方法: 如果Bean实现了InitializingBean接口Spring容器将在所有属性设置完成后调用afterPropertiesSet方法。public class ExampleBean implements InitializingBean {Overridepublic void afterPropertiesSet() throws Exception {// ...}// ... }8. 调用自定义的初始化方法: 如果在Bean的定义中指定了自定义的初始化方法Spring容器将调用它。public class ExampleBean {public void init() {// ...}// ... }9. 调用BeanPostProcessor的postProcessAfterInitialization方法: 如果Bean实plement了BeanPostProcessor接口Spring容器将在初始化方法之后调用postProcessAfterInitialization方法。public class CustomBeanPostProcessor implements BeanPostProcessor {Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {// ...return bean;}// ... }10. Bean的使用: 现在Bean已经准备好了可以被应用程序使用。 11. 调用DisposableBean的destroy方法: 如果Bean实现了DisposableBean接口Spring容器将在容器关闭时调用destroy方法。public class ExampleBean implements DisposableBean {Overridepublic void destroy() throws Exception {// ...}// ... }12. 调用自定义的销毁方法: 如果在Bean的定义中指定了自定义的销毁方法Spring容器将在容器关闭时调用它。public class ExampleBean {public void destroy() {// ...}// ... }13. 下面是一个简单的Java配置示例 Configuration public class AppConfig {Bean(initMethod init, destroyMethod destroy)public ExampleBean exampleBean() {return new ExampleBean();}}在这个示例中ExampleBean是一个简单的Java类它有init和destroy方法分别作为初始化和销毁方法。 14. 流程图如下 ------------------- | 实例化Bean | -------------------|v ------------------- | 设置Bean的属性 | -------------------|v ------------------- | 调用BeanNameAware | | 的setBeanName方法 | -------------------|v ------------------- | 调用BeanFactoryAware| | 的setBeanFactory方法 | -------------------|v ------------------- | 调用ApplicationContextAware| | 的setApplicationContext方法 | -------------------|v ------------------- | 调用BeanPostProcessor | | 的postProcessBeforeInitialization方法 | -------------------|v ------------------- | 调用InitializingBean| | 的afterPropertiesSet方法 | -------------------|v ------------------- | 调用自定义的初始化方法 | -------------------|v ------------------- | 调用BeanPostProcessor | | 的postProcessAfterInitialization方法 | -------------------|v ------------------- | Bean的使用 | -------------------|v ------------------- | 调用DisposableBean | | 的destroy方法 | -------------------|v ------------------- | 调用自定义的销毁方法 | -------------------✍5. 如何使用Bean的生命周期 在Spring Boot中我们可以使用Java配置、注解和自定义的BeanPostProcessor来使用Bean的生命周期。 1. 使用Java配置: 我们可以使用Bean注解的initMethod和destroyMethod属性来指定Bean的初始化和销毁方法。 Configuration public class AppConfig {Bean(initMethod init, destroyMethod destroy)public ExampleBean exampleBean() {return new ExampleBean();}}public class ExampleBean {public void init() {System.out.println(ExampleBean 初始化);}public void destroy() {System.out.println(ExampleBean 销毁);}}在这个示例中ExampleBean是一个简单的Java类它有init和destroy方法分别作为初始化和销毁方法。 2. 使用PostConstruct和PreDestroy注解: 我们可以使用PostConstruct注解来标记Bean的初始化方法使用PreDestroy注解来标记Bean的销毁方法。 Component public class ExampleBean {PostConstructpublic void init() {System.out.println(ExampleBean 初始化);}PreDestroypublic void destroy() {System.out.println(ExampleBean 销毁);}}在这个示例中ExampleBean是一个简单的Java类它有init和destroy方法分别作为初始化和销毁方法。 3. 使用InitializingBean和DisposableBean接口: 我们可以让Bean实现InitializingBean接口和DisposableBean接口。 Component public class ExampleBean implements InitializingBean, DisposableBean {Overridepublic void afterPropertiesSet() throws Exception {System.out.println(ExampleBean 初始化);}Overridepublic void destroy() throws Exception {System.out.println(ExampleBean 销毁);}}在这个示例中ExampleBean是一个简单的Java类它实现了InitializingBean接口和DisposableBean接口。 4. 使用自定义的BeanPostProcessor: 我们可以创建一个自定义的BeanPostProcessor并在postProcessBeforeInitialization方法和postProcessAfterInitialization方法中添加自定义的逻辑。 Component public class CustomBeanPostProcessor implements BeanPostProcessor {Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {if (bean instanceof ExampleBean) {System.out.println(ExampleBean 初始化之前);}return bean;}Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {if (bean instanceof ExampleBean) {System.out.println(ExampleBean 初始化之后);}return bean;}}Component public class ExampleBean {// ...}在这个示例中CustomBeanPostProcessor是一个自定义的BeanPostProcessor它在ExampleBean的初始化之前和之后添加了自定义的逻辑。 ✍6. AOP 在Spring Boot中AOPAspect-Oriented Programming面向切面编程是一种编程范式它允许我们将横切关注点如日志、事务管理从业务逻辑中分离出来。Spring框架提供了一个AOP框架它允许我们定义切面、连接点、通知、切点等。 1. 切面Aspect: 切面是一个模块化的关注点。它定义了通知和切点。 示例 Aspect Component public class LoggingAspect {// ...}在这个示例中LoggingAspect是一个切面它定义了通知和切点。 2. 连接点Joinpoint: 连接点是程序执行的某个位置如方法的调用或异常的抛出。 示例 AfterReturning(pointcut serviceMethods(), returning result) public void logAfterReturning(JoinPoint joinPoint, Object result) {// ... }在这个示例中logAfterReturning方法的joinPoint参数是一个连接点。 3. 通知Advice: 通知是切面的一部分它是在特定的连接点上执行的代码。有五种类型的通知前置通知、后置通知、返回通知、异常通知、环绕通知。 示例 Before(serviceMethods()) public void logBefore(JoinPoint joinPoint) {// ... }After(serviceMethods()) public void logAfter(JoinPoint joinPoint) {// ... }AfterReturning(pointcut serviceMethods(), returning result) public void logAfterReturning(JoinPoint joinPoint, Object result) {// ... }AfterThrowing(pointcut serviceMethods(), throwing exception) public void logAfterThrowing(JoinPoint joinPoint, Throwable exception) {// ... }Around(serviceMethods()) public Object logAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {// ... }在这个示例中logBefore方法是一个前置通知logAfter方法是一个后置通知logAfterReturning方法是一个返回通知logAfterThrowing方法是一个异常通知logAround方法是一个环绕通知。 4. 切点Pointcut: 切点是一组连接点的集合。它定义了通知应该在哪些连接点上执行。 示例 Pointcut(execution(* com.example.service.*.*(..))) public void serviceMethods() {}在这个示例中serviceMethods方法是一个切点它匹配com.example.service包下的所有方法。 5. 目标对象Target Object: 目标对象是被一个或多个切面通知的对象。 示例 Service public class ExampleService {public String doSomething(String name) {return Hello, name;}}在这个示例中ExampleService是一个目标对象它被LoggingAspect切面通知。 6. 代理Proxy: 代理是一个对象它是目标对象的代理它将调用转发到目标对象并在调用之前或之后执行通知。 在上面的示例中当我们调用ExampleService的doSomething方法时实际上是调用了它的代理对象的doSomething方法。 7. 织入Weaving: 织入是将切面和目标对象连接在一起的过程。 在Spring Boot中AOP的织入过程是在Spring容器完成Bean的初始化之后也就是在postProcessAfterInitialization阶段。 这些是Spring Boot中AOP的各个组成部分的示例和解释。在这个示例中AOP的织入过程是在Spring容器完成Bean的初始化之后也就是在postProcessAfterInitialization阶段。这是因为AOP需要在目标对象被完全初始化之后才能创建它的代理对象。 8. AopProxyUtils: AopProxyUtils类是Spring AOP框架的一个工具类它提供了一些静态方法用于处理代理对象和目标对象。 getSingletonTarget(Object candidate): 这个方法用于获取单例bean的目标对象。 示例 ExampleService targetObject (ExampleService) AopProxyUtils.getSingletonTarget(exampleService);在这个示例中exampleService是ExampleService的代理对象。我们使用AopProxyUtils.getSingletonTarget方法获取exampleService的目标对象。 注意这个方法只适用于单例bean。如果bean的作用域不是单例这个方法将返回null。 getTargetClass(Object candidate): 这个方法用于获取代理对象的目标类。 示例 Class? targetClass AopProxyUtils.getTargetClass(exampleService);在这个示例中exampleService是ExampleService的代理对象。我们使用AopProxyUtils.getTargetClass方法获取exampleService的目标类。 注意这个方法返回的是目标类而不是目标对象。 ultimateTargetClass(Object candidate): 这个方法用于获取代理对象的最终目标类。 示例 Class? ultimateTargetClass AopProxyUtils.ultimateTargetClass(exampleService);在这个示例中exampleService是ExampleService的代理对象。我们使用AopProxyUtils.ultimateTargetClass方法获取exampleService的最终目标类。 注意这个方法返回的是最终目标类而不是目标对象。如果代理对象有多层代理这个方法将返回最终的目标类。 这些是AopProxyUtils类的一些常用方法。这个类还有一些其他方法但它们通常不需要在应用程序代码中直接使用。 注意通常我们不需要直接访问目标对象。代理对象会将调用转发到目标对象并在调用之前或之后执行通知。所以通常我们应该使用代理对象而不是目标对象。直接访问目标对象会绕过代理这意味着切面的通知将不会被执行。 这个类还包含一些其他的方法但是它们主要用于内部使用通常不需要在应用程序代码中直接使用。例如AopProxyUtils.completeProxiedInterfaces方法用于确定给定的代理配置的完整代理接口集包括从目标类继承的接口。这个方法通常用于在创建代理对象时确定代理接口。 ✍7. 事件处理 在Spring框架中事件处理是通过ApplicationEvent类和ApplicationListener接口实现的。 1. ApplicationEvent类: ApplicationEvent是一个抽象类它是所有应用程序事件的基类。 示例 public class CustomEvent extends ApplicationEvent {private String message;public CustomEvent(Object source, String message) {super(source);this.message message;}public String getMessage() {return message;}}在这个示例中我们创建了一个CustomEvent类它继承了ApplicationEvent类。CustomEvent类包含一个message属性它可以用于传递事件的信息。 2. ApplicationListener接口: ApplicationListener是一个接口它定义了一个onApplicationEvent方法这个方法会在事件发布时被调用。 示例 Component public class CustomEventListener implements ApplicationListenerCustomEvent {Overridepublic void onApplicationEvent(CustomEvent event) {System.out.println(Received custom event - event.getMessage());}}在这个示例中我们创建了一个CustomEventListener类它实现了ApplicationListener接口。CustomEventListener类定义了onApplicationEvent方法这个方法会在CustomEvent事件发布时被调用。 3. 事件发布: 事件可以通过ApplicationEventPublisher接口的publishEvent方法发布。 示例 Service public class ExampleService {Autowiredprivate ApplicationEventPublisher applicationEventPublisher;public void doSomething() {System.out.println(Doing something...);CustomEvent customEvent new CustomEvent(this, This is a custom event);applicationEventPublisher.publishEvent(customEvent);}}在这个示例中ExampleService类包含一个doSomething方法这个方法会发布一个CustomEvent事件。 这是一个简单的Spring Boot应用程序示例它包括事件的创建、监听和发布。 注意Spring 4.2引入了一个新的EventListener注解它可以用于简化事件监听。使用EventListener注解我们可以直接在方法上定义事件监听而不需要实现ApplicationListener接口。 示例 Component public class CustomEventListener {EventListenerpublic void handleCustomEvent(CustomEvent event) {System.out.println(Received custom event - event.getMessage());}}在这个示例中我们使用EventListener注解定义了一个handleCustomEvent方法这个方法会在CustomEvent事件发布时被调用。 ✍8. 资源管理: 在Spring框架中资源管理是通过Resource接口和ResourceLoader接口实现的。 1. Resource接口: Resource是一个接口它定义了一些方法用于访问底层资源的内容。 Spring提供了一些Resource接口的实现类例如ClassPathResource、FileSystemResource、UrlResource等。 示例 Service public class ExampleService {Autowiredprivate ResourceLoader resourceLoader;public void doSomething() throws IOException {Resource resource resourceLoader.getResource(classpath:example.txt);String content new String(Files.readAllBytes(resource.getFile().toPath()));System.out.println(content);}}在这个示例中ExampleService类包含一个doSomething方法这个方法会读取classpath:example.txt文件的内容并打印到控制台。 2. ResourceLoader接口: ResourceLoader是一个接口它定义了一个getResource方法用于加载资源。 ApplicationContext接口扩展了ResourceLoader接口因此ApplicationContext也可以用作ResourceLoader。 示例 Autowired private ResourceLoader resourceLoader;在这个示例中我们注入了一个ResourceLoader并使用它的getResource方法加载资源。 这是一个简单的Spring Boot应用程序示例它包括资源的加载和读取。 注意ResourceLoader的getResource方法根据资源路径的前缀来确定使用哪个Resource实现类。例如classpath:前缀会使用ClassPathResource实现类file:前缀会使用FileSystemResource实现类没有前缀会使用ServletContextResource实现类。 另外Value注解也可以用于注入资源。 示例 Service public class ExampleService {Value(classpath:example.txt)private Resource resource;public void doSomething() throws IOException {String content new String(Files.readAllBytes(resource.getFile().toPath()));System.out.println(content);}}在这个示例中我们使用Value注解注入了一个Resource并读取它的内容。 ✍9. 验证: 在Spring框架中验证是通过Validator接口实现的。 1. Validator接口: Validator是一个接口它定义了两个方法supports和validate。 supports方法用于检查给定的类是否可以被这个验证器验证。 validate方法用于验证给定的对象。 示例 public class PersonValidator implements Validator {Overridepublic boolean supports(Class? clazz) {return Person.class.equals(clazz);}Overridepublic void validate(Object target, Errors errors) {Person person (Person) target;if (person.getAge() 0) {errors.rejectValue(age, negativevalue);} else if (person.getAge() 110) {errors.rejectValue(age, too.darn.old);}}}在这个示例中我们创建了一个PersonValidator类它实现了Validator接口。PersonValidator类定义了supports方法和validate方法。 2. 使用Validator: Validator可以在服务层或控制层使用。 示例 Service public class PersonService {private final Validator validator;Autowiredpublic PersonService(Validator validator) {this.validator validator;}public void doSomething(Person person) {DataBinder dataBinder new DataBinder(person);dataBinder.setValidator(validator);dataBinder.validate();BindingResult bindingResult dataBinder.getBindingResult();if (bindingResult.hasErrors()) {System.out.println(bindingResult.getAllErrors());} else {System.out.println(Person is valid);}}}在这个示例中PersonService类包含一个doSomething方法这个方法会验证Person对象。 这是一个简单的Spring Boot应用程序示例它包括验证的创建和使用。 注意Spring Boot自动配置了一个LocalValidatorFactoryBean它是javax.validation.Validator的一个实现。因此我们可以直接注入javax.validation.Validator而不需要自定义一个Validator。 另外Spring MVC也支持javax.validation。我们可以在控制器的处理方法参数上添加Valid注解然后Spring MVC会在绑定请求参数到模型对象之后对模型对象进行验证。 示例 RestController public class PersonController {PostMapping(/persons)public ResponseEntity? createPerson(Valid RequestBody Person person, BindingResult bindingResult) {if (bindingResult.hasErrors()) {return ResponseEntity.badRequest().body(bindingResult.getAllErrors());}// ...return ResponseEntity.ok(person);}}在这个示例中createPerson方法会验证Person对象。如果Person对象不合法createPerson方法会返回一个包含所有错误的响应。 ✍10. 数据绑定: 在Spring框架中数据绑定是通过DataBinder类实现的。 1. DataBinder类: DataBinder是一个类它用于绑定请求参数到目标对象并对目标对象进行验证。 示例 Service public class PersonService {private final Validator validator;Autowiredpublic PersonService(Validator validator) {this.validator validator;}public void doSomething(MapString, String parameters) {Person person new Person();DataBinder dataBinder new DataBinder(person);dataBinder.setValidator(validator);dataBinder.bind(new MutablePropertyValues(parameters));dataBinder.validate();BindingResult bindingResult dataBinder.getBindingResult();if (bindingResult.hasErrors()) {System.out.println(bindingResult.getAllErrors());} else {System.out.println(Person is valid);}}}在这个示例中PersonService类包含一个doSomething方法这个方法会绑定请求参数到Person对象并对Person对象进行验证。 2. 使用DataBinder: DataBinder可以在服务层或控制层使用。 示例 RestController public class PersonController {private final PersonService personService;Autowiredpublic PersonController(PersonService personService) {this.personService personService;}PostMapping(/persons)public ResponseEntity? createPerson(RequestBody MapString, String parameters) {personService.doSomething(parameters);return ResponseEntity.ok().build();}}在这个示例中PersonController类包含一个createPerson方法这个方法会调用PersonService的doSomething方法。 这是一个简单的Spring Boot应用程序示例它包括数据绑定的创建和使用。 注意Spring MVC也支持数据绑定。我们可以在控制器的处理方法参数上添加ModelAttribute注解然后Spring MVC会绑定请求参数到模型对象。 示例 RestController public class PersonController {PostMapping(/persons)public ResponseEntity? createPerson(ModelAttribute Person person, BindingResult bindingResult) {if (bindingResult.hasErrors()) {return ResponseEntity.badRequest().body(bindingResult.getAllErrors());}// ...return ResponseEntity.ok(person);}}在这个示例中createPerson方法会绑定请求参数到Person对象。如果Person对象不合法createPerson方法会返回一个包含所有错误的响应。 ✍11. 类型转换: 在Spring框架中类型转换是通过ConversionService接口实现的。 1. ConversionService接口: ConversionService是一个接口它定义了两个方法canConvert和convert。 canConvert方法用于检查一个类型是否可以转换到另一个类型。 convert方法用于将一个对象转换到另一个类型。 示例 Service public class ExampleService {private final ConversionService conversionService;Autowiredpublic ExampleService(ConversionService conversionService) {this.conversionService conversionService;}public void doSomething(String input) {if (conversionService.canConvert(String.class, Integer.class)) {Integer output conversionService.convert(input, Integer.class);System.out.println(output);} else {System.out.println(Cannot convert from String to Integer);}}}在这个示例中ExampleService类包含一个doSomething方法这个方法会将一个String对象转换到Integer对象。 2. 使用ConversionService: ConversionService可以在服务层或控制层使用。 示例 RestController public class ExampleController {private final ExampleService exampleService;Autowiredpublic ExampleController(ExampleService exampleService) {this.exampleService exampleService;}GetMapping(/example)public ResponseEntity? getExample(RequestParam String input) {exampleService.doSomething(input);return ResponseEntity.ok().build();}}在这个示例中ExampleController类包含一个getExample方法这个方法会调用ExampleService的doSomething方法。 这是一个简单的Spring Boot应用程序示例它包括类型转换的创建和使用。 注意Spring Boot自动配置了一个ConversionService它是DefaultConversionService的一个实例。因此我们可以直接注入ConversionService而不需要自定义一个ConversionService。 另外我们也可以自定义类型转换器。自定义类型转换器需要实现Converter接口。 示例 public class StringToIntegerConverter implements ConverterString, Integer {Overridepublic Integer convert(String source) {return Integer.valueOf(source);}}在这个示例中我们创建了一个StringToIntegerConverter类它实现了Converter接口。StringToIntegerConverter类定义了convert方法这个方法会将一个String对象转换到Integer对象。 然后我们可以将自定义的类型转换器添加到ConversionService。 示例 Bean public ConversionService conversionService() {DefaultConversionService conversionService new DefaultConversionService();conversionService.addConverter(new StringToIntegerConverter());return conversionService; }在这个示例中我们创建了一个ConversionService的Bean并将StringToIntegerConverter添加到ConversionService。 ✍12. 表达式语言 Spring Expression Language (SpEL) 是Spring框架的一个强大的表达式语言它可以用于查询和操作对象图。 1. SpEL表达式: SpEL表达式是一个字符串它可以包含字面量、属性、方法、数组、集合、索引器、运算符、变量、类型等。 示例 String expression name John Doe;在这个示例中expression是一个SpEL表达式它比较name属性的值是否等于John Doe。 2. Expression接口: Expression是一个接口它定义了一些方法用于解析和求值SpEL表达式。 示例 ExpressionParser parser new SpelExpressionParser(); Expression expression parser.parseExpression(name John Doe);在这个示例中我们创建了一个SpelExpressionParser对象并使用它的parseExpression方法解析SpEL表达式。 3. 求值SpEL表达式: SpEL表达式可以在一个上下文中求值。上下文可以是一个对象、一个数组、一个集合、一个字典等。 示例 Person person new Person(); person.setName(John Doe); Boolean result expression.getValue(person, Boolean.class); System.out.println(result);在这个示例中我们创建了一个Person对象并设置它的name属性。然后我们使用Expression的getValue方法在Person对象的上下文中求值SpEL表达式。 这是一个简单的Spring Boot应用程序示例它包括SpEL表达式的创建、解析和求值。 注意SpEL也可以用于配置文件、注解、XML配置等。 示例 Value(#{systemProperties[java.home]}) private String javaHome;在这个示例中我们使用Value注解和SpEL表达式注入java.home系统属性的值。 另外SpEL也可以用于Query注解。 示例 Repository public interface PersonRepository extends JpaRepositoryPerson, Long {Query(select p from Person p where p.name :#{#person.name})ListPerson findByName(Param(person) Person person);}在这个示例中我们使用Query注解和SpEL表达式定义了一个查询方法。 ✍13. 国际化 Spring框架通过MessageSource接口支持国际化这样应用程序可以根据不同的地区显示不同的消息。 1. MessageSource接口: MessageSource接口定义了几个方法来解析消息。最常用的是getMessage方法它可以根据消息的键、参数和地区解析消息。 示例 Service public class ExampleService {private final MessageSource messageSource;Autowiredpublic ExampleService(MessageSource messageSource) {this.messageSource messageSource;}public void doSomething(Locale locale) {String message messageSource.getMessage(example.message, null, locale);System.out.println(message);}}在这个示例中ExampleService类有一个doSomething方法该方法会解析example.message消息。 2. 使用MessageSource: MessageSource可以在服务层或控制层使用。 示例 RestController public class ExampleController {private final ExampleService exampleService;Autowiredpublic ExampleController(ExampleService exampleService) {this.exampleService exampleService;}GetMapping(/example)public ResponseEntity? getExample(Locale locale) {exampleService.doSomething(locale);return ResponseEntity.ok().build();}}在这个示例中ExampleController类有一个getExample方法该方法会调用ExampleService的doSomething方法。 这是一个简单的Spring Boot应用程序示例它包括国际化的创建和使用。 注意Spring Boot自动配置了一个MessageSource它是ResourceBundleMessageSource的一个实例。因此我们可以直接注入MessageSource而不需要自定义一个MessageSource。 另外我们可以在application.properties或application.yml文件中配置MessageSource的属性。 示例 spring.messages.basenamemessages spring.messages.cache-duration3600在这个示例中我们配置了MessageSource的basename属性和cacheDuration属性。 basename属性是一个逗号分隔的列表它包含了消息资源的基本名称。cacheDuration属性是消息资源的缓存持续时间单位是秒。 另外我们也可以在Value注解中使用MessageSource。 示例 Value(#{messageSource.getMessage(example.message, null, locale)}) private String message;在这个示例中我们使用Value注解和MessageSource注入example.message消息的值。 3. 为什么要这样做 国际化是软件开发的一个重要组成部分它使得软件可以根据不同的地区显示不同的消息。这对于开发全球化的应用程序是非常重要的。 Spring框架通过MessageSource接口支持国际化这样我们可以在应用程序中使用不同的消息资源而不需要修改代码。 MessageSource接口提供了一个灵活的方式来解析消息。我们可以根据消息的键、参数和地区解析消息。这样我们可以在一个地方管理所有的消息而不需要在代码中硬编码消息。 此外MessageSource接口也支持消息的参数化这样我们可以在消息中插入动态的值。 总的来说MessageSource接口提供了一个强大的方式来支持应用程序的国际化。 ✌2. spring-beans Spring Beans 是 Spring 框架的核心组件。Beans 是 Spring IoC 容器中的对象。IoCInversion of Control是一种编程思想它将对象的创建、配置和管理交给了 IoC 容器而不是传统的在代码中直接 new 一个对象。 1. Bean 的定义 在 Spring 中Bean 是由 Spring IoC 容器实例化、组装和管理的对象。除了应用程序对象之外IoC 容器本身也是一个 Bean。Bean 是 Spring 的一个重要组成部分它是 Spring 框架的基础。 2. Bean 的配置 Bean 可以通过 XML 文件、注解、Java 配置类等方式进行配置。在 XML 文件中可以使用 bean 标签来定义一个 Bean。例如 xml bean idexampleBean classcom.example.ExampleBean/ 在这个例子中id 是 Bean 的唯一标识符class 是 Bean 的全限定类名。 3. Bean 的作用域Spring 支持以下五种作用域 - Singleton默认作用域每个 Spring IoC 容器中只有一个 Bean 的实例。 - Prototype每次请求都会创建一个新的 Bean 实例。 - Request每次 HTTP 请求都会产生一个新的 Bean该 Bean 仅在当前 HTTP request 内有效。 - Session同一个 HTTP session 共享一个 Bean。 - Global session全局 session 作用域一般用于 portlet context。4. Bean 的生命周期Bean 的生命周期包括以下几个步骤 - 实例化Spring IoC 容器创建 Bean 的实例。 - 填充属性Spring IoC 容器通过依赖注入填充 Bean 的属性。 - 调用初始化方法Spring IoC 容器调用 Bean 的初始化方法。 - Bean 可用此时 Bean 已经准备好可以被应用程序使用了。 - 调用销毁方法当容器关闭时Spring IoC 容器会调用 Bean 的销毁方法。5. 依赖注入 依赖注入是 Spring 框架的核心功能之一。它是一种设计模式目的是减少代码之间的耦合。Spring IoC 容器负责将依赖关系注入到 Bean 中。 6. Bean 的自动装配 Spring 可以自动装配 Bean 的属性。这意味着 Spring IoC 容器可以自动为 Bean 的属性、setter 方法、构造函数等注入依赖关系。 7. 注解 Spring 提供了一系列的注解例如 Autowired, Component, Service, Repository, Controller 等这些注解可以用来自动装配 Bean、定义 Bean 的作用域、生命周期等。 8. Java 配置 除了 XML 配置外Spring 还支持 Java 配置。可以使用 Configuration 和 Bean 注解来配置 Bean。 ✌3. spring-context spring-context模块扩展了spring-core和spring-beans提供了应用程序上下文和IOC容器。它支持国际化、资源加载、事件驱动编程、应用程序配置等功能。该模块也包含了ApplicationContext接口用于管理和装配应用程序中的各种Bean。 当然这里是每个点的作用和使用场景 1. ApplicationContext: 作用ApplicationContext 是 Spring 的 IoC 容器它负责管理 Spring 应用中的 beans。使用场景当你需要访问应用上下文例如获取 beans、发布事件、访问资源等。示例import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component;Component public class MyComponent implements ApplicationContextAware {private ApplicationContext applicationContext;Overridepublic void setApplicationContext(ApplicationContext applicationContext) {this.applicationContext applicationContext;} }2. 事件传播: 作用Spring 提供了一个强大的事件传播机制你可以定义自己的事件、监听器和发布器。使用场景当你需要在应用中传播事件例如用户注册事件、订单支付事件等。示例import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component;public class MyEvent extends ApplicationEvent {public MyEvent(Object source) {super(source);} }Component public class MyEventListener implements ApplicationListenerMyEvent {Overridepublic void onApplicationEvent(MyEvent event) {System.out.println(Received: event);} }Component public class MyEventPublisher {private final ApplicationContext applicationContext;public MyEventPublisher(ApplicationContext applicationContext) {this.applicationContext applicationContext;}public void publishEvent() {MyEvent event new MyEvent(this);applicationContext.publishEvent(event);} }3. 资源加载: 作用Spring 提供了一个统一的资源加载抽象你可以使用它来加载文件、类路径资源、URL 资源等。使用场景当你需要加载资源例如配置文件、图片、音频、视频等。示例import org.springframework.context.ApplicationContext; import org.springframework.core.io.Resource; import org.springframework.stereotype.Component;import java.io.IOException; import java.io.InputStream;Component public class MyResourceLoader {private final ApplicationContext applicationContext;public MyResourceLoader(ApplicationContext applicationContext) {this.applicationContext applicationContext;}public void loadResource(String location) throws IOException {Resource resource applicationContext.getResource(location);try (InputStream inputStream resource.getInputStream()) {// ...}} }4. 国际化: 作用Spring 提供了一个消息解析的抽象你可以使用它来实现应用的国际化。使用场景当你需要实现应用的国际化例如显示不同语言的用户界面、发送不同语言的邮件等。示例import org.springframework.context.MessageSource; import org.springframework.stereotype.Component;import java.util.Locale;Component public class MyMessageSource {private final MessageSource messageSource;public MyMessageSource(MessageSource messageSource) {this.messageSource messageSource;}public String getMessage(String code, Object[] args, Locale locale) {return messageSource.getMessage(code, args, locale);} }5. 任务调度: 作用Spring 提供了一个任务调度的抽象你可以使用它来执行定时任务。使用场景当你需要执行定时任务例如每天凌晨备份数据库、每小时检查服务器的健康状态等。示例import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;Component public class MyScheduledTask {Scheduled(fixedRate 1000)public void run() {System.out.println(Running...);} }6. 访问 JNDI: 作用Spring 提供了一个 JNDI 访问的抽象你可以使用它来访问 JNDI 资源。使用场景当你需要访问 JNDI 资源例如数据源、EJB、JMS 等。示例import org.springframework.jndi.JndiTemplate; import org.springframework.stereotype.Component;import javax.naming.NamingException;Component public class MyJndiAccessor {private final JndiTemplate jndiTemplate;public MyJndiAccessor(JndiTemplate jndiTemplate) {this.jndiTemplate jndiTemplate;}public Object lookup(String name) throws NamingException {return jndiTemplate.lookup(name);} }7. 访问 EJB: 作用Spring 提供了一个 EJB 访问的抽象你可以使用它来访问远程的 EJB。使用场景当你需要访问远程的 EJB例如调用远程的业务逻辑、获取远程的数据等。示例import org.springframework.remoting.ejb.SimpleRemoteStatelessSessionProxyFactoryBean; import org.springframework.stereotype.Component;import javax.ejb.EJBHome; import javax.ejb.EJBObject; import java.rmi.RemoteException;Component public class MyEjbAccessor {private final SimpleRemoteStatelessSessionProxyFactoryBean ejbProxyFactory;public MyEjbAccessor(SimpleRemoteStatelessSessionProxyFactoryBean ejbProxyFactory) {this.ejbProxyFactory ejbProxyFactory;}public EJBObject create() throws RemoteException {EJBHome ejbHome (EJBHome) ejbProxyFactory.getObject();return ejbHome.create();} }8. 远程访问: 作用Spring 提供了一个远程访问的抽象你可以使用它来访问远程的服务。 使用场景当你需要访问远程的服务例如调用远程的 REST API、访问远程的 RMI 服务等。 示例 import org.springframework.remoting.rmi.RmiProxyFactoryBean; import org.springframework.stereotype.Component;Component public class MyRmiClient {private final RmiProxyFactoryBean rmiProxyFactory;public MyRmiClient(RmiProxyFactoryBean rmiProxyFactory) {this.rmiProxyFactory rmiProxyFactory;}public MyService getMyService() {return (MyService) rmiProxyFactory.getObject();} }public interface MyService {void doSomething(); }9. 缓存抽象: 作用Spring 提供了一个缓存抽象你可以使用它来缓存方法的结果。使用场景当你需要缓存方法的结果例如缓存数据库查询的结果、缓存远程服务的结果等。示例import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component;Component public class MyService {Cacheable(myCache)public String doSomething(String input) {return Hello, input;} }这些示例展示了如何在 Spring Boot 中使用 spring-context 的一些关键功能。这些示例可以作为你在 Spring Boot 中使用 spring-context 的起点。 ✌4. spring-context-support spring-context-support模块提供了对特定环境如Java EE的支持包括对JNDI查找、EJB访问、邮件发送等功能的集成。 spring-context-support 是 Spring 框架的一个模块它提供了对第三方库的集成支持例如缓存、邮件、调度、模板引擎等。 在 Spring Boot 中使用 spring-context-support 的一些关键点如下 1. 缓存支持: 作用Spring 提供了一个缓存抽象它支持多种缓存库例如 EhCache、Guava、JCache 等。使用场景当你需要缓存方法的结果例如缓存数据库查询的结果、缓存远程服务的结果等。示例import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component;Component public class MyService {Cacheable(myCache)public String doSomething(String input) {return Hello, input;} }在这个示例中MyService 类使用 Cacheable 注解来缓存 doSomething 方法的结果。 2. 邮件发送: 作用Spring 提供了一个邮件发送的抽象你可以使用它来发送简单的邮件、附件邮件、富文本邮件等。使用场景当你需要发送邮件例如发送注册确认邮件、发送密码重置邮件、发送订单确认邮件等。示例import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; import org.springframework.stereotype.Component;Component public class MyMailSender {private final MailSender mailSender;public MyMailSender(MailSender mailSender) {this.mailSender mailSender;}public void sendMail(String to, String subject, String text) {SimpleMailMessage message new SimpleMailMessage();message.setTo(to);message.setSubject(subject);message.setText(text);mailSender.send(message);} }在这个示例中MyMailSender 类使用 MailSender 来发送简单的邮件。 3. 任务调度: 作用Spring 提供了一个任务调度的抽象你可以使用它来执行定时任务。使用场景当你需要执行定时任务例如每天凌晨备份数据库、每小时检查服务器的健康状态等。示例import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;Component public class MyScheduledTask {Scheduled(fixedRate 1000)public void run() {System.out.println(Running...);} }在这个示例中MyScheduledTask 类使用 Scheduled 注解来定义一个定时任务。 4. 模板引擎集成: 作用Spring 提供了对多种模板引擎的集成支持例如 FreeMarker、Velocity、Thymeleaf 等。使用场景当你需要渲染模板例如渲染网页、渲染邮件正文、渲染报表等。示例import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping;Controller public class MyController {GetMapping(/hello)public String hello(Model model) {model.addAttribute(name, World);return hello;} }在这个示例中MyController 类使用 Controller 注解来定义一个控制器hello 方法返回一个视图名Spring 会使用配置的模板引擎来渲染这个视图。 这些示例展示了如何在 Spring Boot 中使用 spring-context-support 的一些关键功能。这些示例可以作为你在 Spring Boot 中使用 spring-context-support 的起点。 ✌5. spring-expression spring-expression模块提供了Spring表达式语言SpEL用于在运行时进行属性的动态计算和值的查询。它支持在XML配置文件和注解中使用表达式。 spring-expression 模块也称为 SpELSpring Expression Language是 Spring 框架的一部分它提供了一个强大的表达式语言用于在运行时查询和操作对象图。 在 Spring Boot 中使用 spring-expression 的一些关键点如下 1. 基本使用: 作用SpEL 可以用于查询对象、调用方法、计算值等。使用场景当你需要在运行时动态计算值、查询对象、调用方法等。示例import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.stereotype.Component;Component public class MySpelEvaluator {private final ExpressionParser parser new SpelExpressionParser();public Object evaluate(String expression) {return parser.parseExpression(expression).getValue();} }在这个示例中MySpelEvaluator 类使用 SpelExpressionParser 来解析和计算 SpEL 表达式。 2. 集合选择: 作用SpEL 提供了一种简洁的语法来从集合中选择元素。使用场景当你需要从集合中选择元素例如从列表中选择满足条件的元素、从映射中选择满足条件的键值对等。示例import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.stereotype.Component;import java.util.List; import java.util.Map;Component public class MySpelEvaluator {private final ExpressionParser parser new SpelExpressionParser();public List? selectFromList(List? list, String expression) {return (List?) parser.parseExpression(expression).getValue(list);}public Map?, ? selectFromMap(Map?, ? map, String expression) {return (Map?, ?) parser.parseExpression(expression).getValue(map);} }在这个示例中MySpelEvaluator 类使用 SpelExpressionParser 来从列表和映射中选择元素。 3. 方法调用: 作用SpEL 提供了一种简洁的语法来调用方法。使用场景当你需要在运行时动态调用方法例如调用对象的方法、调用静态方法等。示例import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.stereotype.Component;Component public class MySpelEvaluator {private final ExpressionParser parser new SpelExpressionParser();public Object callMethod(Object root, String expression) {return parser.parseExpression(expression).getValue(root);} }在这个示例中MySpelEvaluator 类使用 SpelExpressionParser 来调用方法。 属性访问: 作用SpEL 提供了一种简洁的语法来访问属性。使用场景当你需要在运行时动态访问属性例如访问对象的属性、访问映射的键值对等。示例import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.stereotype.Component;Component public class MySpelEvaluator {private final ExpressionParser parser new SpelExpressionParser();public Object accessProperty(Object root, String expression) {return parser.parseExpression(expression).getValue(root);} }在这个示例中MySpelEvaluator 类使用 SpelExpressionParser 来访问属性。 这些示例展示了如何在 Spring Boot 中使用 spring-expression 的一些关键功能。这些示例可以作为你在 Spring Boot 中使用 spring-expression 的起点。 ✌6. spring-aop spring-aop 模块是 Spring 框架的一部分它提供了面向切面编程AOP的功能。AOP 是一种编程范式它允许你定义“切面”这些切面可以插入到对象的方法调用中。 在 Spring Boot 中使用 spring-aop 的一些关键点如下 1. 定义切面: 作用你可以定义一个切面这个切面包含了一组通知和一个切点。使用场景当你需要在方法调用的前后插入代码例如记录日志、事务管理、权限检查等。示例import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component;Aspect Component public class MyAspect {Before(execution(* com.example.MyService.*(..)))public void before() {System.out.println(Before method execution);} }在这个示例中MyAspect 类使用 Aspect 注解来定义一个切面before 方法使用 Before 注解来定义一个前置通知。 2. 定义通知: 作用你可以定义不同类型的通知例如前置通知、后置通知、返回通知、异常通知、环绕通知等。使用场景当你需要在方法调用的不同阶段插入代码例如在方法调用前、方法调用后、方法返回、方法抛出异常等。示例import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.springframework.stereotype.Component;Aspect Component public class MyAspect {After(execution(* com.example.MyService.*(..)))public void after() {System.out.println(After method execution);}AfterReturning(execution(* com.example.MyService.*(..)))public void afterReturning() {System.out.println(After method returning);}AfterThrowing(execution(* com.example.MyService.*(..)))public void afterThrowing() {System.out.println(After method throwing);}Around(execution(* com.example.MyService.*(..)))public Object around(ProceedingJoinPoint pjp) throws Throwable {System.out.println(Before method execution);Object result pjp.proceed();System.out.println(After method execution);return result;} }在这个示例中MyAspect 类定义了四个通知after 方法是一个后置通知afterReturning 方法是一个返回通知afterThrowing 方法是一个异常通知around 方法是一个环绕通知。 3. 定义切点: 作用你可以定义一个切点这个切点确定了通知应该应用到哪些方法上。使用场景当你需要精确控制通知应该应用到哪些方法上例如应用到所有的服务方法上、应用到所有的存储库方法上等。示例import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component;Aspect Component public class MyAspect {Pointcut(execution(* com.example.MyService.*(..)))public void myPointcut() {}Before(myPointcut())public void before() {System.out.println(Before method execution);} }在这个示例中MyAspect 类使用 Pointcut 注解来定义一个切点before 方法使用 Before 注解来定义一个前置通知。 这些示例展示了如何在 Spring Boot 中使用 spring-aop 的一些关键功能。这些示例可以作为你在 Spring Boot 中使用 spring-aop 的起点。 ✌7. spring-aspects spring-aspects模块提供了使用AspectJ实现的Spring方面库用于更强大和灵活的AOP功能。 在 Spring Boot 中使用 spring-aspects 主要是为了集成 Spring 的面向切面编程AOP功能。AOP 是一种编程范式允许你在不改变业务逻辑的情况下通过将横切关注点如日志记录、安全性、事务管理等从主要业务逻辑中分离出来从而提高代码的模块性和可维护性。spring-aspects 模块是 Spring Framework 提供的一个模块它帮助你将 AOP 功能集成到 Spring 应用中。 以下是在 Spring Boot 中使用 spring-aspects 的步骤 1. 添加依赖在项目的 Maven 或 Gradle 构建文件中添加对 spring-aspects 模块的依赖。 Maven 依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId /dependencyGradle 依赖 implementation org.springframework.boot:spring-boot-starter-aop2. 创建切面类 定义一个类作为切面其中包含定义切入点和通知Advice的代码。通知是在切入点处执行的代码比如 Before、After、Around 等。 import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component;Aspect Component public class MyAspect {Before(execution(* com.example.demo.service.*.*(..)))public void beforeAdvice() {System.out.println(Before method execution);} }3. 启用切面 在 Spring Boot 应用的主类上添加 EnableAspectJAutoProxy 注解启用自动代理功能使切面生效。 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.EnableAspectJAutoProxy;SpringBootApplication EnableAspectJAutoProxy public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);} }现在MyAspect 中定义的切面将会在 com.example.demo.service 包下的方法执行之前执行。 区别 between spring-aspects 和 AOP spring-aspects 是 Spring 框架提供的模块它是 Spring 框架中用于集成 AOP 功能的一部分。它使用 AspectJ 来实现 AOP 的各种功能。 AOP 是编程范式AOP 是一种编程范式可以用于任何支持该范式的编程语言和框架。spring-aspects 是 Spring 框架特定的实现用于将 AOP 集成到 Spring 应用中。 AspectJ 支持更强大的切面功能AspectJ 是一个独立的 AOP 框架它比 Spring AOP 提供了更多的切面功能。如果你需要更高级的 AOP 功能可以直接使用 AspectJ。 Spring AOP 是运行时代理Spring AOP 通过运行时代理实现切面。它主要使用 JDK 动态代理和 CGLIB 来创建代理对象。AspectJ 则可以进行更细粒度的静态织入但需要在编译时进行额外的处理。 综上所述spring-aspects 是 Spring 框架中用于集成 AOP 功能的模块它使用 AspectJ 实现 AOP。AOP 是一种编程范式可以用于任何支持该范式的环境。AspectJ 提供更多功能而 Spring AOP 则更简单并且与 Spring 框架集成得更紧密。 ✌8. spring-jdbc spring-jdbc模块提供了JDBC访问和错误处理的支持。它简化了JDBC的使用提供了异常处理、事务管理和连接池等功能。 在 Spring Boot 中使用 spring-jdbc 模块你可以轻松地进行数据库操作包括连接管理、SQL 查询和更新等。spring-jdbc 提供了一种简化和抽象的方式来访问关系型数据库以及处理与数据库相关的操作。以下是在 Spring Boot 中使用 spring-jdbc 的详细步骤 1. 添加依赖 在项目的 Maven 或 Gradle 构建文件中添加对 spring-boot-starter-jdbc 模块的依赖。 Maven 依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-jdbc/artifactId /dependencyGradle 依赖 implementation org.springframework.boot:spring-boot-starter-jdbc2. 配置数据源 在 application.properties 或 application.yml 文件中配置数据库连接信息如数据库 URL、用户名、密码等。 spring.datasource.urljdbc:mysql://localhost:3306/mydb spring.datasource.usernameroot spring.datasource.passwordyourpassword spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver3. 创建 DAO数据访问对象 编写数据库访问相关的代码。可以使用 Spring 的 JdbcTemplate 类来执行 SQL 查询和更新。 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository;Repository public class UserDao {private final JdbcTemplate jdbcTemplate;Autowiredpublic UserDao(JdbcTemplate jdbcTemplate) {this.jdbcTemplate jdbcTemplate;}public String findUserNameById(int userId) {String sql SELECT username FROM users WHERE id ?;return jdbcTemplate.queryForObject(sql, String.class, userId);}public void insertUser(String username, String email) {String sql INSERT INTO users (username, email) VALUES (?, ?);jdbcTemplate.update(sql, username, email);} }4. 使用 DAO在服务类或控制器中使用 DAO 类进行数据库操作。 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;Service public class UserService {private final UserDao userDao;Autowiredpublic UserService(UserDao userDao) {this.userDao userDao;}public String getUsernameById(int userId) {return userDao.findUserNameById(userId);}public void createUser(String username, String email) {userDao.insertUser(username, email);} }5. 在主类中启动应用 在 Spring Boot 的主类中添加 SpringBootApplication 注解启动应用。 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);} }通过以上步骤你就可以在 Spring Boot 中使用 spring-jdbc 模块来进行数据库操作了。JdbcTemplate 提供了许多便利的方法来执行 SQL 查询和更新同时还支持参数绑定、结果映射等功能从而简化了数据库操作的流程。 ✌9. spring-tx 当使用 spring-tx 模块进行事务管理时有几个核心的功能和用法以及相应的使用场景。以下是不同的事务管理功能、其作用、使用场景以及可能出现问题的情况 1. 声明式事务管理Transactional 注解 作用允许你通过注解声明事务的范围从而自动管理事务的开始、提交、回滚等操作将事务和业务逻辑解耦。 使用场景适用于简单的事务场景例如在服务层的方法上添加 Transactional 注解来实现事务管理。 示例 import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;Service public class UserService {Transactionalpublic void transferFunds(int fromAccountId, int toAccountId, double amount) {// Perform fund transfer} }问题和注意事项在一个方法内部多次调用被 Transactional 注解标记的其他方法时事务可能无法正常工作。这是因为 Spring 默认会使用代理来实现事务管理。在这种情况下要么将被调用的方法提取到一个独立的类中要么使用 self-invocation 属性解决问题。 2. 编程式事务管理PlatformTransactionManager 接口 作用允许你在代码中手动管理事务提供更细粒度的控制。 使用场景适用于需要更多控制的复杂事务场景例如多个方法需要在同一个事务内执行。 示例 import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.DefaultTransactionDefinition; import org.springframework.stereotype.Service;Service public class UserService {private final PlatformTransactionManager transactionManager;public UserService(PlatformTransactionManager transactionManager) {this.transactionManager transactionManager;}public void transferFunds(int fromAccountId, int toAccountId, double amount) {TransactionStatus status transactionManager.getTransaction(new DefaultTransactionDefinition());try {// Perform fund transfertransactionManager.commit(status);} catch (Exception e) {transactionManager.rollback(status);}} }问题和注意事项编程式事务需要手动处理事务的开始、提交、回滚等操作可能导致遗漏或错误。此外如果在事务内发生了未捕获的异常事务可能不会回滚。 3. 事务传播行为Propagation 作用定义了事务方法在被其他事务方法调用时如何处理事务。 使用场景适用于有多个方法相互调用需要管理不同事务传播行为的场景。 示例 import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.stereotype.Service;Service public class UserService {Transactional(propagation Propagation.REQUIRED)public void transferFunds(int fromAccountId, int toAccountId, double amount) {// Perform fund transfer} }问题和注意事项不同的传播行为可能会导致事务的不同状态例如 REQUIRES_NEW 可能会导致新事务的创建而 REQUIRED 则会加入当前事务。 4. 事务隔离级别Isolation 作用定义了事务之间的隔离程度以避免并发操作引起的数据问题。 使用场景在多线程或并发访问的环境下需要控制事务隔离级别的场景。 示例 import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Transactional; import org.springframework.stereotype.Service;Service public class UserService {Transactional(isolation Isolation.READ_COMMITTED)public void updateAccountBalance(int accountId, double newBalance) {// Update account balance} }问题和注意事项较高的隔离级别可能会导致性能降低应根据实际需要选择合适的隔离级别。 5. 事务超时Timeout 作用定义事务在超过指定时间后自动回滚以防止事务长时间占用资源。 使用场景适用于需要限制事务执行时间的场景避免资源占用。 示例 import org.springframework.transaction.annotation.Transactional; import org.springframework.stereotype.Service;Service public class UserService {Transactional(timeout 5) // Timeout in secondspublic void performCriticalOperation() {// Perform critical operation}}问题和注意事项设置过小的超时时间可能导致事务无法完成而设置过大的超时时间可能导致资源浪费。 在使用 spring-tx 进行事务管理时可能会出现以下问题 脏读Dirty Read一个事务读取了另一个事务尚未提交的数据如果另一个事务回滚读取的数据就是无效的。 不可重复读Non-repeatable Read一个事务在多次读取同一数据时由于其他事务修改了该数据读取结果不一致。 幻读Phantom Read一个事务在读取一系列数据时其他事务插入了新的数据导致读取结果不一致。 死锁Deadlock多个事务相互等待对方释放资源造成无法继续进行的情况。 ✌10. spring-orm spring-orm模块提供了对对象关系映射ORM框架的集成支持如Hibernate、JPA、JDO等。 在 Spring Boot 中使用 spring-orm 模块你可以集成和使用不同的持久化技术主要包括 Hibernate 和 JPAJava Persistence API。这个模块使得在 Spring Boot 应用中管理持久化数据变得更加简便。下面将详细解释如何在 Spring Boot 中使用 spring-orm 模块以及其中涉及的核心功能和用法。 1. 配置数据源 在 application.properties 或 application.yml 配置文件中设置数据库连接信息如数据库URL、用户名、密码等。 spring.datasource.urljdbc:mysql://localhost:3306/mydb spring.datasource.usernameroot spring.datasource.passwordyour_password spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver2. 引入依赖 在 Maven 或 Gradle 构建配置中引入 spring-boot-starter-data-jpa 或 spring-boot-starter-data-jpa 依赖它们包含了 spring-orm 模块。 Maven dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-jpa/artifactId /dependencyGradle implementation org.springframework.boot:spring-boot-starter-data-jpa3. 创建实体类 创建与数据库表对应的实体类使用 JPA 注解来映射实体属性和数据库表结构。 import javax.persistence.*;Entity Table(name users) public class User {IdGeneratedValue(strategy GenerationType.IDENTITY)private Long id;Column(name username)private String username;Column(name email)private String email;// Getter and setter methods }4. 创建 Repository 接口 创建一个继承自 JpaRepository 的接口该接口提供了一组内置的 CRUD 操作方法。 import org.springframework.data.jpa.repository.JpaRepository;public interface UserRepository extends JpaRepositoryUser, Long {// Custom query methods, if needed }5. 使用 Repository 在服务层或控制层中使用自动生成的 Repository 接口执行数据持久化操作。 import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;Service public class UserService {private final UserRepository userRepository;public UserService(UserRepository userRepository) {this.userRepository userRepository;}Transactionalpublic User createUser(User user) {return userRepository.save(user);}Transactionalpublic User updateUser(User user) {return userRepository.save(user);}Transactional(readOnly true)public User getUserById(Long userId) {return userRepository.findById(userId).orElse(null);}Transactionalpublic void deleteUser(Long userId) {userRepository.deleteById(userId);} }6. 配置 JPA 属性 在 application.properties 或 application.yml 中可以配置 JPA 相关属性如显示 SQL、DDL 自动创建等。 spring.jpa.show-sqltrue spring.jpa.hibernate.ddl-autoupdate以上是在 Spring Boot 中使用 spring-orm 模块的基本步骤。它将帮助你更轻松地集成和操作数据库同时利用 Spring Boot 的自动配置和简化开发流程的特性。 注意在实际应用中你可以根据需要选择 Hibernate、JPA 或其他持久化技术然后进行相应的配置和代码编写。 ✌11. spring-web spring-web模块提供了基本的Web支持包括上传文件、MVCModel-View-Controller架构、HTTP请求和响应等功能。 在 Spring Boot 中使用 spring-web 模块可以构建和开发 Web 应用程序实现控制器、处理请求、视图解析等功能。spring-web 模块为开发 Web 应用提供了基础架构包括处理 HTTP 请求、响应、路由、视图渲染等功能。以下是在 Spring Boot 中使用 spring-web 模块的详细步骤、作用和使用场景。 1. 引入依赖 在 Maven 或 Gradle 构建配置中引入 spring-boot-starter-web 依赖它包含了 spring-web 相关的组件和配置。 Maven dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId /dependencyGradle implementation org.springframework.boot:spring-boot-starter-web2. 创建控制器 创建控制器类处理来自客户端的 HTTP 请求并返回相应的数据或视图。 import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;RestController RequestMapping(/api) public class HelloWorldController {GetMapping(/hello)public String sayHello() {return Hello, Spring Boot!;} }3. 运行应用 使用 Spring Boot 的自动配置和嵌入式 Web 服务器可以直接运行应用程序。启动应用后控制器的映射将会生效。 4. 处理请求 当客户端发送 HTTP 请求到指定的 URL 时控制器将根据映射的方法处理请求并返回响应数据。 5. 视图解析 使用 Thymeleaf、JSP 等模板引擎可以将控制器处理的数据渲染成 HTML 视图。 6. 处理静态资源 spring-web 模块还能处理静态资源如 CSS、JavaScript、图片等使这些资源能够被客户端请求到。 7. 作用和使用场景 构建 Web 应用 通过创建控制器和处理请求可以构建功能完整的 Web 应用程序用于展示数据、接受用户输入等。 RESTful API 使用 RestController 和 RequestMapping 注解可以构建 RESTful 风格的 API处理客户端的 API 请求和响应。 视图渲染 使用模板引擎如 Thymeleaf、FreeMarker、JSP 等将控制器处理的数据渲染成 HTML 视图用于前端展示。 静态资源管理 可以通过 spring-web 处理静态资源使得静态文件如 CSS、JavaScript、图片能够被客户端访问。 国际化和本地化 spring-web 提供了国际化和本地化的支持使得 Web 应用可以根据用户的地区或语言显示不同的内容。 异常处理 可以通过异常处理机制来捕获和处理在控制器中发生的异常返回适当的错误信息给客户端。 总之spring-web 模块为开发 Web 应用程序提供了必要的基础设施能够帮助你构建和管理各种类型的 Web 应用从简单的页面到复杂的 RESTful API。在 Spring Boot 中使用该模块可以更加高效地进行 Web 开发。 ✌12. spring-webmvc 在 Spring Boot 中使用 spring-webmvc 模块你可以构建和开发基于 Model-View-Controller (MVC) 设计模式的 Web 应用程序。spring-webmvc 模块为开发 Web 应用提供了丰富的功能包括控制器、视图解析、请求映射、表单处理等。以下是在 Spring Boot 中使用 spring-webmvc 模块的详细步骤、作用和使用场景。 1. 引入依赖 在 Maven 或 Gradle 构建配置中引入 spring-boot-starter-web 依赖它包含了 spring-webmvc 相关的组件和配置。 Maven dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId /dependencyGradle implementation org.springframework.boot:spring-boot-starter-web2. 创建控制器 创建控制器类处理来自客户端的 HTTP 请求并返回相应的数据或视图。 import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam;Controller public class GreetingController {GetMapping(/greeting)public String greeting(RequestParam(name name, required false, defaultValue World) String name, Model model) {model.addAttribute(name, name);return greeting;} }3. 创建视图模板 使用模板引擎如 Thymeleaf、FreeMarker、JSP 等创建视图模板将控制器处理的数据渲染成 HTML 视图。 4. 运行应用 使用 Spring Boot 的自动配置和嵌入式 Web 服务器可以直接运行应用程序。启动应用后控制器的映射将会生效。 5. 处理请求 当客户端发送 HTTP 请求到指定的 URL 时控制器将根据映射的方法处理请求并返回响应数据。 6. 表单处理 使用 RequestParam、ModelAttribute 等注解来处理表单提交数据进行数据绑定和验证。 7. 视图解析 使用模板引擎解析控制器处理的数据将其渲染成 HTML 视图返回给客户端。 8. 静态资源管理 spring-webmvc 模块能够处理静态资源使得静态文件如 CSS、JavaScript、图片能够被客户端访问。 9. 作用和使用场景 MVC 架构 使用 spring-webmvc 模块构建基于 MVC 设计模式的 Web 应用将业务逻辑、数据展示和用户交互分离。 视图渲染 使用模板引擎将控制器处理的数据渲染成 HTML 视图用于前端页面展示。 表单处理 使用注解和工具来处理表单提交的数据进行数据绑定、验证和处理。 RESTful API 使用 RestController 注解和 RequestMapping 注解构建 RESTful 风格的 API处理客户端的 API 请求和响应。 国际化和本地化 spring-webmvc 提供了国际化和本地化的支持使得 Web 应用可以根据用户的地区或语言显示不同的内容。 异常处理 可以通过异常处理机制来捕获和处理在控制器中发生的异常返回适当的错误信息给客户端。 总之spring-webmvc 模块为开发 Web 应用程序提供了丰富的功能和基础设施能够帮助你构建和管理各种类型的 Web 应用从传统的页面应用到 RESTful API。在 Spring Boot 中使用该模块可以更加高效地进行 Web 开发。 ✌13. spring-websocket 当在 Spring Boot 中使用 spring-websocket 模块你可以简单地集成 WebSocket 技术实现双向通信和实时消息传递。WebSocket 是一种在客户端和服务器之间建立持久性连接的通信协议适用于需要实时更新的应用场景如聊天应用、实时通知等。 以下是在 Spring Boot 中使用 spring-websocket 模块的详细步骤包含注释和示例代码 1. 引入依赖 在 Maven 或 Gradle 构建配置中引入 spring-boot-starter-websocket 依赖它包含了 spring-websocket 相关的组件和配置。 Maven dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-websocket/artifactId /dependencyGradle implementation org.springframework.boot:spring-boot-starter-websocket2. 创建 WebSocket 配置类 创建一个配置类继承 WebSocketMessageBrokerConfigurer 接口配置 WebSocket 相关参数和消息处理。 import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;Configuration EnableWebSocketMessageBroker public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {// 配置消息代理指定消息的目的地前缀Overridepublic void configureMessageBroker(MessageBrokerRegistry config) {config.enableSimpleBroker(/topic);config.setApplicationDestinationPrefixes(/app);}// 注册 Stomp 协议的 WebSocket 端点使用 SockJS 支持Overridepublic void registerStompEndpoints(StompEndpointRegistry registry) {registry.addEndpoint(/websocket).withSockJS();} }3. 创建消息处理器 创建消息处理器类处理客户端发送的消息并发送响应消息。 import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.stereotype.Controller;Controller public class WebSocketController {// 监听客户端发送的消息并发送响应消息到特定的目的地MessageMapping(/hello)SendTo(/topic/greetings)public Greeting greeting(HelloMessage message) throws Exception {Thread.sleep(1000); // 模拟延迟return new Greeting(Hello, message.getName() !);} }4. 创建前端页面 创建前端页面使用 JavaScript 或其他适当的库连接到 WebSocket 服务器接收和发送消息。 示例 HTML 文件使用 SockJS 和 STOMP !DOCTYPE html html headtitleWebSocket Example/titlescript src/webjars/sockjs-client/sockjs.min.js/scriptscript src/webjars/stomp-websocket/stomp.min.js/script /head bodyscriptvar stompClient null;function connect() {var socket new SockJS(/websocket);stompClient Stomp.over(socket);stompClient.connect({}, function(frame) {console.log(Connected: frame);stompClient.subscribe(/topic/greetings, function(greeting) {showMessage(JSON.parse(greeting.body).content);});});}function showMessage(message) {var p document.createElement(p);p.appendChild(document.createTextNode(message));document.getElementById(output).appendChild(p);}function sendName() {var name document.getElementById(name).value;stompClient.send(/app/hello, {}, JSON.stringify({ name: name }));}window.onload function() {connect();};/scriptinput typetext idname placeholderYour Namebutton onclicksendName()Send/buttondiv idoutput/div /body /html5. 运行应用 使用 Spring Boot 的自动配置和嵌入式 Web 服务器可以直接运行应用程序。启动应用后WebSocket 配置将会生效。 6. 前端交互 前端页面可以使用 WebSocket 客户端库与服务器建立连接接收和发送消息。 通过 spring-websocket 模块你可以轻松地在 Spring Boot 应用中集成 WebSocket 技术实现双向通信和实时消息传递的功能。这对于构建实时通知、聊天应用、实时数据更新等场景非常有用。 从springboot将Spring的核心模块 当使用Spring Boot时您实际上是在构建基于Spring Framework的应用程序并且Spring Boot在很大程度上简化了配置和部署过程。Spring Boot集成了Spring Framework的核心模块同时还提供了一些自动配置和约定使得开发过程更加快速和便捷。下面是Spring Framework的核心模块如何结合Spring Boot的全面解析 1. spring-core和spring-beans 这两个模块提供了Spring Framework的基础用于依赖注入和控制反转。Spring Boot继承了这两个模块使得您无需手动配置Bean的装配和依赖注入。通过Component、Autowired等注解Spring Boot会自动扫描和装配Bean简化了开发过程。 2. spring-context和spring-context-support 这两个模块提供了应用程序上下文、国际化、资源加载等功能。Spring Boot中的应用程序上下文是内嵌的可以自动扫描和加载应用程序中的Bean。Spring Boot还集成了国际化支持可以根据用户的语言环境加载相应的资源。 3. spring-aop和spring-aspects Spring Boot支持AOP允许您通过切面和通知实现横切关注点的模块化。您可以使用Aspect注解定义切面将通知应用于特定的Bean方法。 4. spring-jdbc、spring-tx和spring-orm 这些模块提供了数据库访问和事务管理的支持。Spring Boot在数据源的配置方面提供了自动化您只需在配置文件中配置数据库相关属性Spring Boot会自动配置数据源和事务管理。 5. spring-web和spring-webmvc Spring Boot支持Web应用程序开发包括基本的Web支持、MVC框架和Web Socket支持。通过Controller、RestController等注解您可以定义Web控制器Spring Boot会自动创建和管理这些控制器。 6. spring-websocket Spring Boot还支持WebSocket协议用于实现实时通信。您可以使用EnableWebSocket注解来启用WebSocket支持。 综上所述Spring Boot在构建应用程序时集成了Spring Framework的核心模块并通过自动配置和约定使得开发过程更加简单。您可以专注于业务逻辑的开发而无需过多关注繁琐的配置细节。Spring Boot的目标是减少样板代码提供快速、灵活和易于维护的开发体验。
文章转载自:
http://www.morning.ztqyj.cn.gov.cn.ztqyj.cn
http://www.morning.lkkkf.cn.gov.cn.lkkkf.cn
http://www.morning.nxbkw.cn.gov.cn.nxbkw.cn
http://www.morning.kjyqr.cn.gov.cn.kjyqr.cn
http://www.morning.nypgb.cn.gov.cn.nypgb.cn
http://www.morning.qnqt.cn.gov.cn.qnqt.cn
http://www.morning.txltb.cn.gov.cn.txltb.cn
http://www.morning.lpmlx.cn.gov.cn.lpmlx.cn
http://www.morning.dgckn.cn.gov.cn.dgckn.cn
http://www.morning.mmsf.cn.gov.cn.mmsf.cn
http://www.morning.pqqhl.cn.gov.cn.pqqhl.cn
http://www.morning.rldph.cn.gov.cn.rldph.cn
http://www.morning.rbsmm.cn.gov.cn.rbsmm.cn
http://www.morning.rlwgn.cn.gov.cn.rlwgn.cn
http://www.morning.fylsz.cn.gov.cn.fylsz.cn
http://www.morning.mgkcz.cn.gov.cn.mgkcz.cn
http://www.morning.npxcc.cn.gov.cn.npxcc.cn
http://www.morning.muniubangcaishui.cn.gov.cn.muniubangcaishui.cn
http://www.morning.hengqilan.cn.gov.cn.hengqilan.cn
http://www.morning.wqbfd.cn.gov.cn.wqbfd.cn
http://www.morning.lkxzb.cn.gov.cn.lkxzb.cn
http://www.morning.ysckr.cn.gov.cn.ysckr.cn
http://www.morning.mrttc.cn.gov.cn.mrttc.cn
http://www.morning.ffbp.cn.gov.cn.ffbp.cn
http://www.morning.ndrzq.cn.gov.cn.ndrzq.cn
http://www.morning.xzjsb.cn.gov.cn.xzjsb.cn
http://www.morning.fdzzh.cn.gov.cn.fdzzh.cn
http://www.morning.tongweishi.cn.gov.cn.tongweishi.cn
http://www.morning.tjqcfw.cn.gov.cn.tjqcfw.cn
http://www.morning.rkwlg.cn.gov.cn.rkwlg.cn
http://www.morning.jsxrm.cn.gov.cn.jsxrm.cn
http://www.morning.junyaod.com.gov.cn.junyaod.com
http://www.morning.lpmdy.cn.gov.cn.lpmdy.cn
http://www.morning.bfkrf.cn.gov.cn.bfkrf.cn
http://www.morning.rtlth.cn.gov.cn.rtlth.cn
http://www.morning.rbgwj.cn.gov.cn.rbgwj.cn
http://www.morning.jqrhz.cn.gov.cn.jqrhz.cn
http://www.morning.gwqkk.cn.gov.cn.gwqkk.cn
http://www.morning.brps.cn.gov.cn.brps.cn
http://www.morning.lxjcr.cn.gov.cn.lxjcr.cn
http://www.morning.qsy37.cn.gov.cn.qsy37.cn
http://www.morning.zbgqt.cn.gov.cn.zbgqt.cn
http://www.morning.rrjzp.cn.gov.cn.rrjzp.cn
http://www.morning.rsjng.cn.gov.cn.rsjng.cn
http://www.morning.sfyqs.cn.gov.cn.sfyqs.cn
http://www.morning.nyfyq.cn.gov.cn.nyfyq.cn
http://www.morning.srgyj.cn.gov.cn.srgyj.cn
http://www.morning.zwxfj.cn.gov.cn.zwxfj.cn
http://www.morning.dgckn.cn.gov.cn.dgckn.cn
http://www.morning.ldcsw.cn.gov.cn.ldcsw.cn
http://www.morning.ymqrc.cn.gov.cn.ymqrc.cn
http://www.morning.thrgp.cn.gov.cn.thrgp.cn
http://www.morning.rqqlp.cn.gov.cn.rqqlp.cn
http://www.morning.jzsgn.cn.gov.cn.jzsgn.cn
http://www.morning.nwnbq.cn.gov.cn.nwnbq.cn
http://www.morning.wsgyq.cn.gov.cn.wsgyq.cn
http://www.morning.dkbsq.cn.gov.cn.dkbsq.cn
http://www.morning.dtrcl.cn.gov.cn.dtrcl.cn
http://www.morning.tbcfj.cn.gov.cn.tbcfj.cn
http://www.morning.qmxsx.cn.gov.cn.qmxsx.cn
http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn
http://www.morning.htbsk.cn.gov.cn.htbsk.cn
http://www.morning.mspqw.cn.gov.cn.mspqw.cn
http://www.morning.tdldh.cn.gov.cn.tdldh.cn
http://www.morning.hwlmy.cn.gov.cn.hwlmy.cn
http://www.morning.nmyrg.cn.gov.cn.nmyrg.cn
http://www.morning.xiaobaixinyong.cn.gov.cn.xiaobaixinyong.cn
http://www.morning.fqyqm.cn.gov.cn.fqyqm.cn
http://www.morning.hwprz.cn.gov.cn.hwprz.cn
http://www.morning.bypfj.cn.gov.cn.bypfj.cn
http://www.morning.nywrm.cn.gov.cn.nywrm.cn
http://www.morning.itvsee.com.gov.cn.itvsee.com
http://www.morning.btgxf.cn.gov.cn.btgxf.cn
http://www.morning.myzfz.com.gov.cn.myzfz.com
http://www.morning.nmngg.cn.gov.cn.nmngg.cn
http://www.morning.cwqln.cn.gov.cn.cwqln.cn
http://www.morning.bqpg.cn.gov.cn.bqpg.cn
http://www.morning.swsrb.cn.gov.cn.swsrb.cn
http://www.morning.zlbjx.cn.gov.cn.zlbjx.cn
http://www.morning.rrxgx.cn.gov.cn.rrxgx.cn
http://www.tj-hxxt.cn/news/255604.html

相关文章:

  • 东莞 外贸网站建设免费的活动策划网站
  • 建站导航分销系统一般多少钱
  • 手机上能搭建网站吗宁乡做网站
  • 毛纱厂家东莞网站建设图形网站建设软件
  • 口碑营销的优势什么是seo
  • 上海网站开发建设电话高端网站设计工作室
  • 大连微网站开发济宁网站制作
  • 西安做网站-西安网站建设-西安网站制作-西安网络公司_千秋网络网站建设情况登记表
  • 西安网站建设优化重庆网站设计生产厂家
  • 沈阳正规网站建设哪家便宜网站建设公司.
  • 微网站制作软件wordpress注册完成请检查电子邮件
  • 公司网站建设的需求绑定电影卡的app
  • 西瓜网站建设家教辅导培训网站建设
  • 简述网站的制作流程成都设计院招聘
  • 工会网站建设比较好的工会哪种网站
  • 公司的网站百度官方免费下载安装
  • 有哪些网站做电子元器件比较好如何营销推广
  • 搭建公司内部网站淘宝客网站哪里可以做
  • 网站后台培训百度关键词搜索排名
  • 开奖视频网站开发表白网页制作网站
  • heritrix做网站提供企业网站建设价格
  • wordpress媒体插件设计型网站自带优化
  • 台州黄岩住房和城乡建设网站有没有做定制衣服的网站
  • 企业网站建设 租用服务器宝塔网站搭建教程
  • 北京华诚传媒有限公司官方网站开发工具宏怎么使用
  • wordpress建外贸站工程类招聘网站哪个好
  • 浙江建设工程信息网站宁波seo外包优化公司
  • 邯郸移动网站建设报价哈尔滨app网站开发
  • 广州番禺网站制作公司哪家好惠州市住房和城乡建设局网站
  • 网页设计好的网站单位有公网ip怎么做网站