Spring学习——Annotation注解

注解(Annotation)

又称java标注,是一种元数据         GitHub一个annotation无需xml配置的demo
在程序中给    类、方法、变量 添加注解后(相当于标记)反射获取标注内容

Beans.xml配置文件

1、使用<context:annotation-config/> //隐式的向Spring容器注册 4个bean

Bean注解传统声明
AutowiredAnnotationBeanPostProcessor@Autowired<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
CommonAnnotationBeanPostProcessor@PersistenceContext<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
PersistenceAnnotationBeanPostProcessor@Required<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
RequiredAnnotationBeanPostProcessor@Resource、@PostConstruct、@PostConstruct<bean class="org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor"/>

2、使用<context:component-scan base-package=”XX.XX.*.xx”/> //base-package指定扫描的包

     包含了<context:annotation-config/>中的bean
    @Controller(控制器),@Service(服务),@Repository(数据库),@Component(组件)

注解

1、@Autowired 自动使用  autowire="byType" 依赖注入,按类型装配,无需beans.xml中另配置【spring支持】

class B{	
		@Autowired //自动在Bean对象B中通过byType自动注入A
		private A a;
	}

2、@PersistenceContext 持久化容器 【暂时不知道,未使用】

//待学习     ^(0T0)^

3、@Required 用于set方法,要求被注解的元素必须在beans.xml中配置,否则容器就会抛出BeanInitializationException异常

private String name;
	@Required
	public void setName(String name){
		this.name= name;
	}
	public String getName(){
		return this.name;
	}

4、@Resource   默认是按名称来装配注入的,只有当找不到与名称匹配的bean才会按照类型来装配注入【jdk1.6支持】

class B{	
		@Resource(name="a") //自动在Bean对象B中通过byName自动注入a
		private A a;
	}

5、@Qualifier 当beans.mxl中有多个相同类型但不同名时,@Qualifier("bean1")可以确定使用名为bean1的bean对象【可以与autowire组合按名装配】

class B{	
		@Autowired //自动在Bean对象B中通过byType自动注入A
		@Qualifier(name="a") //修改为通过byName装配
		private A a;
	}

6、@PostConstruct 该注解函数代替初始化回调函数
7、@PreDestroy 该注解函数代替销毁回调函数

class A{
		private String name;
   		public void setName(String name){
			this.name= name;
		}
		public String getName(){
			return this.name;
		}
		@PostConstruct
   		public void init(){
      		System.out.println("A类 init 中.");
   		}
   		@PreDestroy
   		public void destroy(){
      		System.out.println("B类 destroy 中.");
   		}
	}

8、@Configuration 注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源
9、@Bean 与@Configuration组合,注解方法返回一个对象B,相当于在beans.xml中注册对象B,
    @Bean(initMethod="init()",destroyMethod="destroy()")相当于在xml中初始化和销毁方法的属性

@Configuration
	public class A {
   		@Bean 
   		public B b(){
      		return new B();
   		}
	}

10、@Import 导入其它类中加载的@Bean, 类A中注入了类B,类C中注入了类D,在C中使用@Improt(A.class),便可只加载C时就可以获取B

@Configuration
	public class A {
   		@Bean
   		public B b() {
      		return new B(); 
   		}
	}
	
	@Configuration
	@Import(A.class)
	public class C {
   		@Bean
   		public D d() {
      		return new D(); 
   		}
	}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值