- 添加在类的声明之前,表示这个类是一个“配置类”
- 如果在AnnotationConfigApplicationContext的构造方法参数中直接使用某个类,则这个类可以不添加@Configuration注解
- 当然,即使添加了也不会出错
- 虽然可以不添加,但是,只要类是用于配置的,应该添加
- 在方法的声明之前,表示该方法是用于创建对象的
- @Bean方法定义在配置类中
- @Bean方法会在加载配置类时,由框架自动调用
- @Bean方法的访问权限一般是public
- 其实,你可以使用任何权限,因为Spring是通过反射来调用的
- 从设计规范来看,应该使用public
- @Bean方法的名称将用于生成Bean id
- 如果需要其它名称,可通过@Bean注解的属性进行配置
- @Bean方法可以没有参数,也可以添加任何由Spring管理的对象作为参数
案例:
1.创建DemoBean
package cn.tedu.configbean;
public class DemoBean {
@Override
public String toString() {
return "DemoBean{}";
}
}
2.创建配置类
package cn.tedu.configbean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConfigDemo {
@Bean
public DemoBean demoBean(){
return new DemoBean();
}
@Bean("myBean")
public DemoBean demoBean2(){
return new DemoBean();
}
}
3.创建测试案例
package cn.tedu.configbean;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
@SpringBootTest
public class ConfigBeanApplicationTests {
//ApplicationContext 就是spring的JavaBean容器
@Autowired
ApplicationContext applicationContext;
@Test
void contextLoads(){
//从spring容器中获取spring创建的bean对象
DemoBean demoBean = applicationContext.getBean(DemoBean.class);
System.out.println(demoBean);
}
@Test
void beanTest(){
//getBeanNameForType 根据类型,获取全部
//Bean ID(Bean name)
String[] names = applicationContext.getBeanNamesForType(DemoBean.class);
for (String id : names){
System.out.println(id);
}
}
@Test
void beanID(){
//DemoBean demoBean = applicationContext.getBean(DemoBean.class);
DemoBean demoBean = applicationContext.getBean("myBean", DemoBean.class);
System.out.println(demoBean);
}
}
Bean的作用域
Bean作用域:默认
默认的作用域是单例的
Bean作用域:prototype作用域"prototype
每次引用到bean时都会创建新的实例
常用Spring作用域 组件扫描 组件扫描 -- 隐式配置 组件名称 关于组件扫描注解补充
案例:
1.创建HelloService接口
package cn.tedu.app.service;
public interface HelloService {
String say();
}
2.创建实现类TypicalHelloService
package cn.tedu.app.service;
public class TypicalHelloService implements HelloService{
@Override
public String say() {
return "我是TypicalHelloService";
}
}
3.创建实现类MyHelloService
package cn.tedu.app.service;
public class MyHelloService implements HelloService{
@Override
public String say() {
return "我是MyHelloService";
}
}
4.创建配置类
package cn.tedu.configbean;
import cn.tedu.app.service.HelloService;
import cn.tedu.app.service.TypicalHelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
@ComponentScan("cn.tedu.app.service")
@ConditionalOnClass(HelloService.class)
public class ConfigDemo {
@Bean
@ConditionalOnMissingBean(HelloService.class)
public HelloService helloService(){
return new TypicalHelloService();
}
}
创建测试类
@Test
void helloService(){
HelloService helloService = applicationContext.getBean(HelloService.class);
System.out.println(helloService.getClass().getName());
helloService.say();
}
结果
cn.tedu.app.service.TypicalHelloService 我是TypicalHelloService生命周期注解@PostConstruct和@PreDestroy @PostConstruct和@PreDestroy 关于@PostConstruct和@PreDestroy @PostConstruct 使用@Bean的生命周期方法



