您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Spring中的各項(xiàng)注解的用法”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
xml配置與類配置
1.xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/sp <bean id="person" class="com.binghe.spring.Person"></bean> </beans>
獲取Person實(shí)例如下所示。
public static void main( String[] args ){ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); System.out.println(ctx.getBean("person")); }
2.類配置
@Configuration public class MainConfig { @Bean public Person person(){ return new Person(); } }
這里,有一個(gè)需要注意的地方:通過(guò)@Bean的形式是使用的話, bean的默認(rèn)名稱是方法名,若@Bean(value="bean的名稱")那么bean的名稱是指定的 。
獲取Person實(shí)例如下所示。
public static void main( String[] args ){ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class); System.out.println(ctx.getBean("person")); }
@CompentScan注解
我們可以使用@CompentScan注解來(lái)進(jìn)行包掃描,如下所示。
@Configuration @ComponentScan(basePackages = {"com.binghe.spring"}) public class MainConfig { }
excludeFilters 屬性
當(dāng)我們使用@CompentScan注解進(jìn)行掃描時(shí),可以使用@CompentScan注解的excludeFilters 屬性來(lái)排除某些類,如下所示。
@Configuration @ComponentScan(basePackages = {"com.binghe.spring"},excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION,value = {Controller.class}), @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value = {PersonService.class}) }) public class MainConfig { }
includeFilters屬性
當(dāng)我們使用@CompentScan注解進(jìn)行掃描時(shí),可以使用@CompentScan注解的includeFilters屬性將某些類包含進(jìn)來(lái)。這里需要注意的是:需要把useDefaultFilters屬性設(shè)置為false(true表示掃描全部的)
@Configuration @ComponentScan(basePackages = {"com.binghe.spring"},includeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION,value = {Controller.class, PersonService.class}) },useDefaultFilters = false) public class MainConfig { }
@ComponentScan.Filter type的類型
注解形式的FilterType.ANNOTATION @Controller @Service @Repository @Compent
指定類型的 FilterType.ASSIGNABLE_TYPE @ComponentScan.Filter(type =FilterType.ASSIGNABLE_TYPE,value = {Person.class})
aspectj類型的 FilterType.ASPECTJ(不常用)
正則表達(dá)式的 FilterType.REGEX(不常用)
自定義的 FilterType.CUSTOM
public enum FilterType { //注解形式 比如@Controller @Service @Repository @Compent ANNOTATION, //指定的類型 ASSIGNABLE_TYPE, //aspectJ形式的 ASPECTJ, //正則表達(dá)式的 REGEX, //自定義的 CUSTOM }
FilterType.CUSTOM 自定義類型
public class CustomFilterType implements TypeFilter { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { //獲取當(dāng)前類的注解源信息 AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); //獲取當(dāng)前類的class的源信息 ClassMetadata classMetadata = metadataReader.getClassMetadata(); //獲取當(dāng)前類的資源信息 Resource resource = metadataReader.getResource(); return classMetadata.getClassName().contains("Service"); } @ComponentScan(basePackages = {"com.binghe.spring"},includeFilters = { @ComponentScan.Filter(type = FilterType.CUSTOM,value = CustomFilterType.class) },useDefaultFilters = false) public class MainConfig { }
配置Bean的作用域?qū)ο?/strong>
不指定@Scope
在不指定@Scope的情況下,所有的bean都是單實(shí)例的bean,而且是餓漢加載(容器啟動(dòng)實(shí)例就創(chuàng)建好了)
@Bean public Person person() { return new Person(); }
@Scope為 prototype
指定@Scope為 prototype 表示為多實(shí)例的,而且還是懶漢模式加載(IOC容器啟動(dòng)的時(shí)候,并不會(huì)創(chuàng)建對(duì)象,而是在第一次使用的時(shí)候才會(huì)創(chuàng)建)
@Bean @Scope(value = "prototype") public Person person() { return new Person(); }
@Scope取值
singleton 單實(shí)例的(默認(rèn))
prototype 多實(shí)例的
request 同一次請(qǐng)求
session 同一個(gè)會(huì)話級(jí)別
懶加載
Bean的懶加載@Lazy(主要針對(duì)單實(shí)例的bean 容器啟動(dòng)的時(shí)候,不創(chuàng)建對(duì)象,在第一次使用的時(shí)候才會(huì)創(chuàng)建該對(duì)象)
@Bean @Lazy public Person person() { return new Person(); }
@Conditional條件判斷
場(chǎng)景,有二個(gè)組件CustomAspect 和CustomLog ,我的CustomLog組件是依賴于CustomAspect的組件 應(yīng)用:自己創(chuàng)建一個(gè)CustomCondition的類 實(shí)現(xiàn)Condition接口
public class CustomCondition implements Condition { /**** @param context * @param metadata * @return */ @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { //判斷容器中是否有CustomAspect的組件 return context.getBeanFactory().containsBean("customAspect"); } } public class MainConfig { @Bean public CustomAspect customAspect() { return new CustomAspect(); } @Bean @Conditional(value = CustomCondition.class) public CustomLog customLog() { return new CustomLog(); } }
向IOC 容器添加組件
(1)通過(guò)@CompentScan +@Controller @Service @Respository @compent。適用場(chǎng)景: 針對(duì)我們自己寫的組件可以通過(guò)該方式來(lái)進(jìn)行加載到容器中。
(2)通過(guò)@Bean的方式來(lái)導(dǎo)入組件(適用于導(dǎo)入第三方組件的類)
(3)通過(guò)@Import來(lái)導(dǎo)入組件 (導(dǎo)入組件的id為全類名路徑)
@Configuration @Import(value = {Person.class}) public class MainConfig { }
通過(guò)@Import 的ImportSeletor類實(shí)現(xiàn)組件的導(dǎo)入 (導(dǎo)入組件的id為全類名路徑)
public class CustomImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { return new String[]{"com.binghe.spring"}; } } Configuration @Import(value = {Person.class} public class MainConfig { }
通過(guò)@Import的 ImportBeanDefinitionRegister導(dǎo)入組件 (可以指定bean的名稱)
public class DogBeanDefinitionRegister implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { //創(chuàng)建一個(gè)bean定義對(duì)象 RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Dog.class); //把bean定義對(duì)象導(dǎo)入到容器中 registry.registerBeanDefinition("dog",rootBeanDefinition); } } @Configuration @Import(value = {Person.class, Car.class, CustomImportSelector.class, DogBeanDefinitionRegister.class}) public class MainConfig { }
通過(guò)實(shí)現(xiàn)FacotryBean接口來(lái)實(shí)現(xiàn)注冊(cè) 組件
public class CarFactoryBean implements FactoryBean<Car> { @Override public Car getObject() throws Exception { return new Car(); } @Override public Class<?> getObjectType() { return Car.class; } @Override public boolean isSingleton() { return true; } }
Bean的初始化與銷毀
指定bean的初始化方法和bean的銷毀方法
由容器管理Bean的生命周期,我們可以通過(guò)自己指定bean的初始化方法和bean的銷毀方法
@Configuration public class MainConfig { //指定了bean的生命周期的初始化方法和銷毀方法.@Bean(initMethod = "init",destroyMethod = "destroy") public Car car() { return new Car(); } }
針對(duì)單實(shí)例bean的話,容器啟動(dòng)的時(shí)候,bean的對(duì)象就創(chuàng)建了,而且容器銷毀的時(shí)候,也會(huì)調(diào)用Bean的銷毀方法
針對(duì)多實(shí)例bean的話,容器啟動(dòng)的時(shí)候,bean是不會(huì)被創(chuàng)建的而是在獲取bean的時(shí)候被創(chuàng)建,而且bean的銷毀不受IOC容器的管理
通過(guò) InitializingBean和DisposableBean實(shí)現(xiàn)
通過(guò) InitializingBean和DisposableBean個(gè)接口實(shí)現(xiàn)bean的初始化以及銷毀方法
@Component public class Person implements InitializingBean,DisposableBean { public Person() { System.out.println("Person的構(gòu)造方法"); } @Override public void destroy() throws Exception { System.out.println("DisposableBean的destroy()方法 "); } @Override public void afterPropertiesSet() throws Exception { System.out.println("InitializingBean的 afterPropertiesSet方法"); } }
通過(guò)JSR250規(guī)范
通過(guò)JSR250規(guī)范 提供的注解@PostConstruct 和@ProDestory標(biāo)注的方法
@Component public class Book { public Book() { System.out.println("book 的構(gòu)造方法"); } @PostConstruct public void init() { System.out.println("book 的PostConstruct標(biāo)志的方法"); } @PreDestroy public void destory() { System.out.println("book 的PreDestory標(biāo)注的方法"); } }
通過(guò)BeanPostProcessor實(shí)現(xiàn)
通過(guò)Spring的BeanPostProcessor的 bean的后置處理器會(huì)攔截所有bean創(chuàng)建過(guò)程
postProcessBeforeInitialization 在init方法之前調(diào)用
postProcessAfterInitialization 在init方法之后調(diào)用
@Component public class CustomBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("CustomBeanPostProcessor...postProcessBeforeInitialization:"+beanName); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("CustomBeanPostProcessor...postProcessAfterInitialization:"+beanName); return bean; } }
BeanPostProcessor的執(zhí)行時(shí)機(jī)
populateBean(beanName, mbd, instanceWrapper) initializeBean{ applyBeanPostProcessorsBeforeInitialization() invokeInitMethods{ isInitializingBean.afterPropertiesSet() 自定義的init方法 } applyBeanPostProcessorsAfterInitialization()方法 }
通過(guò)@Value +@PropertySource來(lái)給組件賦值
public class Person { //通過(guò)普通的方式 @Value("獨(dú)孤") private String firstName; //spel方式來(lái)賦值 @Value("#{28-8}") private Integer age; 通過(guò)讀取外部配置文件的值 @Value("${person.lastName}") private String lastName; } @Configuration @PropertySource(value = {"classpath:person.properties"}) //指定外部文件的位置 public class MainConfig { @Bean public Person person() { return new Person(); } }
自動(dòng)裝配
@AutoWired的使用
自動(dòng)注入
@Repository public class CustomDao { } @Service public class CustomService { @Autowired private CustomDao customDao; }
結(jié)論: (1)自動(dòng)裝配首先時(shí)按照類型進(jìn)行裝配,若在IOC容器中發(fā)現(xiàn)了多個(gè)相同類型的組件,那么就按照 屬性名稱來(lái)進(jìn)行裝配
@Autowired private CustomDao customDao;
比如,我容器中有二個(gè)CustomDao類型的組件 一個(gè)叫CustomDao 一個(gè)叫CustomDao2那么我們通過(guò)@AutoWired 來(lái)修飾的屬性名稱時(shí)CustomDao,那么拿就加載容器的CustomDao組件,若屬性名稱為tulignDao2 那么他就加載的時(shí)CustomDao2組件
(2)假設(shè)我們需要指定特定的組件來(lái)進(jìn)行裝配,我們可以通過(guò)使用@Qualifier("CustomDao")來(lái)指定裝配的組件 或者在配置類上的@Bean加上@Primary注解
@Autowired @Qualifier("CustomDao") private CustomDao customDao2
(3)假設(shè)我們?nèi)萜髦屑礇](méi)有CustomDao 和CustomDao2,那么在裝配的時(shí)候就會(huì)拋出異常
No qualifying bean of type 'com.binghhe.spring.dao.CustomDao' available
若我們想不拋異常 ,我們需要指定 required為false的時(shí)候可以了
@Autowired(required = false) @Qualifier("customDao") private CustomDao CustomDao2;
(4)@Resource(JSR250規(guī)范) 功能和@AutoWired的功能差不多一樣,但是不支持@Primary 和@Qualifier的支持
(5)@InJect(JSR330規(guī)范) 需要導(dǎo)入jar包依賴,功能和支持@Primary功能 ,但是沒(méi)有Require=false的功能
<dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency>
(6)使用@Autowired 可以標(biāo)注在方法上
標(biāo)注在set方法上
//@Autowired public void setCustomLog(CustomLog customLog) { this.customLog = customLog; }
標(biāo)注在構(gòu)造方法上
@Autowired public CustomAspect(CustomLog customLog) { this.customLog = customLog; }
標(biāo)注在配置類上的入?yún)⒅?可以不寫)
@Bean public CustomAspect CustomAspect(@Autowired CustomLog customLog) { CustomAspect customAspect = new CustomAspect(customLog); return ustomAspect; }
XXXAwarce接口
我們自己的組件 需要使用spring ioc的底層組件的時(shí)候,比如 ApplicationContext等我們可以通過(guò)實(shí)現(xiàn)XXXAware接口來(lái)實(shí)現(xiàn)
@Component public class CustomCompent implements ApplicationContextAware,BeanNameAware { private ApplicationContext applicationContext; @Override public void setBeanName(String name) { System.out.println("current bean name is :【"+name+"】"); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }
@Profile注解
通過(guò)@Profile注解 來(lái)根據(jù)環(huán)境來(lái)激活標(biāo)識(shí)不同的Bean
@Profile標(biāo)識(shí)在類上,那么只有當(dāng)前環(huán)境匹配,整個(gè)配置類才會(huì)生效
@Profile標(biāo)識(shí)在Bean上 ,那么只有當(dāng)前環(huán)境的Bean才會(huì)被激活
沒(méi)有標(biāo)志為@Profile的bean 不管在什么環(huán)境都可以被激活
@Configuration @PropertySource(value = {"classpath:ds.properties"}) public class MainConfig implements EmbeddedValueResolverAware { @Value("${ds.username}") private String userName; @Value("${ds.password}") private String password; private String jdbcUrl; private String classDriver; @Override public void setEmbeddedValueResolver(StringValueResolver resolver) { this.jdbcUrl = resolver.resolveStringValue("${ds.jdbcUrl}"); this.classDriver = resolver.resolveStringValue("${ds.classDriver}"); } @Bean @Profile(value = "test") public DataSource testDs() { return buliderDataSource(new DruidDataSource()); } @Bean @Profile(value = "dev") public DataSource devDs() { return buliderDataSource(new DruidDataSource()); } @Bean @Profile(value = "prod") public DataSource prodDs() { return buliderDataSource(new DruidDataSource()); } private DataSource buliderDataSource(DruidDataSource dataSource) { dataSource.setUsername(userName); dataSource.setPassword(password); dataSource.setDriverClassName(classDriver); dataSource.setUrl(jdbcUrl); return dataSource; } }
激活切換環(huán)境的方法
(1)運(yùn)行時(shí)jvm參數(shù)來(lái)切換
-Dspring.profiles.active=test|dev|prod
(2)通過(guò)代碼的方式來(lái)激活
public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setActiveProfiles("test","dev"); ctx.register(MainConfig.class); ctx.refresh(); printBeanName(ctx); }
“Spring中的各項(xiàng)注解的用法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。