服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 这么多的Bean,Spring是如何区分的?

这么多的Bean,Spring是如何区分的?

2021-05-11 23:41愚公要移山冯冬冬 Java教程

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

这么多的Bean,Spring是如何区分的?

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

主要是通过以下三种:

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

 

第一步:创建User类

  1. public class User { 
  2.     private String name
  3.     public User(String name) { 
  4.         this.name = name
  5.     } 
  6.     public String getName() { 
  7.         return name
  8.     } 
  9.     public void setName(String name) { 
  10.         this.name = name
  11.     } 
  12.     public void hello() { 
  13.         System.out.println("name:" +"hello world"); 
  14.     } 

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

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

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans 
  5.                            http://www.springframework.org/schema/beans/spring-beans.xsd"> 
  6.     
  7.     <bean id="user" class="com.example.demo.beam.User"
  8.         <constructor-arg value="愚公要移山"/> 
  9.     </bean> 
  10. </beans> 

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

第三步:验证

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

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

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

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

第二步:创建config

  1. @Configuration 
  2. @ComponentScan("com.example.demo.beam"
  3. public class JavaConfig { 

第三步:验证

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

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

这么多的Bean,Spring是如何区分的?

3、Java配置

 

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

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

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

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

案例验证:

第一步:改变JavaConfig

  1. @Configuration 
  2. @ComponentScan("com.example.demo.beam"
  3. public class JavaConfig { 
  4.     @Bean 
  5.     public UserService getStudentService() { 
  6.         UserService userService = new UserService(); 
  7.         return userService; 
  8.     } 

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

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

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

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

这么多的Bean,Spring是如何区分的?

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

原文链接:https://mp.weixin.qq.com/s/rSWWd_H4803pRm0qJXTARQ

延伸 · 阅读

精彩推荐