[Study Notes] SpringBoot Container Function
Article directory
First, the addition of components
@Configuration
Configuration: Configuration.
@Configuration
It is an annotation introduced by Spring 3.0, which is used to replace the xml configuration file. Using this annotation tells that SpringBoot
this class is a configuration class == configuration file.
Full mode: proxyBeanMethods = true
Get the object directly from the IOC container to ensure that the components returned by each @Bean method are called a single instance, that is, the objects are the same.
@Configuration(proxyBeanMethods = true)
public class MyConfig {
@Bean("xiaobao")
public User user1(){
User user = new User("小宝", 18);
return user;
}
}
//======================//
MyConfig bean = run.getBean(MyConfig.class);
User user = bean.user1();
User user1 = bean.user1();
System.out.println(user == user1); //true
Lite mode: proxyBeanMethods = false
If it is set to false, that is, no annotation is used, the object obtained by calling the method marked with @Bean is different from the one in the IOC container, and it is a new object.
@Configuration(proxyBeanMethods = false)
public class MyConfig {
@Bean("xiaobao")
public User user1(){
User user = new User("小宝", 18);
return user;
}
}
//======================//
MyConfig bean = run.getBean(MyConfig.class);
User user = bean.user1();
User user1 = bean.user1();
System.out.println(user == user1); //false
Note: By looking @Configuration
at the source code of , we can know that the proxyBeanMethods
default is true
.
@Target({
ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
@AliasFor(
annotation = Component.class
)
String value() default "";
boolean proxyBeanMethods() default true;
}
@Bean
Role: Add components to the container. Use the method name as the component's id. The return type is the component type. The returned value is the instance of the component in the container.
@ComponentScan、@Import
* 4、@Import({
User.class, DBHelper.class})
* 给容器中自动创建出这两个类型的组件、默认组件的名字就是全类名
*
*
*
*/
@Import({
User.class, DBHelper.class})
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
public class MyConfig {
}
@Conditional
@ConditionalOnMissingBean(name = "tom")
@Import({
User.class})
public class MyConfig {
@Bean
public User user (){
User xiaobao = new User("xiaobao", 18);
return xiaobao;
}
}
ConfigurableApplicationContext run = SpringApplication.run(SpringBootDemo02Application.class, args);
boolean user = run.containsBean("user");
System.out.println("Bean中的user"+user);
Second, the introduction of native configuration files
@ImportResource
@ImportResource: Native configuration files can be imported.
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="haha" class="com.xiaobao.boot.pojo.User">
<property name="name" value="zhangsan"></property>
<property name="age" value="18"></property>
</bean>
</beans>
test:
@ImportResource("classpath:beans.xml")
@SpringBootApplication
public class MainApplication {
//固定写法,让SpringBoot跑起来
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
Object hahah = run.containsBean("haha");
System.out.println(hahah);//true
}
}
3. Configuration binding
How to use Java to read the content in the properties file and encapsulate it into JavaBean for use at any time;
public class getProperties {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties pps = new Properties();
pps.load(new FileInputStream("a.properties"));
Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
while(enum1.hasMoreElements()) {
String strKey = (String) enum1.nextElement();
String strValue = pps.getProperty(strKey);
System.out.println(strKey + "=" + strValue);
//封装到JavaBean。
}
}
}
@ConfigurationProperties
First configure the entity class:
@Component
@ConfigurationProperties(prefix = "car")
public class Car {
private String brand;
private Integer price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
properties
Configure parameters in .
car.brand=BWM
car.price = 1000000
test:
@Autowired
Car car;
@RequestMapping("/car")
public Car car(){
return car;
}
result:
@EnableConfigurationProperties
@EnableConfigurationProperties(Car.class)
//1、开启Car配置绑定功能
//2、把这个Car这个组件自动注册到容器中
public class MyConfig {
}
Attachment - Creation of SpringBoot Project
Tools: IDEA Professional Edition.
First open File->Project in the upper left corner.
The second step is to select Spring initializr
and start creating your own project name.
Note: It is best to choose version 8 for the Java version, otherwise it may lead to version conflicts! ! !
Then choose the tools you need, this is just the blogger's own configuration, you can refer to it, and other configurations can also be used!
After the creation is successful, delete the currently useless configuration.
After deleting, we must remember to use the maven3.3
version above to guide the package! ! !