Spring 开发技巧
1. @FrameworkEndpoint
该注解只用于框架提供端点,语义和 Controller 注解一致,
如果使用Controller提供了和FrameworkEndpoint标识的相同路径,@FrameworkEndpoint注解的类则不生效;
一般的场景用于:覆盖框架默认提供的端点;
2.BeanPostProcessor 初始化钩子方法
org.springframework.beans.factory.config.BeanPostProcessor
@Component
public class SpringSocialConfigurerPostProcessor implements BeanPostProcessor {
// 任何bean初始化回调之前
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
//任何bean初始化回调之后
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//判断当前加载的bean的name
if (beanName.equals("imoocSocialSecurityConfig")) {
SpringSocialConfigurer config = (SpringSocialConfigurer) bean;
config.signupUrl("/social/signUp");
return bean;
}
return bean;
}
}
3.@Import
@Import注解配合自定义注解,引入新功能
/**
*
* @author kevin
*
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(LogFilterAutoConfiguration.class) //引入LogFilterAutoConfiguration配置类
public @interface EnableLogFilter {
}
@import引入相关的bean配置
@Import({ ClientDetailsServiceConfiguration.class, AuthorizationServerEndpointsConfiguration.class })
public class AuthorizationServerSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private List<AuthorizationServerConfigurer> configurers = Collections.emptyList();
@Autowired
private ClientDetailsService clientDetailsService;
@Autowired
private AuthorizationServerEndpointsConfiguration endpoints;
}
分析类注释得出结论:
1、声明一个bean
2、导入@Configuration注解的配置类
3、导入ImportSelector的实现类 (通过ImportSelector注册一堆类)
4、导入ImportBeanDefinitionRegistrar的实现类(通过ImportBeanDefinitionRegistrar 注册一堆类)
加载传统的xml文件
4. 针对集合的装配
Spring默认的,针对集合的装配,所有满足集合限定泛型的实现类都放里面 list set map
1.基于自定义注解集合装配
收集所有加自定义注解的并实现RestTemplate接口的 类
2.基于实现类的集合装配
收集系统中所有实现ValidateCodeProcessor接口的实现
key为bean name、value为bean实例
Spring会查找应用上下文里可赋给MediaPlayer类型的bean放进mediaPlayerList
@Autowired
private List<MediaPlayer> mediaPlayerList = Collections.emptyList();
4. 多变的注解
@DependsOn
@ConditionalOnClass({ AuthenticationManager.class, GlobalAuthenticationConfigurerAdapter.class })
当前上下文中存在某个对象时,才会实例化一个Bean
@ConditionalOnBean(name = DEFAULT_FILTER_NAME)
当前上下文中存在某个bean时,才会实例化一个Bean
@ConditionalOnMissingBean(AuthenticationEventPublisher.class)
当前上下文中不存在某个bean时,才会实例化一个Bean
@ConfigurationProperties(prefix = "security")
再properties类上, 表示该类属性在配置文件以security开头
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true)
再类上, 表示该类属性在配置文件spring.aop.proxy-target-class=true, 则创建实例化一个Bean. matchIfMissing表示如果没有设置就默认则创建实例化一个Bean.
5. 配置文件application.properties 或者application.yml
spring-boot-autoconfigure-1.5.x包里
5. ApplicationContext 获取
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context.getServletContext());
DefaultMQProducer defaultMQProducer = (DefaultMQProducer) ctx.getBean("defaultMQProducer");
@Component("springBeanUtilCore")
public class SpringBeanUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (SpringBeanUtil.applicationContext == null) {
SpringBeanUtil.applicationContext = applicationContext;
}
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 通过Bean名字获取Bean
*
* @param beanName
* @return
*/
public static Object getBean(String beanName) {
return getApplicationContext().getBean(beanName);
}
/**
* 通过Bean类型获取Bean
*
* @param beanClass
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> beanClass) {
return getApplicationContext().getBean(beanClass);
}
}
@SpringBootApplication
public class CmsWebApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(CmsWebApplication.class);
springApplication.addListeners(new ApplicationPidFileWriter());
ApplicationContext ctx = springApplication.run(args);
}
}
————————————————
版权声明:本文为CSDN博主「凯歌的博客」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/kaige8312/article/details/85293384
- 点赞
- 收藏
- 关注作者
评论(0)