文章目录
1.@Configuration 添加/注入bean
1.注入bean
1.编写一个JavaBean,Monster.java
package com.sun.springboot.bean;
/**
* @author 孙显圣
* @version 1.0
*/
public class Monster {
private Integer id;
private String name;
private Integer age;
private String skill;
public Monster() {
}
public Monster(Integer id, String name, Integer age, String skill) {
this.id = id;
this.name = name;
this.age = age;
this.skill = skill;
}
public Integer getId() {
return id;
}
public void setId(Integer 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 String getSkill() {
return skill;
}
public void setSkill(String skill) {
this.skill = skill;
}
@Override
public String toString() {
return "Monster{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", skill='" + skill + '\'' +
'}';
}
}
2.创建一个config文件夹(名字任意),用于存放配置Bean的类(相当于配置文件)
3.BeanConfig.java
package com.sun.springboot.config1;
import com.sun.springboot.bean.Monster;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
/**
* @author 孙显圣
* @version 1.0
*/
@Configuration //这个注解类似于原来的配置文件
public class BeanConfig {
/**
* 类型:Monster
* id:monster01, 如果想要指定id,只需设置@Bean的name属性
* 单例bean注入容器:return new Monster()
* @return
*/
@Bean
// @Scope("prototype") //默认是单例的如果希望是多例的,则加一个注解即可。
public Monster monster01() {
return new Monster(1, "牛魔王", 12, "牛牛冲撞");
}
}
4.测试使用 MainApp.java
package com.sun.springboot;
import com.sun.springboot.bean.Monster;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/**
* @author 孙显圣
* @version 1.0
*/
//表示是一个springboot项目
@SpringBootApplication(scanBasePackages = {"com.sun"}) //扫描指定的包,参数是数组类型的
public class MainApp {
public static void main(String[] args) {
//启动springboot
ConfigurableApplicationContext ioc = SpringApplication.run(MainApp.class, args);
//由于是单例所以可以通过类型来获取
Monster bean = ioc.getBean(Monster.class);
System.out.println(bean);
}
}
2.结果展示
2.细节说明
1.@Configuration的proxyBeanMethods属性
2.可以有多个配置类但是注入bean的id不能相同
1.新建一个BeanConfig01.java作为第二个配置类,故意和第一个配置类注入的bean的id相同
package com.sun.springboot.config1;
import com.sun.springboot.bean.Monster;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author 孙显圣
* @version 1.0
*/
//这是第二个bean配置文件
@Configuration
public class BeanConfig01 {
@Bean
public Monster monster01() {
return new Monster(1, "牛魔王", 12, "牛牛冲撞");
}
}
2.会报错
3.修改一下这个bean的id
4.MainApp.java获取这两个bean
package com.sun.springboot;
import com.sun.springboot.bean.Monster;
import com.sun.springboot.config1.BeanConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/**
* @author 孙显圣
* @version 1.0
*/
//表示是一个springboot项目
@SpringBootApplication(scanBasePackages = {"com.sun"}) //扫描指定的包,参数是数组类型的
public class MainApp {
public static void main(String[] args) {
//启动springboot
ConfigurableApplicationContext ioc = SpringApplication.run(MainApp.class, args);
//获取第一个bean
Monster bean1 = ioc.getBean("monster01", Monster.class);
//获取第二个bean
Monster bean2 = ioc.getBean("monster02", Monster.class);
System.out.println(bean1);
System.out.println(bean2);
}
}
5.成功获取不同配置类注入的bean
2.@Import 注入bean
1.新建两个空的bean,Dog和Cat
2.配置类中使用注解注入单例bean,id为全类名
3.MainApp.java 获取两个bean对象
package com.sun.springboot;
import com.sun.springboot.bean.Cat;
import com.sun.springboot.bean.Dog;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/**
* @author 孙显圣
* @version 1.0
*/
//表示是一个springboot项目
@SpringBootApplication(scanBasePackages = {"com.sun"}) //扫描指定的包,参数是数组类型的
public class MainApp {
public static void main(String[] args) {
//启动springboot
ConfigurableApplicationContext ioc = SpringApplication.run(MainApp.class, args);
Cat cat = ioc.getBean(Cat.class);
Dog dog = ioc.getBean(Dog.class);
System.out.println(cat);
System.out.println(dog);
}
}
4.结果展示
1.成功得到两个bean对象
2.id为全类名
3.@Conditional 条件装配
1.基本介绍
1.介绍
2.扩展注解功能
2.应用实例1:@ConditionalOnBean
1.需求分析
2.新建一个配置类 ConditionalConfig.java 用于测试条件装配
package com.sun.springboot.config1;
import com.sun.springboot.bean.Cat;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author 孙显圣
* @version 1.0
*/
//测试条件装配
@Configuration
public class ConditionalConfig {
//这里注入一个猫girl
@Bean(name = "cat_girl")
public Cat cat() {
return new Cat();
}
@Bean
@ConditionalOnBean(name = "cat_girl") //注入一个猫boy,条件是有一个猫girl
public Cat cat_boy() {
return new Cat();
}
}
3.测试获取cat_boy
package com.sun.springboot;
import com.sun.springboot.bean.Cat;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/**
* @author 孙显圣
* @version 1.0
*/
//表示是一个springboot项目
@SpringBootApplication(scanBasePackages = {"com.sun"}) //扫描指定的包,参数是数组类型的
public class MainApp {
public static void main(String[] args) {
//启动springboot
ConfigurableApplicationContext ioc = SpringApplication.run(MainApp.class, args);
//获取cat_boy
Cat cat = ioc.getBean("cat_boy", Cat.class);
System.out.println(cat);
}
}
4.结果展示
3.应用实例2:@ConditionalOnMissingBean
1.需求分析
注入一个猫boy,条件是没有一个猫girl
2.ConditionalConfig.java 添加方法
@Bean
@ConditionalOnMissingBean(name = "cat_girl") //注入一个猫boy,条件是没有一个猫girl
public Cat cat_boy2() {
return new Cat();
}
3.测试获取cat_boy2
package com.sun.springboot;
import com.sun.springboot.bean.Cat;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/**
* @author 孙显圣
* @version 1.0
*/
//表示是一个springboot项目
@SpringBootApplication(scanBasePackages = {"com.sun"}) //扫描指定的包,参数是数组类型的
public class MainApp {
public static void main(String[] args) {
//启动springboot
ConfigurableApplicationContext ioc = SpringApplication.run(MainApp.class, args);
//获取cat_boy2
Cat cat_boy2 = ioc.getBean("cat_boy2", Cat.class);
System.out.println(cat_boy2);
}
}
4.结果展示
4.条件装配注解可以放在类型上
4.@ImportResource 原生配置文件引入
1.基本介绍
2.应用实例
1.需求分析
2.新建一个 BeanConfig02.java 用于测试
package com.sun.springboot.config1;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
/**
* @author 孙显圣
* @version 1.0
*/
@Configuration
//通过类路径来读取beans.xml文件,引入bean到这个配置类,然后就可以通过SpringBoot的ioc来获取bean
//注意这个注解的属性可以是value也可以是locations,而value属性是可以不写属性名的
@ImportResource(locations = "classpath:beans.xml")
public class BeanConfig02 {
}
3.类路径下编写一个beans.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 class="com.sun.springboot.bean.Monster" id="monsterByImport">
<property name="id" value="1"/>
<property name="skill" value="1"/>
<property name="name" value="1"/>
<property name="age" value="1"/>
</bean>
</beans>
4.MainApp.java 使用SpringBoot的ioc来获取bean
package com.sun.springboot;
import com.sun.springboot.bean.Cat;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/**
* @author 孙显圣
* @version 1.0
*/
//表示是一个springboot项目
@SpringBootApplication(scanBasePackages = {"com.sun"}) //扫描指定的包,参数是数组类型的
public class MainApp {
public static void main(String[] args) {
//启动springboot
ConfigurableApplicationContext ioc = SpringApplication.run(MainApp.class, args);
//获取从beans.xml中导入到SpringBoot中的ioc容器中
Object bean = ioc.getBean("monsterByImport");
System.out.println(bean);
}
}