spring容器内的bean交给ApplicationContext管理,实现依赖管理和自动注入

spring容器内的bean交给ApplicationContext管理,实现依赖管理和自动注入

将bean交由spring容器来管理有三种种方式:
1.在java中进行显示配置
2.隐式的bean发现机制和自动装配
3.在XML中进行显示配置

第一种方式(在java中进行显示配置):

//接口
package com.zzg.chapter2.October_twelve.ExplicitAutoAssembly_bean;

public interface ICDPlayer {
    void play();
}

//实现类
package com.zzg.chapter2.October_twelve.ExplicitAutoAssembly_bean;

import org.springframework.stereotype.Component;

@Component
public class CyxCdPlayer implements ICDPlayer {
    @Override
    public void play() {
        System.out.println("陈奕迅的cd");
    }
}
//配置类
package com.zzg.chapter2.October_twelve.ExplicitAutoAssembly_bean;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CDConfig {

    @Bean(name = "CyxCd")
    public CyxCdPlayer getCdPlayer(){
        return new CyxCdPlayer();
    }

}
//测试类
package com.zzg.chapter2.October_twelve.ExplicitAutoAssembly_bean;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = CDConfig.class)
public class CdTest {

    @Autowired
    private ICDPlayer icdPlayer;

    @Test
    public void test1(){
        ApplicationContext context = new AnnotationConfigApplicationContext(CDConfig.class);
        CyxCdPlayer bean = (CyxCdPlayer) context.getBean("CyxCd");
        bean.play();
    }

    @Test
    public void test2(){
        icdPlayer.play();
    }

}

第二种方式(隐式的bean发现机制和自动装配)

//接口和实现类同第一种方法
//配置类
package com.zzg.chapter2.October_twelve.ImplicitAutoAssembly_bean;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class CDConfig {
}
//测试类同第一种方法

第三种方式(在XML中进行显示配置):

//接口类同第一种方法
//demo类(作为实现类的参数)
package com.zzg.chapter2.October_twelve.XmlAutoAssembly_bean;

import org.springframework.stereotype.Component;


@Component
public class Demo {
    private String str;
    private Integer integer;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    public Integer getInteger() {
        return integer;
    }

    public void setInteger(Integer integer) {
        this.integer = integer;
    }
}


//实现类
package com.zzg.chapter2.October_twelve.XmlAutoAssembly_bean;

import org.springframework.stereotype.Component;

@Component
public class CyxCdPlayer implements ICDPlayer {


    private Demo demo;
    private String ss;

	//最好保留空参构造
    public CyxCdPlayer(){}

    public CyxCdPlayer(Demo demo,String ss){
        this.demo = demo;
        this.ss = ss;
    }

    @Override
    public void play() {

        System.out.println("demo的str:" + demo.getStr() + "===========" + "demo的integer:" + demo.getInteger() + "cdplay的string:" + ss);
    }
}
//配置文件(resource下)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">



    <context:component-scan base-package="com.zzg.chapter2.October_twelve.XmlAutoAssembly_bean"/>

    <!-- id为测试类内引入demo对象的名称,最好创建一个,否则系统会自动创建,不方便对demo的使用;id为创建的单例Demo的对象名称,可以创建多个不同id的同一个单例Demo对象 -->
    <bean id="demo" class="com.zzg.chapter2.October_twelve.XmlAutoAssembly_bean.Demo">
        <!-- property配置Demo的属性,name为参数名称,前提是Demo类必须提供该属性的setter方法 -->
        <property name="str" value="justExe1"/>
        <property name="integer" value="1122334"/>
    </bean>
  
    <bean id="icdPlayer" class="com.zzg.chapter2.October_twelve.XmlAutoAssembly_bean.CyxCdPlayer">
    	<!-- constructor-arg配置CyxCdPlayer的有参构造,用于传入参数,现已支持name属性的配置  -->
        <constructor-arg name="demo" ref="demo"/>
        <constructor-arg name="ss" value="ss_string"/>
    </bean>
</beans>

//测试类
package com.zzg.chapter2.October_twelve.XmlAutoAssembly_bean;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration(locations = "classpath:spring-config.xml")
public class CdTest {

   @Autowired
   private Demo demo;

   @Autowired
   private ICDPlayer icdPlayer;

    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        Demo bean = (Demo)context.getBean("demo");
        System.out.println(bean.getStr());

    }

    @Test
    public void test2(){
        System.out.println(demo.getStr());
    }

    @Test
    public void test3(){
        icdPlayer.play();
    }


}

以上三种方法都可以运行.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值