专业的高端企业网站,免费行情网站推荐,网站专题页面用什么做,帮人做彩票网站有事吗文章目录 1. 什么是Bean#xff0c;如何配置2. 如何配置bean2.1 使用注解Bean2.2 使用注解Import 1. 什么是Bean#xff0c;如何配置
被spring容器所管理的对象被称为bean#xff0c;管理方式可以有纯xml文件方式、注解方式进行管理(比如注解Component)。 在Spring Boot中如何配置2. 如何配置bean2.1 使用注解Bean2.2 使用注解Import 1. 什么是Bean如何配置
被spring容器所管理的对象被称为bean管理方式可以有纯xml文件方式、注解方式进行管理(比如注解Component)。 在Spring Boot中在注解 Component 的基础上衍生出注解 Service(专门用于处理业务类的注解)、Repository(专门用于处理数据访问的注解)。
2. 如何配置bean
2.1 使用注解Bean
在一个配置类上定义一个方法返回值为一个对象的实例化在这个方法上添加注解Bean如下
package com.lize.demo.dao;public class UserDao {public void printUserDao(){System.out.println(UserDao);}
}package com.lize.demo.config;import com.lize.demo.dao.UserDao;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class SpringConnfig {Beanpublic UserDao getUserDao(){return new UserDao();}
}单元测试类如下
package com.lize.demo;import com.lize.demo.dao.UserDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;SpringBootTest
class DemoApplicationTests {Autowiredprivate UserDao ud;Testvoid contextLoads() {ud.printUserDao();}
}通过Bean这种方式定义Bean相比于直接在类上添加注解component定义Bean。前者返回的是一个实例化对象可以在这个过程中设置一些参数初始化Bean。
2.2 使用注解Import
需要写在类上标记的类必须是一个bean否则不会起作用
Component
Import(UserDao.class)
public class SpringConnfig {}上述只是基础写法。另外可以实现ImportSelector这个接口并重写其方法selectImports这个方法返回的是一个字符串数组字符串数组的值为类的完整路径进行批量注入Bean如下
package com.lize.demo.config;import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;public class MyImportSelector implements ImportSelector {Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {return new String[]{com.lize.demo.dao.UserDao};// 返回字符串数组}
}Component
Import(MyImportSelector.class)
public class SpringConnfig {}还有一种写法为实现ImportBeanDefinitionRegistrar这个接口并重写其方法registerBeanDefinitions如下
package com.lize.demo.config;import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;public class MyImportBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {Overridepublic void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {RootBeanDefinition definition new RootBeanDefinition();definition.setBeanClassName(com.lize.demo.dao.UserDao);registry.registerBeanDefinition(UserDao,definition);}
}
package com.lize.demo.config;import com.lize.demo.dao.UserDao;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;Component
Import(MyImportBeanDefinitionRegister.class)
public class SpringConnfig {}