承接上文:
import注解的使用:
作用:引用時,容器自動創建出這填寫的類型的組件,默認組件的名字就是全類名。
往容器中添加兩個組件
@Import({User.class, DBHelper.class})
測試:
System.out.println("=======測試import========");
String[] beanNamesForType = run.getBeanNamesForType(User.class);
System.out.println("==============");
for(String s: beanNamesForType){
System.out.println(s);
}
DBHelper bean1 = run.getBean(DBHelper.class);
System.out.println(bean1);
結果:
com.xbhog.pojo.User
user
ch.qos.logback.core.db.DBHelper@68565bc7
可以看出,使用import的組件返回名字時全類名,我們自己寫的組件,就是原來的。
條件裝配
條件裝配:滿足Conditional指定的條件,則進行組件注入.
組件注解:
實例采用以下兩個為例:
- @ConditionalOnMissingBean(name="tom11")
- @ConditionalOnBean(name="tom11")
-
ConditionalOnMissingBean表示當容器中沒有這個tom11組件時,執行下面的組件
@Import({User.class, DBHelper.class}) @Configuration(proxyBeanMethods = false) public class MyConfig { @ConditionalOnMissingBean(name="tom11") @Bean public User user(){ User user = new User("xbhog", 18); return user; } //@Bean("tom11") public Pet tomcat(){ return new Pet("tomcat"); } }
主啟動程序中的代碼片段:
ConfigurableApplicationContext run = SpringApplication.run(Myapp.class, args); //返回IOC容器 boolean tom11 = run.containsBean("tom11"); //容器中tom11組件:false //容器中user組件:true System.out.println("容器中tom11組件:"+tom11); boolean user = run.containsBean("user"); System.out.println("容器中user組件:"+user);
結果:
容器中tom11組件:false 容器中user組件:true
-
ConditionalOnBean表示當容器中存在該組件時,才執行下面的內容
@Import({User.class, DBHelper.class}) @Configuration(proxyBeanMethods = false) public class MyConfig { @ConditionalOnBean(name="tom11") @Bean public User user(){ User user = new User("xbhog", 18); return user; } //@Bean("tom11") public Pet tomcat(){ return new Pet("tomcat"); } }
主程序啟動代碼:
ConfigurableApplicationContext run = SpringApplication.run(Myapp.class, args); //返回IOC容器 boolean tom11 = run.containsBean("tom11"); //容器中tom11組件:false //容器中user組件:true System.out.println("容器中tom11組件:"+tom11); boolean user = run.containsBean("user"); System.out.println("容器中user組件:"+user);
結果:
容器中tom11組件:false 容器中user組件:false
配置綁定:
解決的問題:
將properties文件中的內容綁定到類的屬性中,並把它們封裝到JavaBean中,以供隨時使用
有兩種方法實現:
第一種:
采用component+configurationProperties來綁定properties中的數據
properties數據:(brand:別摸我)
mycar.brand = BMW
mycar.price = 200000
編寫Car實體類:
package com.xbhog.popj;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@AllArgsConstructor //有參構造
@NoArgsConstructor //無參構造
@Component //將該類加入到容器中,來獲得Spring Boot提供的強大的功能
@ConfigurationProperties(prefix="mycar") //實現讀取綁定到properties中的以mycar前綴的數據
public class Car {
private String brand;
private String price;
}
編寫Controller:
package com.xbhog.controller;
import com.xbhog.popj.Car;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@Autowired //自動匹配到容器中的car
Car car;
@RequestMapping("/car")
public Car car(){
return car;
}
}
實現效果:
{"brand":"BMM","price":"200000"}
第二種:
EnableConfigurationProperties+ConfigurationProperties
@EnableConfigurationProperties(類.class)
功能:
- 開啟類配置綁定功能
- 把這個類這個組件自動注冊到容器中
需要把EnableConfigurationProperties注解加到配置類中,因為配置類本身就是組件。
MyConfig:
@Configuration
@EnableConfigurationProperties(Car.class) //開啟Car配置綁定功能n並把Car自動注冊到容器中
public class MyConfig {
}
@ConfigurationProperties(prefix = “mycar”)
找到配置文件中application.properties中的前置為mycar的內容
實現效果:
{"brand":"BMM","price":"200000"}
原生配置文件引入:
使用@ImportResouce注解將bean.xml中的組件加入到容器中
bean.xml:
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="haha" class="com.xbhog.pojo.User">
<property name="name" value="zhangsan"></property>
<property name="age" value="18"></property>
</bean>
<bean id="hehe" class="com.xbhog.pojo.Pet">
<property name="name" value="tomcat"></property>
</bean>
</beans>
package com.xbhog.springboot1times;
import ch.qos.logback.core.db.DBHelper;
import com.xbhog.config.MyConfig;
import com.xbhog.pojo.Pet;
import com.xbhog.pojo.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
//@SpringBootApplication
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.xbhog")
@ImportResource("classpath:bean.xml")
public class Myapp {
public static void main(String[] args) {
/*1. 返回我們IOC容器*/
ConfigurableApplicationContext run = SpringApplication.run(Myapp.class, args);
boolean haha = run.containsBean("haha");
boolean hehe = run.containsBean("hehe");
System.out.println("是否存在:"+haha+"==="+hehe);
}
}
結果:是否存在:true===true
參考:
結束:
如果你看到這里或者正好對你有所幫助,希望能點個關注或者推薦,感謝;
有錯誤的地方,歡迎在評論指出,作者看到會進行修改。