Spring注入及装配

Spring装配:创建应用对象之间协作关系的行为

1.隐式自动装配:@component 组件

    @componentScan 扫描组件

   @Autowired自动装配---》具有多项匹配时会有歧义性需要消除

当bean中需要参数传值时,不能使用自动装配
2.显示Java装配:使用一个javaconfig类,在类中声明方法返回需要的组件对象 使用@Bean 
  Bean和component都会为Spring容器加载一个单例bean实例
  以@component注解的bean需要在javaconfig中开启componentScan扫描才可获得
  Bean声明方法调用参数时若对应参数也是通过Bean方法获得,则会从Spring容器中获得

  例: 

@Bean

public A getA(){
return new A();
}
@Bean B getB(){
return new B(getA()); //只有一份A在Spring容器中
}

  注意:Bean和component同时装配某一个类的话,容器中会有2份实例

3.显示XML装配:①<constructor-arg> ②c-命名空间  

Spring自动装配歧义处理: 
1.@Primary 设置首选
例 

   @Component
  @Primary
  public class ABean implements aa {...}

  @Bean
  @Primary
  public ABean getaBean(){return new ABean();}
2.限定符

  @Qualifier
@Autowired
@Qualifier{"aBean"}
public void setbean(aa bean){......}
为@Qualifier注解所设置的参数就是想要注入的bean的ID。
所有使用@Component注解声明的类都会创建为bean,并且bean的ID为首字母变为小写的类名。
3.自定义限定符@Qualifier 
在bean声明上添加@Qualifier注解,设置其bean ID
@Component
@Qualifier{"cold"}
public class ABean implements aa {...}

@Bean
@Qualifier{"cold"}
public ABean getaBean(){return new ABean();}
4.自定义创建限定符注解,可创建多个,然后为需要的Bean添加多个自定义限定符注解,以达到缩小定位的作用
@Target({ElementType.CONSTRUCTOR,ElementType.FIELD,ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Cold{}
例:
@Component
@Cold
public class ABean implements aa {...}

@Bean
@Cold
public ABean getaBean(){return new ABean();}

————————————————————————————————

例:具有多接口实现,采用@component组件注解,使用@Qualifier注解定位注入

public interface MyService {

	String saveAA();
	void sayHello(String name);
}

@Configuration
@ComponentScan
public class javaConfig {
    //empty
}

@Component
public class MyServiceImpl implements MyService{

	@Override
	public String saveAA() {
		return "MyServiceImpl -> AA";
	}

	@Override
	public void sayHello(String name) {
		System.out.println(name+" sayHello! from MyServiceImpl");
	}

}

@Component
public class MyServiceImpl2 implements MyService{

	@Override
	public String saveAA() {
		return "MyServiceImpl2 -> AA";
	}

	@Override
	public void sayHello(String name) {
		System.out.println(name+" sayHello! from MyServiceImpl2");
	}
}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=javaConfig.class)
public class testRMI {
	
	@Autowired
	@Qualifier("myServiceImpl2")
	MyService theService;
	
	@Test
	public void test1(){
		theService.sayHello("John");
	}
}
/* output:
    John sayHello! from MyServiceImpl2
*/

————————————————————————————

例:可以改为使用javaConfig配置Bean

@Configuration
@ComponentScan
public class javaConfig {

  	@Bean
 	public MyServiceImpl myServiceImpl(){
 		return new MyServiceImpl();
 	}
 	@Bean	 //可以给Bean起别名 @Bean("M1")
 	@Primary //设置首选
 	public MyServiceImpl2 myServiceImpl2(){
 		return new MyServiceImpl2();
 	}
}

———————————————————————————

Spring运行时注入:
属性占位符( Property placeholder)。
Spring表达式语言( SpEL)。
1.使用@PropertySource注解和Environment(显示javaConfig类装配方式)
例:
@Configuration
@PropertySource("classpath:/com/soundsystem/app.properties")//声明属性源
public class ExpressiveConfig{
@Autowired
Environment env;
@Bean
public BlankDisc disc(){
return new BlankDisc(
env.getProperty("disc.title"),
env.getProperty("disc.artise")//检索属性值
);
}
}
2.使用@Value注解(隐式自动装配方式)
例:
@Component
public class BlankDisc{
public BlankDisc(@Value("${disc.title}") String title,
@Value("${disc.artise}") String artise){...}
}
为了使用占位符,须要配置一个PropertyPlaceholderConfigurer bean或PropertySourcesPlaceholderConfigurer bean。
推荐PropertySourcesPlaceholderConfigurer
在javaConfig类中配置这个Bean
@Bean
public static PropertySourcesPlaceholderConfigurer placeholderconf{
return new PropertySourcesPlaceholderConfigurer();
}

——————————————————————————————

@Component
public class DefaultStr {
	//@Value("#{'Barry'.toUpperCase()}")
	private String Str=null;
	public DefaultStr(@Value("#{'Barry'.toUpperCase()}") String Str){
		this.Str=Str;
	}
	public void sayHello( ){
		 System.out.println(Str+",I print a message ! " );
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值