springBoot系统初始化器实现(方式三)

系统初始化方式三:
application.properties配置
第一步:新建ThirdInitializer实现ApplicationContextInitializer接口
第一步.png
源码:
package com.xy.initializer;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;

import java.util.HashMap;
import java.util.Map;

@Order(2)
public class ThirdInitializer implements ApplicationContextInitializer {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        ConfigurableEnvironment environment =applicationContext.getEnvironment();
        Map map = new HashMap();
        map.put("key3","value3");
        MapPropertySource mapPropertySource = new MapPropertySource("thirdInitializer",map);
        environment.getPropertySources().addLast(mapPropertySource);
        System.out.println("Run thirdInitializer");
    }
}
第二步:配置 application.properties
第二步.png
文件内容:
server.port=8080
spring.datasource.username=root
spring.datasource.password=root123
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/databasename
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.xy.sb2.bean
mybatis.configuration.map-underscore-to-camel-case=true
logging.pattern.console=[%thread] %-5level %logger{50} - %msg%n
context.initializer.classes=com.xy.initializer.ThirdInitializer
第三步:写一个测试类service
第三步.png
源代码:
package com.xy.service;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class TestService implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("test setApplicationContext......");
        this.applicationContext = applicationContext;
    }

    public String test(){
        System.out.println(applicationContext.getBean(DemoService.class));
        DemoService t = applicationContext.getBean(DemoService.class);
        return applicationContext.getEnvironment().getProperty("key3");
    }
}
第四步:写一个controller测试
第四步.png
源码:
package com.xy.controller;

import com.xy.bean.Userinfo;
import com.xy.service.DemoService;
import com.xy.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Optional;

@Controller
@RequestMapping("/demo")
public class UserInfoController {
    @Autowired
    private DemoService demoService;
    @Autowired
    private TestService testService;
    @RequestMapping("/hello/{openid}")
    @ResponseBody
    public String hello(@PathVariable(value="openid") String openid){
        return Optional.ofNullable(demoService.getUserInfoById(openid)).map(Userinfo::toString)
                .orElse("empty String");
    }

    @RequestMapping("test")
    @ResponseBody
    public String test(){
        return testService.test();
    }
}
第五步:浏览器访问
http://localhost:8080/demo/test
第五步.png
完,喜欢的朋友可以点个赞哦!!

你可能感兴趣的:(springBoot系统初始化器实现(方式三))