做网站到底能不能赚钱怎么优化网站关键词排名
Spring Boot 提供了许多注解,这些注解大大简化了 Spring 应用的配置和开发过程。以下是一些常见的 Spring Boot注解及其作用。
目录
- 配置组件类 (Configure Component )
- @Configuration
- 解释:
- Demo Code:
- 更深度使用:
- @ComponentScan
- 解释:
- Demo Code:
- 更深度使用:
- @Scope
- 解释:
- Demo Code:
- 1. Singleton(单例)
- 2. Prototype(原型)
- 3. Request(请求)
- 4. Session(会话)
- 5. Application(应用)
- 6. WebSocket(WebSocket 会话)
- 引申
- @Lazy
- 解释:
- Demo Code:
- 更深度使用:
- @Conditional
- 解释:
- Demo Code 自定义一个Condition
- @Import
- 详解
- 使用 Demo
配置组件类 (Configure Component )
@Configuration
解释:
@Configuration 注解用于指示一个类声明了一个或多个 @Bean 方法,并且可能被 Spring 容器作为 Bean
定义的源进行处理。它允许你通过 Java 配置的方式来替代 XML 配置文件。被 @Configuration 注解的类内部可以包含多个
@Bean 注解的方法,这些方法将返回对象,这些对象会作为 Spring 应用上下文中的 Bean。
被@Configuration 注解的类通常被称为 配置类。
Demo Code:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class AppConfig {@Beanpublic MyService myService() {return new MyServiceImpl();}
}class MyService {// Service methods
}class MyServiceImpl implements MyService {// Implementation details
}
更深度使用:
-
可以结合 @Import 注解来导入其他配置类。
-
使用 @Profile 注解来指定配置类只在特定的环境下激活。
-
可以使用 @Enable* 注解来启用特定的 Spring 功能(如 @EnableTransactionManagement)。
@ComponentScan
解释:
@ComponentScan 注解用于告诉 Spring 框架在包及其子包中查找被
@Component、@Service、@Repository、@Controller 等注解标记的类,并将这些类注册为 Spring应用上下文中的 Bean。
Demo Code:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;@Configuration
@ComponentScan(basePackages = "com.example.demo")
public class AppConfig {// Configuration details
}// In package com.example.demo
@Service
public class MyService {// Service implementation
}
更深度使用:
-
可以通过 excludeFilters 和 includeFilters 属性来过滤扫描到的类。
-
可以设置 lazyInit 属性来指定是否延迟初始化扫描到的 Bean。
@Scope
解释:
@Scope 注解用于指定 Spring 容器中 Bean 的作用域。Spring
提供了几种作用域,包括单例(Singleton)、原型(Prototype)、会话(Session)、请求(Request)等。
Demo Code:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.WebApplicationContext;@Configuration
public class AppConfig {@Bean@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)public MySessionBean mySessionBean() {return new MySessionBean();}
}class MySessionBean {// Session scoped bean
}
1. Singleton(单例)
解释:在 Spring 容器中,每个 Bean
只有一个实例,且这个实例会被存储在单例缓存中。对于所有的请求和会话,都会返回这个共享的实例。这是默认的作用域。
Demo:由于 Singleton 是默认作用域,所以通常不需要显式指定。但如果要强调其单例性质,可以这样写:
@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
public MySingletonBean mySingletonBean() {return new MySingletonBean();
}
2. Prototype(原型)
解释:每次从 Spring 容器中请求该 Bean 时,都会创建一个新的实例。这意味着每个请求都会得到一个新的 Bean 实例。
Demo:
@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public MyPrototypeBean myPrototypeBean() {return new MyPrototypeBean();
}
3. Request(请求)
解释:每次 HTTP 请求都会创建一个新的 Bean 实例,并且该实例仅在当前 HTTP 请求内有效。这通常用于 Web 应用程序中,与
Servlet 请求的生命周期相关联。
注意:要使用请求作用域,你需要在一个 Web 环境中,并且 Spring 的 DispatcherServlet 正在运行。
Demo(假设在 Web 环境中):
@Bean
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public MyRequestBean myRequestBean() {return new MyRequestBean();
}
4. Session(会话)
解释:每个 HTTP 会话都会创建一个新的 Bean 实例,且该实例仅在当前会话内有效。这也适用于 Web 应用程序。
Demo(假设在 Web 环境中):
@Bean
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public MySessionBean mySessionBean() {return new MySessionBean();
}
5. Application(应用)
解释:在 ServletContext 的生命周期内,每个 Bean 都只有一个实例。这通常用于存储跨多个用户会话的共享数据。但是,请注意,Spring Framework 本身并没有直接提供名为“Application”的内置作用域。这通常是通过在 ServletContext 中直接存储数据或使用 @ServletContextAttribute 注解来实现的。
Demo:由于 Spring 没有直接提供“Application”作用域,这里不展示具体的 Bean 定义,但你可以通过实现自己的 Scope 接口或使用 ServletContextListener 来管理跨会话的数据。
6. WebSocket(WebSocket 会话)
解释:对于使用 WebSocket 的应用程序,Spring 4.2 引入了 WebSocket 会话作用域。这允许你将 Bean 的生命周期与 WebSocket 会话绑定。
Demo:WebSocket 会话作用域的使用依赖于 Spring 的 WebSocket 支持,并且不常见于所有应用程序。因此,这里不提供具体的 Demo,但你可以查看 Spring WebSocket 文档以获取更多信息。
引申
在Spring框架中,当你配置一个Bean的作用域为请求(WebApplicationContext.SCOPE_REQUEST)、会话(WebApplicationContext.SCOPE_SESSION)或其他非单例作用域时,并且你希望将这个Bean注入到一个单例Bean中时,你会遇到一个问题:单例Bean的生命周期与请求/会话Bean的生命周期不一致。为了解决这个问题,Spring引入了作用域代理(Scoped Proxy)的概念。
proxyMode = ScopedProxyMode.TARGET_CLASS 是配置作用域代理的一种方式,它告诉Spring为这个Bean创建一个代理对象,而不是直接创建Bean的实例。这个代理对象将负责在每次需要时获取目标Bean的实例,从而实现了作用域的隔离。
具体来说,ScopedProxyMode.TARGET_CLASS 使用CGLIB库来创建目标类的子类作为代理。这意味着代理对象可以无缝地替代目标类对象,而不需要实现额外的接口(与 ScopedProxyMode.INTERFACES 不同,后者要求目标类实现至少一个接口)。
示例
假设你有一个会话作用域的Bean,你希望将其注入到一个单例Bean中:
@Bean
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public MySessionBean mySessionBean() {return new MySessionBean();
}@Service
public class MySingletonService {@Autowiredprivate MySessionBean mySessionBean; // 这里注入的是代理对象public void doSomething() {// 在这里调用mySessionBean的方法时,实际上是通过代理对象来访问当前会话的MySessionBean实例mySessionBean.doSomethingInSessionScope();}
}
@Lazy
解释:
@Lazy 注解用于指示 Spring 容器在初始化时不立即创建该 Bean 的实例,而是在首次使用时才创建。既延时加载。
Demo Code:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;@Configuration
public class AppConfig {@Bean@Lazypublic MyLazyBean myLazyBean() {return new MyLazyBean();}
}class MyLazyBean {// Lazy initialized bean
}
更深度使用:
@Lazy 可以与 @Autowired 结合使用,但需要注意,当使用 @Autowired 注入 @Lazy 标注的 Bean
时,默认行为可能会因 Spring 版本而异(可能需要额外的设置或注解)。
@Conditional
解释:
@Conditional 注解用于条件化地创建 Bean。它允许基于满足特定条件的情况下来注册 Bean。通常与自定义的 Condition
实现类一起使用。
Demo Code 自定义一个Condition
为了写一个自定义@Conditional的demo,我们需要定义一个实现org.springframework.context.annotation.Condition接口的类,并在一个配置类中使用@Conditional注解来引用这个条件类。下面是一个简单的示例,其中我们将基于一个系统属性来决定是否创建某个Bean。
首先,我们定义一个条件类OnPropertyCondition,它检查系统属性中是否存在某个特定的键:
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;public class OnPropertyCondition implements Condition {private static final String PROPERTY_NAME = "myapp.feature.enabled";@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {Environment env = context.getEnvironment();// 检查系统属性或环境变量中是否存在该属性,并且其值为trueString property = env.getProperty(PROPERTY_NAME);return Boolean.TRUE.toString().equals(property);}
}
然后,我们创建一个配置类,使用@Conditional注解来引用我们的条件类,并根据条件决定是否创建Bean:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Conditional;@Configuration
public class ConditionalConfig {@Bean@Conditional(OnPropertyCondition.class)public FeatureBean featureBean() {return new FeatureBean();}// FeatureBean 的实现public static class FeatureBean {// 实现细节public void doSomething() {System.out.println("Feature is enabled and doing something...");}}
}
@Import
@Import 注解在 Spring
框架中用于导入其他配置类,使得你可以在一个配置类中组合和重用多个配置类。这对于模块化配置特别有用,因为它允许你将配置分散到多个类中,然后在需要的地方通过
@Import 注解将它们组合在一起。
详解
用途:
将其他配置类导入到当前配置类中,以便重用它们的 @Bean 定义、@ComponentScan 扫描路径等。
位置:
通常用于 @Configuration 注解的类上。
参数:
@Import 可以直接指定要导入的配置类(Class 类型)。 也可以指定 ImportSelector 或
ImportBeanDefinitionRegistrar 接口的实现类,以实现更复杂的导入逻辑。 注意:当使用 @Import
导入配置类时,这些配置类中定义的 Bean 也会被 Spring 容器管理,就像它们在当前配置类中定义的一样。
使用 Demo
假设我们有两个配置类,DatabaseConfig 和 SecurityConfig,我们想要在一个主配置类 AppConfig
中将它们都导入。
DatabaseConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class DatabaseConfig {@Beanpublic DataSource dataSource() {// 创建并返回 DataSource 实例return new DataSource(); // 注意:这里应该是实际的 DataSource 实现,这里只是示例}
}
SecurityConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class SecurityConfig {@Beanpublic SecurityManager securityManager() {// 创建并返回 SecurityManager 实例return new SecurityManager(); // 注意:这里应该是实际的安全管理器实现,这里只是示例}
}
AppConfig.java
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;@Configuration
@Import({DatabaseConfig.class, SecurityConfig.class})
public class AppConfig {// 这里不需要定义任何 Bean,因为所有的 Bean 都已经在 DatabaseConfig 和 SecurityConfig 中定义了// AppConfig 仅仅是一个组合配置类,它通过 @Import 注解导入了其他配置类
}