Spring3 Java注解

Java注解是Java代码里的特殊标记,为我们在代码中添加用Java程序无法表达的额外信息提供了一种形式化的方法,使我们可以在未来某个时刻方便地使用这些被注解修饰的程序元素(这些程序元素包括:类、属性、方法),这些注解可以在编译、类加载、运行时读取,并执行相应的处理


Java注解的语法

不带参数的注解:@Annotation  例如@Override

带一个参数的注解;@Annotation(参数), 例如@SuppressWarnings(value="unused")

带多个参数的注解:@Annotation({参数1,参数2,参数3,......}),例如@MyTag(name="jhon",age=20)


1、   作用

1)     可以定位类对象

不需要Bean.xml中注册,直接注解方探测类对象

2)     可以注解方法初始化类属性

变量的值可以在Bean.xml中定义

注解引入到当前类属性中

3)     采用注解方法处理事务

4)     采用注解方法标识类

2、         注解编程

1)     需要在spring的Bean.xml定位注解

MyEclipse加载Spring时,这些配置不存在,需要手工加入

可能有多个地方加入配置点

2)     在类适当地方加入注解

可能是类上面

也可能是方法上面

3)     在spring容器中检测

依然是Bean.xml文件,此文件定义注解工作入口点

 

 

一、        关于注解模式定位类对象方法

1、         编程要点

1)     spring容器Bean.xml文件要3点要求

Ø  格式:context

Ø  开启注解模式

Ø  扫描当前类所在package

2)     java类定位

@Component

@Service

@Repository

上面3种注解定位类效果相同,用于区分业务分层

entity/bean/model,三种名称含意相同,@Repository

服务层,@Service

通用,@Component

3)     类的作用域

@Scope(“prototype”)

@Scope(value=”prototype”)

@Scope(“singleton”)

2、         注意点

所有注解后面无分号

3、         示例如下

BeanAnn.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:context="http://www.springframework.org/schema/context"

  xsi:schemaLocation="http://www.springframework.org/schema/beans

      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

      http://www.springframework.org/schema/context

      http://www.springframework.org/schema/context/spring-context-3.1.xsd

">

<!--

下面一行表示开启注解模式

 -->

   <context:annotation-config/>

   <!--

   下面要指注解类可能存在的package,指向当前类的package,或父package

   <context:component-scan base-package="iss"/>

   <context:component-scan base-package="iss.spring"/>

    -->

   <context:component-scan base-package="iss.spring.ann"/>

 

</beans>

 

package iss.spring.ann;

 

import org.springframework.context.annotation.Scope;

import org.springframework.stereotype.Component;

import org.springframework.stereotype.Repository;

import org.springframework.stereotype.Service;

 

import qh.Log;

 

/**

 * 作者:刘明

 *2017年10月15日,下午3:51:55

 */

//@Component

//@Service

@Repository

//@Scope("prototype")   //与下面一行等效

@Scope(value="prototype")

//@Scope("singleton")

public class Student {

     public Student() {

            Log.getLog().info("*88**,this="+this);

     }

 

}

public  class TestSpringAnn_First {

 

     /**

      * @param args

      */

     public static void main(String[] args) {

            // 构建spring工作容器

            ApplicationContext ac=new ClassPathXmlApplicationContext("/iss/spring/ann/BeanAnn.xml");

            Log.getLog().info("ok?");

            Student stud1=ac.getBean(Student.class);

            Log.getLog().info("stud1="+stud1);

            Student stud2=ac.getBean(Student.class);

            Log.getLog().info("stud2="+stud2);

            boolean check=stud1==stud2;

            Log.getLog().info("check="+check);

 

     }

 

}

 


 

二、        关于注解模式注入类属性

1、         实现需求

java类属性,注解方式送入

2、         编程要点

1)     这个类由spring容器管理

2)     变量的初始化在Bean.xml中定义

3)     类属性注解注入

@resource(name=”变量名称”)

4)     在spring容器中检测

3、         示例如下

BeanAnn2.xml

   <context:annotation-config/>

 

   <context:component-scan base-package="iss.spring.ann"/>

   <bean id="i1" class="java.lang.Integer"   >

      <constructor-arg value="10088"/>

   </bean>

   <bean id="dt1" class="java.util.Date"   >

      <constructor-arg value="88"/>

      <constructor-arg value="8"/>

      <constructor-arg value="18"/>

   </bean>

 

</beans>

//@Component

//@Service

@Repository

//@Scope("prototype")   //与下面一行等效

@Scope(value="prototype")

//@Scope("singleton")

public class Student2 {

    

     //下面的属性不需要set方法,也能送入

     @Resource(name="i1")

      Integer studId;

    

     @Resource(name="dt1")

      Date birthday;

     public Student2() {

            Log.getLog().info("*88**,this="+this);

     }

     public Integer getStudId() {

            return studId;

     }

     public Date getBirthday() {

            return birthday;

     }

    

 

}

public  class TestSpringAnn2 {

 

     /**

      * @param args

      */

     public static void main(String[] args) {

            // 构建spring工作容器

            ApplicationContext ac=new ClassPathXmlApplicationContext("/iss/spring/ann/BeanAnn2.xml");

            Log.getLog().info("ok?");

            Student2 stud1=ac.getBean(Student2.class);

            Log.getLog().info("stud1="+stud1);

//        Log.getLog().info("studId="+stud1.studId+",birthday="+stud1.birthday.toLocaleString());

            Log.getLog().info(

                          "studId="+stud1.getStudId()+

                          ",birthday="+stud1.getBirthday().toLocaleString());

           

 

     }

 

}

部分输出如下

iss.spring.ann.Student2.<init>(Student2.java:32)10-15 16:36:47>

*88**,this=iss.spring.ann.Student2@17ec256

 

 iss.spring.ann.TestSpringAnn2.main(TestSpringAnn2.java:25)10-15 16:36:48>

stud1=iss.spring.ann.Student2@17ec256

 

 iss.spring.ann.TestSpringAnn2.main(TestSpringAnn2.java:27)10-15 16:36:48>

studId=10088,birthday=1988-9-18 0:00:00


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值