温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring中如何区分每一个Bean

发布时间:2021-06-25 14:16:15 来源:亿速云 阅读:174 作者:chen 栏目:开发技术

这篇文章主要讲解了“Spring中如何区分每一个Bean”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring中如何区分每一个Bean”吧!

万事万物都有名字,一个人可能有很多名字,比如朱元璋,可以使用朱重八来区分。对于bean来说也是一样。本文主要探究,spring中是如何区分每一个bean的。主要是方式,其核心原理主要在bean的生命周期中去管理。

主要是通过以下三种:

1、XML中的name或者是id属性

第一步:创建User类

public class User {     private String name;     public User(String name) {         this.name = name;     }     public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     }     public void hello() {         System.out.println("name:" +"hello world");     } }

User是作为一个bean,里面定义了一个hello的方法。

第二步:创建spring.xml文件,在xml文件中配置bean

<?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="user" class="com.example.demo.beam.User">         <constructor-arg value="愚公要移山"/>     </bean> </beans>

我在resource目录下面新建了一个META-INF目录用于存放spring.xml文件。这个文件中使用了id来区分不同的bean。

第三步:验证

public class Main {     public static void main(String[] args) {         String xmlPath = "META-INF/spring.xml";         ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);         //从spring容器获得         User user = (User) applicationContext.getBean("user");         user.hello();     } }

在控制台就可以看到结果了。图片

2、通过注解的方式

声明Bean的注解:

(1)@Component组件,没有明确的角色。

(2)@Service在业务逻辑层(Service层)使用。

(3)@Repository在数据访问层(dao层)使用。

(4)@Controller在展现层使用。

它们默认没有直接指定bean的name,所以bean的name是spring自动生成的。bean的name生成规则如下:

(1)class的simpleName如果大于1个字符且第二个字符是大写字母,则simpleName就是bean name,

(2)如果class的simpleName是1个字符或者第2个字符是小写,则将首字母转为小写的字符串作为bean name,

举例 "FooBah"的bean名称变成了"fooBah","X" 变成了 "x","URL" 依然是"URL"。下面通过实例演示一波:

注意:若不同的包下有两个名字相同的类,而这两个类都声明成spring的bean,这时候就会产成冲突。因为bean的名字就是bean的唯一标示,是不允许重复的。

第一步:创建UserService

public class UserService {     public void hello() {         System.out.println("hello world");     } }

第二步:创建config

@Configuration @ComponentScan("com.example.demo.beam") public class JavaConfig { }

第三步:验证

public class Main {     public static void main(String[] args) {         AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);         UserService teacherService = (UserService) context.getBean("userService");         teacherService.hello();         context.close();     } }

在验证的时候可以看到,获取bean的时候输入的名字是userService。看结果

Spring中如何区分每一个Bean

3、Java配置

@Bean 注解声明的方法名是放入spring容器的bean的name。

Java配置是通过@Configuration和@Bean来实现的。

@Configuration声明当前类是一个配置类,相当于一个Spring配置的xml文件。

@Bean注解在方法上,声明当前方法的返回值为一个Bean。

案例验证:

第一步:改变JavaConfig

@Configuration @ComponentScan("com.example.demo.beam") public class JavaConfig {     @Bean     public UserService getStudentService() {         UserService userService = new UserService();         return userService;     } }

此时的bean注解到了方法上,根据上面的定义,此时的bean是返回类型UserService,因此返回的结果也是这。

第二步:基于上面的进行测试

public class Main {     public static void main(String[] args) {         AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);         UserService teacherService = (UserService) context.getBean("userService");         teacherService.hello();         context.close();     } }

在Main测试环境下,我们依然使用userService进行bean的获取和测试。

Spring中如何区分每一个Bean

结果依然如此。其他的方式待补充。

感谢各位的阅读,以上就是“Spring中如何区分每一个Bean”的内容了,经过本文的学习后,相信大家对Spring中如何区分每一个Bean这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI