Practical skills of Spring and SpringBoot development
Links
SpringBoot knowledge column: https://blog.csdn.net/qq_37248504/category_10924414.html
Spring knowledge column: https://blog.csdn.net/qq_37248504/category_10924424.html
Back end development knowledge column: https://blog.csdn.net/qq_37248504/category_9699519.html
Get ApplicationContext
Direct injection
- Inject directly where it is used
@Autowired private ApplicationContext applicationContextBean;
Implement ApplicationContextAware interface
- The instance code, SpringContextUtils, defines the global tool class for obtaining context
@Component public class SpringContextUtils implements ApplicationContextAware { private static final Logger logger = LoggerFactory.getLogger(SpringContextUtils.class); /** * Context object instance */ private static ApplicationContext applicationContext = null; private SpringContextUtils() { } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if (Objects.isNull(SpringContextUtils.applicationContext)) { logger.info(LogConst.LOG_SUCCESS_PREFIX + "ApplicationUtils initialization..."); SpringContextUtils.applicationContext = applicationContext; } logger.info(LogConst.LOG_SUCCESS_PREFIX + "ApplicationUtils Initialization succeeded!"); } /** * Get the current ApplicationContext * * @return ApplicationContext */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * Get Bean by name * * @param name Bean Name of * @return Object */ @SuppressWarnings("all") public static Object getBean(String name) { return getApplicationContext().getBean(name); } /** * Get the Bean from ApplicationContext and transform it * * @param tClass class * @param <T> T * @return T */ public static <T> T getBean(Class<T> tClass) { return getApplicationContext().getBean(tClass); } }
Get ApplicationContext Bean as a parameter
- Spring will automatically pass in ApplicationContext when initializing AutoConfiguration. At this time, we can use the following method to obtain ApplicationContext:
@Configuration public class TestConfig { private static final Logger logger = LoggerFactory.getLogger(TestConfig.class); /** * Get Bean as parameter * * @param applicationContext applicationContext */ public TestConfig(ApplicationContext applicationContext) { logger.info(String.valueOf(applicationContext)); } }
Get ApplicationContext at startup
- When starting the Spring Boot project, you need to call springapplication run() method, and the return value of run() method is ApplicationContext. We can save the ApplicationContext object returned by run() method for easy use at any time:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @MapperScan("com.li.springbootproject.mapper") @EnableAspectJAutoProxy public class SpringBootProjectApplication { private static final Logger logger = LoggerFactory.getLogger(SpringBootProjectApplication.class); public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringBootProjectApplication.class, args); logger.info(LogConst.LOG_SUCCESS_PREFIX + "Successful startup:" + applicationContext); } }
Get through WebApplicationContextUtils
Spring provides a tool class for obtaining ApplicationContext objects:
@Autowired private ServletContext servletContext; @Override public String testSix() { WebApplicationContext context1 = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); WebApplicationContext context2 = WebApplicationContextUtils.getWebApplicationContext(servletContext); return "Test successful!"; }
Initialization of Bean
Implement InitializingBean interface
- The InitializingBean interface is at org springframework. beans. In the factory package, this method is not officially recommended, and it is used in the framework layer.
@Configuration public class PeopleBean implements InitializingBean { private static final Logger logger = LoggerFactory.getLogger(PeopleBean.class); private int id; private String name; @Bean public PeopleBean peopleBeanInit(){ PeopleBean peopleBeanInit = new PeopleBean(); peopleBeanInit.setId(this.id); peopleBeanInit.setName(this.name); return peopleBeanInit; } // Object attribute assignment @Override public void afterPropertiesSet() throws Exception { logger.info("=====> I want to be right peoplebean Initialize!"); this.id = 100; this.name = "Li Si"; } }
@Use of PostConstruct annotations
@Configuration public class PeopleOneBeanInit { private int id; private String name; @Bean public PeopleOneBeanInit peopleOneBean(){ PeopleOneBeanInit peopleOneBean = new PeopleOneBeanInit(); peopleOneBean.setId(this.id); peopleOneBean.setName(this.name); return peopleOneBean; } // Using this annotation, you can initialize this type of attribute here. @PostConstruct public void init(){ this.id = 1001; this.name = "Test Zhang San"; } }
Spring xml configuration method
<bean id="helloWorld" class="com.li.entity.HelloWorld" scope="singleton" init-method="init" destroy-method="destroy"> </bean>
public class HelloWorld { public void init() { // Object properties can be initialized here } }
- Annotation method
@Configuration public class PeopleTwoConfig { /** * It is equivalent to init method in xml configuration * @return */ @Bean(initMethod = "init") public PeopleTwoBeanInit peopleTwoBeanInit(){ return new PeopleTwoBeanInit(); } } public class PeopleTwoBeanInit { public void init(){ this.id = 1005; this.name = "Li Si test 1"; } }
Modify the attribute information of the Bean
Implement the BeanPostProcessor interface
Implement the BeanPostProcessor interface and rewrite postProcessBeforeInitialization() and postProcessAfterInitialization(). In the rewritten method, you can obtain the properties of the Bean and carry out relevant operations on the Bean.
@Component public class UserBeanPostProcessor implements BeanPostProcessor { private static final Logger logger = Logger.getLogger(String.valueOf(UserBeanPostProcessor.class)); // Here you can get the relevant information of the bean and carry out relevant operations @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { final String name = "myUser"; if(name.equals(beanName)){ User user = (User) bean; user.setName("Li Si"); user.setDate(new Date()); logger.info("=====> postProcessBeforeInitialization(): "+ JSONObject.toJSONString(user)); } return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } }
Get BeanFactory
Aware in Spring
Aware is mainly used to assist Spring in accessing the data in the container. In the method of implementing this class rewriting, you can get the information of relevant components, so you can expand accordingly.
Common Aware implementation classes
- BeanFactory aware: get BeanFactory container
- BeanNameAware: get the name of the Bean
- Get ApplicationContext: applicationcontextware
Get BeanFactory
Implement BeanFactoryAware and obtain relevant Spring components in setXXX().
@Component public class GetBeanFactory implements BeanFactoryAware { private static final Logger logger = LoggerFactory.getLogger(GetBeanFactory.class); private BeanFactory beanFactory; @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = beanFactory; MyBeans bean = beanFactory.getBean(MyBeans.class); logger.info("-----> Get current BeanFactory "+ beanFactory); } }
Load resources after service startup
-
springboot provides us with two ways to "boot up": ApplicationRunner and CommandLineRunner.
-
The purpose of these two methods is to implement some methods immediately when the project starts. We can do this by implementing ApplicationRunner and CommandLineRunner, both of which are executed after the spring application is executed.
Order annotation usage
- After the Spring container is started, you can load some resources or do some business operations
- Implement CommandLineRunner
@Component @Order(1) public class OrderTestOne implements CommandLineRunner { private static final Logger logger = LoggerFactory.getLogger(OrderTestOne.class); /** * implement * * @param args parameter * @throws Exception */ @Override public void run(String... args) throws Exception { logger.info("OrderTestOne..."); } }
- The smaller the value of order, the higher the priority
- If order is not marked with numbers, it defaults to the lowest priority, because its default value is the maximum value of int
Implement ApplicationRunner and Ordered interfaces
@Component public class OrderTestTwo implements ApplicationRunner, Ordered { private static final Logger logger = LoggerFactory.getLogger(OrderTestTwo.class); /** * implement * * @param args parameter * @throws Exception */ @Override public void run(ApplicationArguments args) throws Exception { logger.info("OrderTestTwo..."); } /** * Define the order of execution * * @return int */ @Override public int getOrder() { return 2; } }
Fluent Builder API
- SpringApplicationBuilder allows you to link multiple method calls together, including parent and child methods, which allow you to create a hierarchy, as shown in the following example:
new SpringApplicationBuilder() .sources(Parent.class) .child(Application.class) .bannerMode(Banner.Mode.OFF) .run(args);
Application exit
Each Spring application registers a JVM close hook to ensure that the ApplicationContext can be closed gracefully when exiting. All standard Spring lifecycle callbacks (such as DisposableBean interface or @ PreDestroy annotation) can be used.
In addition, if you want to call springapplication If a specific exit code is returned when exit(), the bean can implement org springframework. boot. Exitcotegenerator interface. After that, the exit code will be passed to system Exit() to return it as a status code, as shown in the example:
@SpringBootApplication public class ExitCodeApplication { @Bean public ExitCodeGenerator exitCodeGenerator() { return () -> 100; } public static void main(String[] args) { System.exit(SpringApplication.exit(SpringApplication.run(ExitCodeApplication.class, args))); } }
Inherit Starter Parent
- To inherit the project configuration from spring boot starter parent, you only need to set the parent as follows:
<!-- from Spring Boot Inherit default configuration --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent>
be careful
You only need to specify the version number of Spring Boot on this dependency. If you want to import other starter s, you can safely omit the version number.
With this setting, you can also override the configuration properties in your project to override individual dependencies. For example, to upgrade to another Spring Data distribution, you need to add the following to POM XML file.
<properties> <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version> </properties>
Read the attribute registration Bean in the configuration file
- Define Bean
@Component @ConfigurationProperties(prefix = "spring.redis") public class RedisProperties { private String host; private int port; private int timeout; private String password; // Omit getset }
- Attribute values defined in yml
spring: redis: host: 127.0.0.1 port: 6379 password:
SpringBoot Event synchronous and asynchronous processing
-
In Spring, org. Org is called when the container is initialized springframework. context. The reFresh() method in the configurableapplicationcontext interface loads Be~an, which will listen to and register events.
-
See the address for details of the event mechanism: https://blog.csdn.net/qq_37248504/article/details/115269995