spring framework Annotation(注解)

spring  framework (4.2.4)Annotation(注解)
1、建立java项目

所用JAR
commons-logging-1.1.3.jar
spring-aop-4.2.4.RELEASE.jar
spring-beans-4.2.4.RELEASE.jar
spring-context-4.2.4.RELEASE.jar
spring-core-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar

2、创建bean.xml放在项目src目录下

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3 xmlns="http://www.springframework.org/schema/beans"
 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5 xmlns:context="http://www.springframework.org/schema/context" <!-- 需手动添加 -->
 6 xsi:schemaLocation="http://www.springframework.org/schema/beans
 7 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
 8 http://www.springframework.org/schema/context    <!-- 需手动添加 -->
 9 http://www.springframework.org/schema/context/spring-context-3.0.xsd" <!-- 需手动添加 -->
10 >
11 <!-- 配置扫描说有的com下所有的包以及子包 -->
12 <context:component-scan base-package="com"/>
13 </beans>

2、建立JAVA实体类

Student类

 1 package com.entity;
 2
 3 import javax.annotation.Resource;
 4
 5 import org.springframework.beans.factory.annotation.Value;
 6 import org.springframework.stereotype.Component;
 7
 8 @Component("student")
 9 public class Student {
10 @Value("20160125")
11 private int id;
12 @Value("张三")
13 private String name;
14 @Resource(name="myDate")
15 private MyDate date;
16 public int getId() {
17 return id;
18 }
19 public void setId(int id) {
20 this.id = id;
21 }
22 public String getName() {
23 return name;
24 }
25 public void setName(String name) {
26 this.name = name;
27 }
28 public MyDate getDate() {
29 return date;
30 }
31 public void setDate(MyDate date) {
32 this.date = date;
33 }
34
35 @PostConstruct//初始化方法
36 //@PreDestory销毁
37 public void abc() {
38 System.out.println("abc()");
39 }
40
41 }

MyDate类

 1 package com.entity;
 2
 3 import java.util.Date;
 4
 5 import org.springframework.stereotype.Service;
 6
 7 //@Component
 8 @Service
 9 public class MyDate extends Date {
10
11 }

3、Test类

 1 package com.tese;
 2
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 import com.entity.Student;
 6
 7 public class Test {
 8 public static void main(String[] args) {
 9 ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
10
11 Student stu = (Student)ctx.getBean("student");
12 System.out.println(stu.getId());
13 System.out.println(stu.getName());
14 System.out.println(stu.getDate());
15 }
16 }

运行结果:
20160125
张三
Mon Jan 25 14:34:07 CST 2016

时间: 2024-08-09 02:18:50

spring framework Annotation(注解)的相关文章

spring与hibernate整合配置基于Annotation注解方式管理实务

1.配置数据源 数据库连接基本信息存放到properties文件中,因此先加载properties文件 1 <!-- jdbc连接信息 --> 2 <context:property-placeholder 3 location="classpath:io/shuqi/ssh/spring/transactionalAnnotation/jdbc.properties"/> 使用DBCP数据源配置xml如下 1 <!-- dbcp数据源配置 -->

spring中自定义注解(annotation)与AOP中获取注解

一.自定义注解(annotation) 自定义注解的作用:在反射中获取注解,以取得注解修饰的类.方法或属性的相关解释. package me.lichunlong.spring.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.R

spring的@Transactional注解详细用法

概述 事务管理对于企业应用来说是至关重要的,即使出现异常情况,它也可以保证数据的一致性.Spring Framework对事务管理提供了一致的抽象,其特点如下: 为不同的事务API提供一致的编程模型,比如JTA(Java Transaction API), JDBC, Hibernate, JPA(Java Persistence API和JDO(Java Data Objects) 支持声明式事务管理,特别是基于注解的声明式事务管理,简单易用 提供比其他事务API如JTA更简单的编程式事务管理

[Spring笔记]支持注解的Spring调度器

概述 如果想在Spring中使用任务调度功能,除了集成调度框架Quartz这种方式,也可以使用Spring自己的调度任务框架. 使用Spring的调度框架,优点是:支持注解(@Scheduler),可以省去大量的配置. 实时触发调度任务 TaskScheduler接口 Spring3引入了TaskScheduler接口,这个接口定义了调度任务的抽象方法. TaskScheduler接口的声明: public interface TaskScheduler { ScheduledFuture<?>

Spring Framework启动详解

之前一直对Spring启动的过程很迷糊,所以这次国庆梳理一下. Spring启动一言以蔽之:创建一个根应用上下文.(因为其他的所有的应用上下文都可以通过各种方式继承它) (一)了解应用上下文 Spring Framework容器以一个或多个应用上下文的形式存在,由org.springframework.context.ApplicationContext接口表示.每一个应用上下文管理着一组bean.执行业务逻辑的Java对象.执行任务.持久化和获取持久化数据.响应HTTP请求等.由Spring管

Spring Framework 组件注册 之 @Import

Spring Framework 组件注册 之 @Import 写在前面 向spring中注册组件或者叫javaBean是使用spring的功能的前提条件.而且spring也提供了很多种方式,让我们可以将普通的javaBean注册到spring容器中,比如前一篇文章Spring Framework 组件注册 之 @Component中写的利用@Component注解将普通的javaBean注册到容器中,本文说的@Import注解也是spring Framework提供的将普通javaBean注册

spring mvc 方法注解拦截器

应用场景,在方法级别对本次调用进行鉴权,如api接口中有个用户唯一标示accessToken,对于有accessToken的每次请求可以在方法加一个拦截器,获得本次请求的用户,存放到request或者session域. python中,之前在python flask中可以使用装饰器来对方法进行预处理,进行权限处理 先看一个实例,使用@access_required拦截: @api.route('/post_apply') @access_required def apply():     "&q

Spring MVC常用注解

cp by http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Model 返回给对应的View 进行展示.在SpringMVC 中提供了一个非常简便的定义Controller 的方法,你无需继承特定的类或实现特定的接口,只需使用@Control

使用Spring处理自定义注解

使用Spring处理自定义注解 本文只讲思想,不讲代码. 可能的两种方法 spring schema spring aop aspect 参考1 dubbo service 包名:com.alibaba.dubbo.config 参考2 spring mvc 包名:org.springframework.web.servlet.config 可以参考这两个的实现,利用schema添加自定义注解并处理自己的注解,注册搜索模块. 源码分析 通过schema添加配置解析如: 在 spring配置文件中