spring实战-注解装配bean


    

spring实战-注解装配bean

标签: Javajava webspring
138人阅读 评论(0) 收藏 举报
分类:

spring提供的基于xml的bean装配并不受所有人的欢迎,实际上很多开发人员排斥太多的xml配置,spring还提供了基于注解的bean申明和装配,事实上该种方式也是目前最普遍受欢迎的方式

spring-beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.     http://www.springframework.org/schema/context  
  8.     http://www.springframework.org/schema/context/spring-context.xsd  
  9.     http://www.springframework.org/schema/util  
  10.     http://www.springframework.org/schema/util/spring-util.xsd">  
  11.       
  12.     <!-- context:component-scan会扫描制定的包及其子包,并查找出能够自动注册为bean的类 -->  
  13.     <!-- 它能够识别的注解包括 @Component通用注解, @Controller控制器注解, @Service服务层注解, @Respository数据层注解 -->  
  14.     <!-- 可以通过<context:exclude-filter>和<context:include-filter>来调整扫描行为 -->  
  15.     <!-- filter中的type常用有annotation,assingable,regex。 -->  
  16.     <!-- annotation表示过滤expression中指定注解的类, -->  
  17.     <!-- assingable表示过滤expression中指定类的派生类, -->  
  18.     <!-- regex表示指定过滤expression中指定正则表达式匹配的类名的类 -->  
  19.     <context:component-scan base-package="com.halfworlders">  
  20.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>  
  21.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>  
  22.     </context:component-scan>  
  23.       
  24. </beans>  
<?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"
	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/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/util
	http://www.springframework.org/schema/util/spring-util.xsd">
	
	<!-- context:component-scan会扫描制定的包及其子包,并查找出能够自动注册为bean的类 -->
	<!-- 它能够识别的注解包括 @Component通用注解, @Controller控制器注解, @Service服务层注解, @Respository数据层注解 -->
	<!-- 可以通过<context:exclude-filter>和<context:include-filter>来调整扫描行为 -->
	<!-- filter中的type常用有annotation,assingable,regex。 -->
	<!-- annotation表示过滤expression中指定注解的类, -->
	<!-- assingable表示过滤expression中指定类的派生类, -->
	<!-- regex表示指定过滤expression中指定正则表达式匹配的类名的类 -->
	<context:component-scan base-package="com.halfworlders">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
	</context:component-scan>
	
</beans>

TestMain

  1. package com.halfworlders.test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. /** 
  7.  * @Primary 注解 设置首选bean 
  8.  * @Qualifier 注解设置限定符 
  9.  * @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) 注解设置作用域 
  10. */  
  11. class TestMain3 {  
  12.     @SuppressWarnings({ "resource""unused" })  
  13.       
  14.     public static void main(String[] args) {  
  15.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-beans.xml");    
  16.         Object proxy = applicationContext.getBean("proxyInfo");    
  17.         Object connection = applicationContext.getBean("connection");    
  18.         Object service = applicationContext.getBean("service");    
  19.         System.out.println("-----end------");    
  20.     }  
  21. }  
package com.halfworlders.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Primary 注解 设置首选bean
 * @Qualifier 注解设置限定符
 * @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) 注解设置作用域
*/
class TestMain3 {
	@SuppressWarnings({ "resource", "unused" })
	
	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-beans.xml");  
        Object proxy = applicationContext.getBean("proxyInfo");  
        Object connection = applicationContext.getBean("connection");  
        Object service = applicationContext.getBean("service");  
        System.out.println("-----end------");  
	}
}


ProxyInfo

  1. package com.halfworlders.web;  
  2.   
  3. import org.springframework.stereotype.Component;  
  4.   
  5. /* 
  6.  * 当为Component注解指定参数时,该Bean的ID就是参数值 
  7.  * 如果没有指定参数,默认就为类名首字母小写作为ID值 
  8.  */  
  9. @Component("proxyInfo")  
  10. public class ProxyInfo {  
  11.     private String ip;  
  12.     private int port;  
  13.       
  14.     get..set..  
  15. }  
package com.halfworlders.web;

import org.springframework.stereotype.Component;

/*
 * 当为Component注解指定参数时,该Bean的ID就是参数值
 * 如果没有指定参数,默认就为类名首字母小写作为ID值
 */
@Component("proxyInfo")
public class ProxyInfo {
	private String ip;
	private int port;
	
	get..set..
}


Config

  1. package com.halfworlders.web;  
  2.   
  3. import org.springframework.beans.factory.annotation.Value;  
  4. import org.springframework.stereotype.Component;  
  5.   
  6. @Component  
  7. public class Config {  
  8.       
  9.     /* 
  10.      * 可以通过Value装配任何类型的值,包括基本类型等硬编码的值,但是这样做并没有什么意义 
  11.      * 但是Value注解可以使用SpEL表达式 
  12.      * 特别的是,在项目中我们经常通过Value注解将properties文件的配置参数注入到系统中 
  13.      */  
  14.     @Value("5")  
  15.     private int timeOut;  
  16.   
  17.     get...set...  
  18.       
  19. }  
package com.halfworlders.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Config {
	
	/*
	 * 可以通过Value装配任何类型的值,包括基本类型等硬编码的值,但是这样做并没有什么意义
	 * 但是Value注解可以使用SpEL表达式
	 * 特别的是,在项目中我们经常通过Value注解将properties文件的配置参数注入到系统中
	 */
	@Value("5")
	private int timeOut;

	get...set...
	
}

Connection

  1. package com.halfworlders.web;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.beans.factory.annotation.Qualifier;  
  5. import org.springframework.stereotype.Component;  
  6.   
  7. /** 
  8.  * autowired注解注入,必须保证有且只有一个可以匹配的bean,否则会报异常 
  9.  * 但是也都有对应的方法,避免异常:1,可选的自动装配;2,限定歧义性依赖 
  10.  */  
  11. @Component  
  12. public class Connection {  
  13.   
  14.     /* 
  15.      * 基于属性的注解注入,autowired注解的注入不会受限于private关键字 
  16.      * 1,required=false可选的自动装配,如果没有可以匹配的bean,这proxyInfo为null值 
  17.      * 2,Qualifier可以帮助限定歧义性依赖,如果有多个可以匹配的bean,可以通过Qualifier制定bean的名字(ID)来注入 
  18.      */  
  19.     @Autowired(required=false)  
  20.     @Qualifier("proxyInfo")  
  21.     private ProxyInfo proxyInfo;  
  22.       
  23.     private Config config;  
  24.       
  25.     public Connection(){  
  26.     }  
  27.       
  28.   
  29.     /* 
  30.      * 基于属性set函数的注解注入 
  31.      */  
  32.     @Autowired  
  33.     public void setConfig(Config config) {  
  34.         this.config = config;  
  35.     }  
  36.       
  37.     get...set...  
  38.   
  39. }  
package com.halfworlders.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * autowired注解注入,必须保证有且只有一个可以匹配的bean,否则会报异常
 * 但是也都有对应的方法,避免异常:1,可选的自动装配;2,限定歧义性依赖
 */
@Component
public class Connection {

	/*
	 * 基于属性的注解注入,autowired注解的注入不会受限于private关键字
	 * 1,required=false可选的自动装配,如果没有可以匹配的bean,这proxyInfo为null值
	 * 2,Qualifier可以帮助限定歧义性依赖,如果有多个可以匹配的bean,可以通过Qualifier制定bean的名字(ID)来注入
	 */
	@Autowired(required=false)
	@Qualifier("proxyInfo")
	private ProxyInfo proxyInfo;
	
	private Config config;
	
	public Connection(){
	}
	

	/*
	 * 基于属性set函数的注解注入
	 */
	@Autowired
	public void setConfig(Config config) {
		this.config = config;
	}
	
	get...set...

}

Service

  1. package com.halfworlders.web;  
  2.   
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. import org.springframework.beans.factory.annotation.Autowired;  
  7. import org.springframework.stereotype.Component;  
  8.   
  9. @Component  
  10. public class Service {  
  11.     private Connection connection;  
  12.     private List<String> urls;  
  13.     private Map<String, String> response;  
  14.       
  15.     public Service() {  
  16.           
  17.     }  
  18.       
  19.     /* 
  20.      * 基于构造函数的注解注入 
  21.      * 当autowired标注多个构造函数时,系统会选择参数最多的那个构造器 
  22.      */  
  23.     @Autowired  
  24.     public Service(Connection connection) {  
  25.         this.connection = connection;  
  26.     }  
  27.       
  28.     get...set...  
  29. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值