package com.example.boot3.Config;
import com.alibaba.druid.FastsqlException;
import com.example.boot3.bean.Pig;
import com.example.boot3.bean.Sheep;
import com.example.boot3.bean.User;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Scope;
//主要用于导入第三方的需要组件化并配置属性的类
//因为默认SpringBoot只扫描自己bootApp所在的包及其子包,如果导入第三方包,即便标注了注解@Component@ConfigurationProperties等
//依旧无法导入组件并进行属性绑定
@EnableConfigurationProperties(Sheep.class)//开启后会自动把这个Sheep放到容器中,并开启sheep的属性绑定
@SpringBootConfiguration//这是一个SpringBoot配置类
//@Configuration//这是一个配置类
@ConditionalOnClass(name = "com.alibaba.druid.FastsqlException")//放在类级别就会使整个类具有条件注解的效果
//如果没有整个不生效
@Import(FastsqlException.class)//导入任意类作为组件,但是组件的名字默认是全类名!!!!!
public class AppConfig
{
// 组件默认是单实例的
@Scope("prototype")
@Bean//替代Bean标签
public User user(){//默认的Bean名字就是方法名字
var user = new User();
user.setName("Jack");
user.setId(1L);
return user;
}
@Bean
@ConfigurationProperties(prefix = "pig")//即能标注在类上也能标注在方法上
public Pig pig(){
return new Pig();
}
// 导入第三方Bean
@Bean
public FastsqlException fastsqlException(){
return new FastsqlException();
}
}
package com.example.boot3.Config;
import com.alibaba.druid.FastsqlException;
import com.example.boot3.bean.Pig;
import com.example.boot3.bean.Sheep;
import com.example.boot3.bean.User;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Scope;
//主要用于导入第三方的需要组件化并配置属性的类
//因为默认SpringBoot只扫描自己bootApp所在的包及其子包,如果导入第三方包,即便标注了注解@Component@ConfigurationProperties等
//依旧无法导入组件并进行属性绑定
@EnableConfigurationProperties(Sheep.class)//开启后会自动把这个Sheep放到容器中,并开启sheep的属性绑定
@SpringBootConfiguration//这是一个SpringBoot配置类
//@Configuration//这是一个配置类
@ConditionalOnClass(name = "com.alibaba.druid.FastsqlException")//放在类级别就会使整个类具有条件注解的效果
//如果没有整个不生效
@Import(FastsqlException.class)//导入任意类作为组件,但是组件的名字默认是全类名!!!!!
public class AppConfig
{
// 组件默认是单实例的
@Scope("prototype")
@Bean//替代Bean标签
public User user(){//默认的Bean名字就是方法名字
var user = new User();
user.setName("Jack");
user.setId(1L);
return user;
}
@Bean
@ConfigurationProperties(prefix = "pig")//即能标注在类上也能标注在方法上
public Pig pig(){
return new Pig();
}
// 导入第三方Bean
@Bean
public FastsqlException fastsqlException(){
return new FastsqlException();
}
}
package com.example.boot3.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//@Component
//@ConfigurationProperties(prefix = "pig")//因为我们标注了这个注解
//因此会进行自动绑定
public class Pig
{
private Long id;
private String name;
private Integer age;
public Pig() {
}
@Override
public String toString() {
return "Pig{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Pig(Long id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
}
package com.example.boot3.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//@Component
//@ConfigurationProperties(prefix = "pig")//因为我们标注了这个注解
//因此会进行自动绑定
public class Pig
{
private Long id;
private String name;
private Integer age;
public Pig() {
}
@Override
public String toString() {
return "Pig{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Pig(Long id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
}
package com.example.boot3.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "sheep")
//@Component
public class Sheep {
private Long id;
private String name;
private Integer age;
public Sheep() {
}
public Sheep(Long id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Sheep{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.example.boot3.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "sheep")
//@Component
public class Sheep {
private Long id;
private String name;
private Integer age;
public Sheep() {
}
public Sheep(Long id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Sheep{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.example.boot3.Config;
import com.example.boot3.bean.Cat;
import com.example.boot3.bean.Dog;
import com.example.boot3.bean.User;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
@SpringBootConfiguration
public class AppConfig2 {
@Bean
@ConditionalOnClass(name="com.alibaba.druid.FastsqlException")
public Cat cat01(){
return new Cat();
}
@Bean
@ConditionalOnMissingClass(value="com.alibaba.druid.FastsqlException")
public Dog dog01(){
return new Dog();
}
@Bean
@ConditionalOnBean(name = "dog01")
public User zhangsan(){
return new User();
}
@Bean
@ConditionalOnMissingBean(name = "dog01")
public User lisi(){
return new User();
}
}
package com.example.boot3.Config;
import com.example.boot3.bean.Cat;
import com.example.boot3.bean.Dog;
import com.example.boot3.bean.User;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
@SpringBootConfiguration
public class AppConfig2 {
@Bean
@ConditionalOnClass(name="com.alibaba.druid.FastsqlException")
public Cat cat01(){
return new Cat();
}
@Bean
@ConditionalOnMissingClass(value="com.alibaba.druid.FastsqlException")
public Dog dog01(){
return new Dog();
}
@Bean
@ConditionalOnBean(name = "dog01")
public User zhangsan(){
return new User();
}
@Bean
@ConditionalOnMissingBean(name = "dog01")
public User lisi(){
return new User();
}
}
package com.example.boot3;
import com.example.boot3.bean.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan("com.example")//改变包扫描路径
@SpringBootApplication(scanBasePackages = "com.example.boot3")//手动指定扫描包位置
public class Boot3Application
{
public static void main(String[] args) {
var ioc = SpringApplication.run(Boot3Application.class, args);
// for (String name : ioc.getBeanDefinitionNames()) {
// System.out.println(name);
// }
// Object user = ioc.getBean("user");
// Object user1 = ioc.getBean("user");
// System.out.println(user == user1);
// System.out.println(ioc.getBean("fastsqlException"));
for (String s : ioc.getBeanNamesForType(Cat.class)) {
System.out.println(s);
}
for (String s : ioc.getBeanNamesForType(Dog.class)) {
System.out.println(s);
}
for (String s : ioc.getBeanNamesForType(User.class)) {
System.out.println(s);
}
Object pig = ioc.getBean("pig");
System.out.println(pig);
// for (String s : ioc.getBeanNamesForType(Sheep.class)) {
// System.out.println(s);
// }
Object sheep = ioc.getBean(Sheep.class);
System.out.println(sheep);
}
}
package com.example.boot3;
import com.example.boot3.bean.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan("com.example")//改变包扫描路径
@SpringBootApplication(scanBasePackages = "com.example.boot3")//手动指定扫描包位置
public class Boot3Application
{
public static void main(String[] args) {
var ioc = SpringApplication.run(Boot3Application.class, args);
// for (String name : ioc.getBeanDefinitionNames()) {
// System.out.println(name);
// }
// Object user = ioc.getBean("user");
// Object user1 = ioc.getBean("user");
// System.out.println(user == user1);
// System.out.println(ioc.getBean("fastsqlException"));
for (String s : ioc.getBeanNamesForType(Cat.class)) {
System.out.println(s);
}
for (String s : ioc.getBeanNamesForType(Dog.class)) {
System.out.println(s);
}
for (String s : ioc.getBeanNamesForType(User.class)) {
System.out.println(s);
}
Object pig = ioc.getBean("pig");
System.out.println(pig);
// for (String s : ioc.getBeanNamesForType(Sheep.class)) {
// System.out.println(s);
// }
Object sheep = ioc.getBean(Sheep.class);
System.out.println(sheep);
}
}
pig.id=1
pig.name=佩奇
pig.age=5
sheep.id=1
sheep.name=苏茜
sheep.age=5
dog.id=1
dog.name=丹妮
dog.age=5
horse.id=1
horse.name=佩德罗
horse.age=5
rabbit.id=1
rabbit.name=瑞贝卡
rabbit.age=5
pig.id=1
pig.name=佩奇
pig.age=5
sheep.id=1
sheep.name=苏茜
sheep.age=5
dog.id=1
dog.name=丹妮
dog.age=5
horse.id=1
horse.name=佩德罗
horse.age=5
rabbit.id=1
rabbit.name=瑞贝卡
rabbit.age=5