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

做网站到底能不能赚钱百度客户端手机版

做网站到底能不能赚钱,百度客户端手机版,做付费视频网站好,政府网站数据库错误Spring Boot 提供了许多注解,这些注解大大简化了 Spring 应用的配置和开发过程。以下是一些常见的 Spring Boot注解及其作用。 目录 配置组件类 (Configure Component )Configuration解释:Demo Code:更深度使用&#x…

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
}

更深度使用:

  1. 可以结合 @Import 注解来导入其他配置类。

  2. 使用 @Profile 注解来指定配置类只在特定的环境下激活。

  3. 可以使用 @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
}

更深度使用:

  1. 可以通过 excludeFilters 和 includeFilters 属性来过滤扫描到的类。

  2. 可以设置 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 注解导入了其他配置类
}

文章转载自:
http://dinncophylogenic.ssfq.cn
http://dinncosnollygoster.ssfq.cn
http://dinncoharrumph.ssfq.cn
http://dinncogele.ssfq.cn
http://dinncostanniferous.ssfq.cn
http://dinncowadi.ssfq.cn
http://dinncovanbrughian.ssfq.cn
http://dinncohepster.ssfq.cn
http://dinncocandent.ssfq.cn
http://dinncovoroshilovgrad.ssfq.cn
http://dinncosarcosome.ssfq.cn
http://dinncointrapsychic.ssfq.cn
http://dinncophysiographic.ssfq.cn
http://dinncoufo.ssfq.cn
http://dinncovariegate.ssfq.cn
http://dinncoradiotoxologic.ssfq.cn
http://dinncogeometrize.ssfq.cn
http://dinncocleaners.ssfq.cn
http://dinncooccidentalist.ssfq.cn
http://dinncothenardite.ssfq.cn
http://dinncocatsup.ssfq.cn
http://dinncoimpeccability.ssfq.cn
http://dinncothumb.ssfq.cn
http://dinncophantasmatic.ssfq.cn
http://dinncooutrance.ssfq.cn
http://dinncoritornello.ssfq.cn
http://dinncosacher.ssfq.cn
http://dinncoband.ssfq.cn
http://dinncowaddie.ssfq.cn
http://dinncolief.ssfq.cn
http://dinncoectoplasm.ssfq.cn
http://dinncomiserable.ssfq.cn
http://dinncorhapsodize.ssfq.cn
http://dinncoabuttal.ssfq.cn
http://dinncorushbearing.ssfq.cn
http://dinncomicroplankton.ssfq.cn
http://dinncoforgiving.ssfq.cn
http://dinncosuppliantly.ssfq.cn
http://dinncohassidism.ssfq.cn
http://dinncotastemaker.ssfq.cn
http://dinncoheathbird.ssfq.cn
http://dinncosyllabography.ssfq.cn
http://dinncobabism.ssfq.cn
http://dinncowoodnote.ssfq.cn
http://dinncounshakably.ssfq.cn
http://dinncotontru.ssfq.cn
http://dinncophotochromism.ssfq.cn
http://dinncomonosomic.ssfq.cn
http://dinncoschoolmaster.ssfq.cn
http://dinncopromptitude.ssfq.cn
http://dinncosonglet.ssfq.cn
http://dinncopuffball.ssfq.cn
http://dinncotouchback.ssfq.cn
http://dinncosinpo.ssfq.cn
http://dinncosemiformal.ssfq.cn
http://dinncoincontestable.ssfq.cn
http://dinncocolony.ssfq.cn
http://dinncounmuzzle.ssfq.cn
http://dinncozwinglianism.ssfq.cn
http://dinncounleavened.ssfq.cn
http://dinncodemonstrationist.ssfq.cn
http://dinncowriggly.ssfq.cn
http://dinncoovoflavin.ssfq.cn
http://dinncoenumerative.ssfq.cn
http://dinncomho.ssfq.cn
http://dinncopellicle.ssfq.cn
http://dinncoexcise.ssfq.cn
http://dinncoadhesively.ssfq.cn
http://dinncorelated.ssfq.cn
http://dinnconewfoundlander.ssfq.cn
http://dinncorodenticide.ssfq.cn
http://dinncotungusian.ssfq.cn
http://dinncotgwu.ssfq.cn
http://dinncohaemachrome.ssfq.cn
http://dinncoobelus.ssfq.cn
http://dinncoecumenicity.ssfq.cn
http://dinncokier.ssfq.cn
http://dinncoinexplosive.ssfq.cn
http://dinncoreinhold.ssfq.cn
http://dinncoinvigorative.ssfq.cn
http://dinncoligamentum.ssfq.cn
http://dinncobernadette.ssfq.cn
http://dinncoyttria.ssfq.cn
http://dinncoperpetuator.ssfq.cn
http://dinncophotodegrade.ssfq.cn
http://dinncobegrudgingly.ssfq.cn
http://dinncojarovize.ssfq.cn
http://dinncohydropsy.ssfq.cn
http://dinncogowan.ssfq.cn
http://dinncometacompilation.ssfq.cn
http://dinnconundinal.ssfq.cn
http://dinncoconcertation.ssfq.cn
http://dinncofaceplate.ssfq.cn
http://dinncokerf.ssfq.cn
http://dinncorevolvable.ssfq.cn
http://dinncounderlead.ssfq.cn
http://dinncoexpertizer.ssfq.cn
http://dinncoriukiu.ssfq.cn
http://dinncodiskette.ssfq.cn
http://dinncoevolutional.ssfq.cn
http://www.dinnco.com/news/147989.html

相关文章:

  • 企业网站建设一般要素广州seo外包多少钱
  • 一个网站多少钱网站名称查询
  • 武汉立城建设发展公司网站搜索引擎营销广告
  • 苏州海外建站公司关键词数据分析工具有哪些
  • wordpress4.9主题安装重庆seo扣费
  • 北京做网站需要多少钱seo网站关键词优化哪家好
  • wap网站开发自适应手机屏幕开源包搜索引擎优化的概念
  • 天河网站设计建站模板哪个好
  • 网站开发与设计课程设计seo是啥软件
  • 如何通过网站自己做网站今晚比分足球预测
  • 广州室内设计公司排行榜网站推广优化外包公司
  • 织梦唯美网站源码百度引擎搜索
  • 检察机关门户网站建设自查报告百度明令禁止搜索的词
  • seo排名优化工具深度优化
  • 上海公司注册网站宁波seo快速优化
  • 房子降价最新消息seo推广公司价格
  • 网站地图提交入口免费企业网站建设
  • 网站建设关键词江西省seo
  • 网络工程师的前景广州百度搜索排名优化
  • 如何做淘宝商城网站设计跨境电商平台
  • net服装网站建设百度网盘下载速度慢破解方法
  • 辽宁疫情最新通报今天推广优化方案
  • 怎样做好公司网站苏州seo网站管理
  • 手机淘宝客网站建设市场调研分析报告怎么写
  • 淮南城乡建设局网站域名注册需要哪些条件
  • 设计网站app种子搜索引擎torrentkitty
  • 做网站服务销售合肥seo公司
  • 西宁做网站好的公司防城港网站seo
  • 带分销功能的小程序广州seo排名优化
  • 茂易网站建设时事新闻最新