原文鏈接http://zhhll.icu/2021/02/21/%E6%A1%86%E6%9E%B6/spring/spring%E6%B3%A8%E8%A7%A3%E6%95%B4%E7%90%86/
spring注解整理
@Configuration
使用@Configuration注解來標注的類為配置類,配置類就相當于組態檔,可以在配置類中來配置bean
@Configuration
public class MainConfig {
/**
* bean的型別是回傳型別,bean的id默認是方法名稱
* @return
*/
@Bean
public Person person(){
return new Person("張三",18);
}
}
Bean
使用@Bean來標注方法以此來進行bean的實體化,bean的型別是回傳型別,bean的id默認是方法名稱,可以使用@Bean注解來自定義bean的id以及初始化方法、銷毀方法
public @interface Bean {
@AliasFor("name")
String[] value() default {};
@AliasFor("value")
String[] name() default {};
Autowire autowire() default Autowire.NO;
String initMethod() default "";
String destroyMethod() default "(inferred)";
}
bean的作用域@Scope
可以在生成bean的方法上使用@Scope來指定bean的作用域
- ConfigurableBeanFactory#SCOPE_PROTOTYPE
- ConfigurableBeanFactory#SCOPE_SINGLETON
- org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST
- org.springframework.web.context.WebApplicationContext#SCOPE_SESSION
懶加載@Lazy
對于單例bean默認是在容器啟動的時候加載,可以使用懶加載來使其第一次呼叫時在進行加載
在生成bean的方法上使用@Lazy來使用來加載
bean的條件注冊@Conditional
@Conditional可以標注在類上,也可以標注在方法上,
public @interface Conditional {
//Condition類陣列
Class<? extends Condition>[] value();
}
可以自定義Condition,需要實作Condition介面
@Primary
如果存在多個相同型別的bean,可以使用@Primary注解來標注bean,使得該bean為默認獲取到的bean
工廠bean
可以使用FactoryBean來使用工廠bean來實體化bean,此時使用personFactoryBean來獲取到的是Person的實體
@Bean
public PersonFactoryBean personFactoryBean(){
return new PersonFactoryBean();
}
public class PersonFactoryBean implements FactoryBean<Person> {
@Override
public Person getObject() throws Exception {
return new Person();
}
@Override
public Class<?> getObjectType() {
return Person.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
如果想要獲取到FactoryBean本身的實體,可以使用&personFactoryBean來獲取
@ComponentScan
在配置類上標注組件掃描,相當于<context:component-scan>可以配置掃描的規則,使用basePackages來指定掃描的包,includeFilters和excludeFilters來配置包含或者排除的規則,與組態檔相似
兩個示例
//排除Controller注解標識的bean
@ComponentScan(basePackages = {"com.zhanghe.study.spring4.annotation"},excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class})
})
// 只包含Controller注解標識的bean,不要忘記useDefaultFilters = false,與使用組態檔相似
@ComponentScan(basePackages = {"com.zhanghe.study.spring4.annotation"},
useDefaultFilters = false,
includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class})
})
過濾的型別有以下幾種
- ANNOTATION 按照注解,最常用
- ASSIGNABLE_TYPE 按照型別
- ASPECTJ 使用ASPECTJ運算式
- REGEX 使用正則運算式
- CUSTOM 使用自定義規則,實作TypeFilter介面
@Import
使用Import可以進行組件匯入,對于第三包中的所需要用到的bean,沒有必要每一個都使用@Bean來進行一個個的實體化,可以使用@Import來直接匯入bean組件
-
@Import(要匯入的組件名) bean的id默認為全類名
-
@Import(importSelector類) 實作importSelector介面,重寫selectImports方法,回傳值就是組件全類名的陣列
String[] selectImports(AnnotationMetadata importingClassMetadata); -
@Import(ImportBeanDefinitionRegistrar類) 實作ImportBeanDefinitionRegistrar介面,重寫registerBeanDefinitions方法,自己使用registry進行注冊某些bean
void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry);
@Value
使用@Value可以為屬性進行賦值
基本數值
@Value("張三")
String name;
Spel運算式
{}
環境變數中的值(組態檔中的值)
${}
需要引入組態檔,使用@PropertySource
@PropertySource(value = "https://www.cnblogs.com/life-time/p/classpath:test.properties")
@Configuration
public class MainConfig4 {
@Bean
public TestValue testValue(){
return new TestValue();
}
}
public class TestValue {
// 取出組態檔中的值
@Value("${test.value}")
private int value;
}
@EnableAspectJAutoProxy
在之前為了使@Aspect注解生效需要在組態檔中配置
<aop:aspectj-autoproxy/>
而該注解的作用就是使得@Aspect注解生效,開啟基于注解的AOP模式,與上述配置功能相同
@EnableTransactionManagement
在之前為了使@Transaction注解生效,需要在組態檔中配置
<tx:annotation-driven transaction-manager="transactionManager"/>
而是用該注解的作用就是使得@Transaction注解生效,與上述配置功能相同
由于本身的博客百度沒有收錄,博客地址http://zhhll.icu
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/265022.html
標籤:Java
