Spring4.0学习笔记 第二章 Javaconfig 显式装配(中)

构造器注入

目录结构:

CDConfig:

package config;


import interfaces.ConstuctBean;
import interfaces.DiskImpl;
import interfaces.DiskInterface;


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


@Configuration   //表示配置文件
public class CDConfig {
	
	@Bean //该注解等效xml中的<bean>标签配置,spring容器会将所有带@Bean的方法生成实例存放于容器之中
	//生成的bean ID名 默认为方法名, 该bean作为构造器注入下面方法
	public ConstuctBean getCbean(){
		return new ConstuctBean();
		
	}


	
	@Bean //该注解等效xml中的<bean>标签配置,生成的bean名 默认为方法名(首字母小写)
	public  DiskInterface getDisk(){
		//getCbean()上不加@Bean注释 我们也是能正常的获取ConstuctBean的实例的,只不过每次获取的都是新的对象,
		//而加了@Bean注解,,Spring会拦截所有对它的调用,并确保直接返回该方法所创建的bean,而不是每次都对其进行实际的调用
		//也就是不是每次都生成实例,spring默认生成实例策略为单例模式
		return new DiskImpl(getCbean());
		
	}
	
}


ConstutBean:(作为构造器参数)

package interfaces;

public class ConstuctBean {
	
	public void con(){
		System.out.println("我是构造器注入bean");
	}

}
DiskImpl(DiskInterface接口的实现):

package interfaces;


public class DiskImpl implements DiskInterface {
	
	private ConstuctBean cb;
	

	public DiskImpl(ConstuctBean cb){
		this.cb=cb;
	}

	public void play() {
		cb.con();
		//输出对象句柄
		System.out.println(cb);
	}

}

调用测试代码:

CDPlayer:

package interfaces;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import config.CDConfig;


public class CDPlayer {
	
		public static void main(String[] args) {
			//JavaConfig上下文,加载配置文件
			AnnotationConfigApplicationContext ac = 
					new AnnotationConfigApplicationContext(CDConfig.class);
			DiskImpl ip=(DiskImpl) ac.getBean("getDisk");
			ip.play();
			
		}

}

输出:我是构造器注入bean
interfaces.ConstuctBean@56dacb7


现在将Config文件拆分成两个:

分别为CDConfig 和CDConfig1


CDConfig:

package config;


import interfaces.ConstuctBean;
import interfaces.DiskImpl;
import interfaces.DiskInterface;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Import(CDConfig1.class)//导入CDConfig1配置文件合成一个
@Configuration   //表示配置文件
public class CDConfig {
	@Autowired //自动注入
	private ConstuctBean cb;


	
	@Bean //该注解等效xml中的<bean>标签配置,生成的bean名 默认为方法名(首字母小写)
	public  DiskInterface getDisk(){
		//getCbean()上不加@Bean注释 我们也是能正常的获取ConstuctBean的实例的,只不过每次获取的都是新的对象,
		//而加了@Bean注解,,Spring会拦截所有对它的调用,并确保直接返回该方法所创建的bean,而不是每次都对其进行实际的调用
		//也就是不是每次都生成实例,spring默认生成实例策略为单例模式
		return new DiskImpl(cb);
	 //还有一种方式:
	  //将 @Autowired 
	  // private ConstuctBean cb; 
       // 这两行去掉,然后在public  DiskInterface getDisk()中加入参数变成public  DiskInterface getDisk(ConstuctBean cb),
		//这样虽然ConstuctBean实例在CDConfig1文件中,CDConfig1中生成的ConstuctBean实例会注入给参数cb




		
	}
	
}
CDConfig1:

package config;

import interfaces.ConstuctBean;

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

@Configuration
public class CDConfig1 {
	
	@Bean //该注解等效xml中的<bean>标签配置,spring容器会将所有带@Bean的方法生成实例存放于容器之中生成的bean ID名 默认为方法名,
//     该注解必须有,否则的话autowired自动注入发现容器中没有生成的ConstuctBean 实例,会发生注入错误
// 注入的前提是 已生成该bean 实例,才能正确注入
	public ConstuctBean getCbean(){
		return new ConstuctBean();
		
	}

}


package interfaces;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import config.CDConfig;


public class CDPlayer {
	
		public static void main(String[] args) {
			//JavaConfig上下文,加载配置文件
			AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(CDConfig.class);
			DiskImpl ip=(DiskImpl) ac.getBean("getDisk");
			ip.play();
			
		}

}

输出:我是构造器注入bean
interfaces.ConstuctBean@667cc058


JavaConfig 和XML配置混用


TotalConfig:

package soundsystem;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;

@Configuration
@Import({CDConfig.class})//引入另一个javaconifg配置文件
//引入xml配置文件
@ImportResource("classpath:application.xml")
public class TotalConfig {
	

}
CDConfig:

package soundsystem;

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

@Configuration
public class CDConfig {
	@Bean
	public DiskImpl getDk(ConstBean cb){
		
		return new DiskImpl(cb);
		
	}

}
ConstBean:

package soundsystem;

public class ConstBean {
	
	public void con(){
		System.out.println("我是构造器参数bean");
	}

}
xml:

<?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:c="http://www.springframework.org/schema/c"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd 
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util.xsd">
 	
 	<bean  class="soundsystem.ConstBean">
 	
 	</bean>

 </beans>

Test:

package soundsystem;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	
		public static void main(String[] args) {
			AnnotationConfigApplicationContext ac =
					new AnnotationConfigApplicationContext(TotalConfig.class);
			ac.getBean("getDk", DiskImpl.class).play();
//			ClassPathXmlApplicationContext ac = 
//					new ClassPathXmlApplicationContext("application.xml");
//			ac.getBean("getDk", DiskImpl.class).play();
			
		}

}
输出:
sing song
我是构造器参数bean



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值