@SpringBootApplication 之 @SpringBootConfiguration
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/14900455.html
@SpringBootApplication 是一个复合注解,包括
- @SpringBootConfiguration
- @EnableAutoConfiguration
- @ComponentScan
@SpringBootConfiguration 源码
可以看出@SpringBootConfiguration 其实就是一个 @Configuration,标注当前类是配置类。
从Spring3.0开始,@Configuration 用于定义配置类,可替换xml 配置文件,被注解的类内部包含一个或多个@Bean 注解的方法,这些方法将会被AnnotationConfigApplicationContext 或AnnotationConfigWebApplicationContext 类进行扫描,并且构建bean 定义,初始化Spring 容器。
@Configuration 标注在类上,等价于Spring 的xml 配置文件中的 <beans></beans>
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> </bean> <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> </bean> <!-- more bean definitions go here --> </beans>
使用@Configuration 配置Spring 并加载Spring容器
Project Directory
Maven Dependency
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.12.RELEASE</version> <relativePath/> </parent> <groupId>org.fool</groupId> <artifactId>hellospring</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
SRC
MockUserBean.java
package org.fool.spring.configuration; public class MockUserBean { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "MockUserBean{" + "username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
MockUserService.java
package org.fool.spring.configuration; public class MockUserService { public MockUserBean findUser() { MockUserBean mockUserBean = new MockUserBean(); mockUserBean.setUsername("administrator"); mockUserBean.setPassword("administrator"); return mockUserBean; } }
AppConfiguration.java
package org.fool.spring.configuration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfiguration { public AppConfiguration() { System.out.println("AppConfiguration init..."); } @Bean public MockUserBean userBean() { MockUserBean mockUserBean = new MockUserBean(); mockUserBean.setUsername("admin"); mockUserBean.setPassword("admin"); return mockUserBean; } @Bean public MockUserService userService() { return new MockUserService(); } }
Note: 带有@Bean 的注解方法会返回一个对象,该对象会被注册到Spring IoC 容器中。
MainTest.java
package org.fool.spring.configuration; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class MainTest { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(AppConfiguration.class); MockUserBean mockUserBean = context.getBean(MockUserBean.class); MockUserService mockUserService = context.getBean(MockUserService.class); System.out.println(mockUserBean); System.out.println(mockUserService.findUser()); } }
Run
欢迎点赞关注和收藏
强者自救 圣者渡人