一、Bean是什么

1、Java面向对象,对象有方法和属性,那么就需要对象实例来调用方法和属性(即实例化);

2、凡是有方法或属性的类都需要实例化,这样才能具象化去使用这些方法和属性;

3、规律:凡是子类及带有方法或属性的类都要加上注册Bean到Spring IoC的注解;

4、把Bean理解为类的代理或代言人(实际上确实是通过反射、代理来实现的),这样它就能代表类拥有该拥有的东西了

5、我们都在微博上@过某某,对方会优先看到这条信息,并给你反馈,那么在Spring中,你标识一个@符号,那么Spring就会来看看,并且从这里拿到一个Bean或者给出一个Bean

二、注解分为两类:

1、一类是使用Bean,即是把已经在xml文件中配置好的Bean拿来用,完成属性、方法的组装;比如@Autowired , @Resource,可以通过byTYPE(@Autowired)、byNAME(@Resource)的方式获取Bean;

2、一类是注册Bean,@Component , @Repository , @ Controller , @Service , @Configration这些注解都是把你要实例化的对象转化成一个Bean,放在IoC容器中,等你要用的时候,它会和上面的@Autowired , @Resource配合到一起,把对象、属性、方法完美组装。

三、@Bean是啥?

1、原理是什么?先看下源码中的部分内容:

 1 Indicates that a method produces a bean to be managed by the Spring container.
 2  
 3  <h3>Overview</h3>
 4  
 5  <p>The names and semantics of the attributes to this annotation are intentionally
 6  similar to those of the {@code <bean/>} element in the Spring XML schema. For
 7  example:
 8  
 9  <pre class="code">
10      @Bean
11      public MyBean myBean() {
12          // instantiate and configure MyBean obj
13          return obj;
14     }</pre>

意思是@Bean明确地指示了一种方法,什么方法呢——产生一个bean的方法,并且交给Spring容器管理;从这我们就明白了为啥@Bean是放在方法的注释上了,因为它很明确地告诉被注释的方法,你给我产生一个Bean,然后交给Spring容器,剩下的你就别管了

 2、记住,@Bean就放在方法上,就是产生一个Bean,那你是不是又糊涂了,因为已经在你定义的类上加了@Configration等注册Bean的注解了,为啥还要用@Bean呢?这个我也不知道,下面我给个例子,一起探讨一下吧:

 1 package com.edu.fruit;
 2   //定义一个接口
 3     public interface Fruit<T>{
 4         //没有方法
 5 }
 6  
 7 /*
 8 *定义两个子类
 9 */
10 package com.edu.fruit;
11      @Configuration
12      public class Apple implements Fruit<Integer>{//将Apple类约束为Integer类型
13  
14 }
15  
16 package com.edu.fruit;
17      @Configuration
18      public class GinSeng implements Fruit<String>{//将GinSeng 类约束为String类型
19  
20 }
21 /*
22 *业务逻辑类
23 */
24 package com.edu.service;
25        @Configuration
26        public class FruitService {
27           @Autowired
28           private Apple apple;
29           @Autowired
30           private GinSeng ginseng;
31     //定义一个产生Bean的方法
32        @Bean(name="getApple")
33        public Fruit<?> getApple(){
34        System.out.println(apple.getClass().getName().hashCode);
35          System.out.println(ginseng.getClass().getName().hashCode);
36        return new Apple();
37 }
38 }
39 /*
40 *测试类
41 */
42 @RunWith(BlockJUnit4ClassRunner.class)
43 public class Config {
44     public Config(){
45         super("classpath:spring-fruit.xml");
46     }
47     @Test
48     public void test(){
49         super.getBean("getApple");//这个Bean从哪来,从上面的@Bean下面的方法中来,返回
50                                                           的是一个Apple类实例对象
51          
52     }
53 }

总结:

1、凡是子类及带属性、方法的类都注册Bean到Spring中,交给它管理;

2、@Bean 用在方法上,告诉Spring容器,你可以从下面这个方法中拿到一个Bean

Spring帮助我们管理Bean分为两个部分,一个是注册Bean,一个装配Bean。完成这两个动作有三种方式,一种是使用自动配置的方式、一种是使用JavaConfig的方式,一种就是使用XML配置的方式。在自动配置的方式中,使用@Component去告诉Spring,我是一个bean,你要来管理我,然后使用@AutoWired注解去装配Bean(所谓装配,就是管理对象直接的协作关系)。然后在JavaConfig中,@Configuration其实就是告诉spring,spring容器要怎么配置(怎么去注册bean,怎么去处理bean之间的关系(装配))。那么就很好理解了,@Bean的意思就是,我要获取这个bean的时候,你spring要按照这种方式去帮我获取到这个bean。到了使用xml的方式,也是如此。君不见<bean>标签就是告诉spring怎么获取这个bean,各种<ref>就是手动的配置bean之间的关系。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐