第一种:实现ApplicationListener<ContextRefreshedEvent>接口 package bdc.base; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import bdc.ws.dao.CommonDao; import bdc.ws.entity.RsaKey; //@Component 使用这个注解或者采用bean注入<bean id="afterSpringBegin" class="bdc.base.AfterSpringBegin"></bean> public class AfterSpringBegin implements ApplicationListener<ContextRefreshedEvent> { @Resource private CommonDao commonDao; public static final Map<String, RsaKey> KEY_RSA = new HashMap<String, RsaKey>(); @Override public void onApplicationEvent(ContextRefreshedEvent event) { if (event.getApplicationContext().getParent() == null) { try { List<RsaKey> selectAllRsaKey = commonDao.selectAllRsaKey(); for (int i = 0; i < selectAllRsaKey.size(); i++) { KEY_RSA.put(selectAllRsaKey.get(i).getUserOrg(),selectAllRsaKey.get(i)); } } catch (Exception e) { e.printStackTrace(); } } } } 第二种使用@PostConstruct注解 @Component public class TuneNetInterface { private static Client client; private static QName opName; @Value("${WebUrl}") private String WebUrl; @Value("${operation}") private String operation; @PostConstruct public void init() { //以下为具体逻辑 JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory .newInstance(BusFactory.newInstance().createBus()); client = factory.createClient(WebUrl); Endpoint endpoint = client.getEndpoint(); opName = new QName(endpoint.getService().getName().getNamespaceURI(), operation); BindingInfo bindingInfo = endpoint.getEndpointInfo().getBinding(); if (bindingInfo.getOperation(opName) == null) { for (BindingOperationInfo operationInfo : bindingInfo.getOperations()) { if (operation.equals(operationInfo.getName().getLocalPart())) { opName = operationInfo.getName(); break; } } } } public static Object[] request(String content) throws Exception { return client.invoke(opName, content); } } 第三种:使用@Configuration注解 package com.dxz.demo.configuration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; @Configuration //添加自动扫描注解,basePackages为TestBean包路径 @ComponentScan(basePackages = "com.dxz.demo.configuration") public class TestConfiguration { public TestConfiguration() { System.out.println("TestConfiguration容器启动初始化。。。"); } /*// @Bean注解注册bean,同时可以指定初始化和销毁方法 // @Bean(name="testNean",initMethod="start",destroyMethod="cleanUp") @Bean @Scope("prototype") public TestBean testBean() { return new TestBean(); }*/ } 从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。 注意:@Configuration注解的配置类有如下要求: @Configuration不可以是final类型; @Configuration不可以是匿名类; 嵌套的configuration必须是静态类。 一、用@Configuration加载spring 1.1、@Configuration配置spring并启动spring容器 1.2、@Configuration启动容器+@Bean注册Bean 1.3、@Configuration启动容器+@Component注册Bean 1.4、使用 AnnotationConfigApplicationContext 注册 AppContext 类的两种方法 1.5、配置Web应用程序(web.xml中配置AnnotationConfigApplicationContext) 二、组合多个配置类 2.1、在@configuration中引入spring的xml配置文件 2.2、在@configuration中引入其它注解配置 2.3、@configuration嵌套(嵌套的Configuration必须是静态类) 三、@EnableXXX注解 四、@Profile逻辑组配置 五、使用外部变量 一、@Configuation加载Spring方法 1.1、@Configuration配置spring并启动spring容器 @Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) 复制代码 package com.dxz.demo.configuration; import org.springframework.context.annotation.Configuration; @Configuration public class TestConfiguration { public TestConfiguration() { System.out.println("TestConfiguration容器启动初始化。。。"); } } 复制代码 相当于: 复制代码 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" default-lazy-init="false"> </beans> 复制代码 主方法进行测试: 复制代码 package com.dxz.demo.configuration; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class TestMain { public static void main(String[] args) { // @Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContext ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class); // 如果加载spring-context.xml文件: // ApplicationContext context = new // ClassPathXmlApplicationContext("spring-context.xml"); } } 复制代码 从运行主方法结果可以看出,spring容器已经启动了: SpringBoot项目初始化 1、 创建自定义类实现 CommandLineRunner接口,重写run()方法。springboot启动之后会默认去扫描所有实现了CommandLineRunner的类,并运行其run()方法。 @Component @Order(2) //通过order值的大小来决定启动的顺序 public class AskForLeave implements CommandLineRunner { @Override public void run(String... args) throws Exception { askForLeave(); } public void askForLeave(){ System.out.println("项目启动了,执行了方法"); } } 2、创建自定义类实现ApplicationRunner 接口,重写run()方法。 @Component @Order(3) public class Hello implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { hello(); } public void hello(){ System.out.println("项目又启动了,这次使用的是:继承 ApplicationRunner"); } } 关于二者的区别: 其实并没有什么区别,如果想获取更加详细的参数的时候,可以选择使用ApplicationRunner接口。其参数类型为:ApplicationArguments 。
摘自:https://www.cnblogs.com/gaomanito/p/9604569.html
个人理解,如有错误,欢迎指正!