• spring 注入bean的几种方式(J2EE)


    • 好久没写博客了,最近决定重新拾起来,这些天面试了很多 ,借机也了解恢复了很多状态hh

    通过xml进行注册:

    • 一开始接触spring的时候经常会使用xml一个个去进行注册
      类似如下
    <bean id="bean" class="beandemo.Bean" />
    
    • 但是一个个太麻烦进行注册太麻烦了有时候就会使用xml扫描进行注册
    <context:component-scan base-package="com.company.beandemo"/>
    

    通过构造函数方式进行注册(不常用,xml的一种)

    • 如下,写一个带有构造函数的类
    public class PersonBizImpl implements PersonBiz {
    
      // 声明"依赖对象"PersonDAO
      PersonDAO personDao = null;
    
      // 声明"依赖的基本数据类型"
      String str = null;
    
      // 生成无参构造方法
      public PersonBizImpl() {
          super();
      }
    
      // 生成带参构造方法
      public PersonBizImpl(PersonDAO personDao, String str) {
        super();
        this.personDao = personDao;
        this.str = str;
      }
    
       public void addPerson() {
         this.personDao.addPerson();
         System.out.println(str);
      }
    }
    
    • 然后通过在xml文件里面进行注册

    · index是索引,指定注入的属性,从0开始,如:0代表personDao,1代表str属性;

    · type是指该属性所对应的类型,如Persondao对应的是com.aptech.dao.PersonDAO;

    · ref 是指引用的依赖对象;

    · value 当注入的不是依赖对象,而是基本数据类型时,就用value;

        <!-- 利用构造器配置依赖注入 -->
    
        <bean id="personDao" class="com.aptech.dao.impl.PersonDAOImpl"></bean>
    
        <bean id="personBiz" class="com.aptech.biz.impl.PersonBizImpl">
    
        <constructor-arg index="0" type="com.aptech.dao.PersonDAO"ref="personDao"></constructor-arg>
    
        <constructor-arg index="1" value="Spring学习"></constructor-arg>
    
        </bean>
    

    通过setter进行注入(不常用,xml的一种)

    • 首先要配置被注入的bean,在该bean对应的类中,应该有要注入的对象属性或者基本数据类型的属性。例如:为UserBiz类注入UserDAO,同时为UserBiz注入基本数据类型String,那么这时,就要为UserDAO对象和String类型设置setter方法.,用于进行依赖注入。

    如何配置该bean呢?

    <bean id="userBiz" class="com.text.biz.impl.UserBizImpl">
    
    <property name="userDao">
    
    <ref>userDao</ref>
    
    </property>
    
    </bean>
    

    以上就是一个使用属性的setter方法的方式进行依赖注入。

    通过注解方式进行注入

    • 一般情况下,注入Bean有一个最直白,最易懂的方式去实现注入,代码如下。
    • Bean类
      public class MyBean{	}
    
    • Configuration类
       //创建一个class配置文件	
        @Configuration	
        public class MyConfiguration{		
          //将一个Bean交由Spring进行管理       
          @Bean       
           public MyBean myBean(){           
                return new MyBean();        
          }
      }
    
    • Test类

    • 与xml有一点不同,这里在Test中,实例化的不再是ClassPathXmlApplicationContext,而是获取的AnnotationConfigApplicationContext实例。

      ApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);	
      MyBean myBean = cotext.getBean("myBean",MyBean.class);	
      System.out.println("myBean = " + myBean);
    

    • 上面的代码中MyBean也就是我们需要Spring去管理的一个Bean,他只是一个简单的类。
      而MyConfiguration中,我们首先用@Configuration注解去标记了该类,这样标明该类是一个Spring的一个配置类,在加载配置的时候会去加载他。
      在MyConfiguration中我们可以看到有一个方法返回的是一个MyBean的实例,并且该方法上标注着@Bean的注解,表明这是一个注入Bean的方法,会将下面的返回的Bean注入IOC容器。
  • 相关阅读:
    醒醒吧少年,只用Cucumber不能帮助你BDD
    【Python3 爬虫】06_robots.txt查看网站爬取限制情况
    【Python3 爬虫】05_安装Scrapy
    【基尼系数】基尼系数的计算方法与计算案例
    【Python3 爬虫】04_urllib.request.urlretrieve
    【Python3 爬虫】03_urllib.error异常处理
    【Python3 爬虫】02_利用urllib.urlopen向百度翻译发送数据并返回结果
    【Python】help与dir的用法
    在线激活Pycharm(亲测有效)
    【Python3 爬虫】01_简单页面抓取
  • 原文地址:https://www.cnblogs.com/athony/p/16008789.html
Copyright © 2020-2023  润新知