`
jetway
  • 浏览: 480211 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Spring2.5 Annotations

    博客分类:
  • java
 
阅读更多

完成setXxxx功能,即配置文件的 <property name="xxx" ref="xxx" /> 可省略, eg:

public class Boss {
    //required = false 代表 xml 文件可以不配置 car bean,如不定义且没有 car bean,则跑出异常
    @Autowired(required = false)
    private Car car;
    
    // @Qualifier("office2")代表当 xml 文件有多个 office 类型bean时,选择 id="office2" 的bean
    //@Autowired
    // public void setOffice(@Qualifier("office2")Office office) {
    @Autowired
    @Qualifier("office2")
    private Office office;

    
    @Override
    public String toString() {
        return "Car :" + car + "\noffice: " + office;
    }

}

XML配置:
    <bean id="boss" class="com.qtj.spring.annotations.Boss"/>
    <bean id="office" class="com.qtj.spring.annotations.Office">
        <property name="officeNo" value="001" />
    </bean>

    <bean id="office2" class="com.qtj.spring.annotations.Office">
        <property name="officeNo" value="002" />
    </bean>
    
    <bean id="car" class="com.qtj.spring.annotations.Car" scope="singleton">
        <property name="brand" value=" 红旗 CA72" />
        <property name="price" value="2000" />
    </bean>

@Resource

类似 @Autowired,只不过 Resource 是  byName的,且默认可以不需要 属性
public class Boss {
    
    //定义类型
    @Resource(type=Car.class)
    private Car car;
    
    // 多个同一类型bean时,指定特定 bean,相当于 @Autowried + @Qualifier("office1")
    @Resource(name="office1")
    private Office office;

    public Car getCar() {
        return car;
    }

    public Office getOffice() {
        return office;
    }

    @Override
    public String toString() {
        return "car:" + car + "\n" + "office:" + office;
    }

}

@PostConstruct, @PreDestroy

Bean在初始化后和销毁前做的工作
public class Boss {

    @Resource
    private Car car;

    @Resource(name = "office")
    private Office office;

    public Car getCar() {
        return car;
    }

    public Office getOffice() {
        return office;
    }

    @Override
    public String toString() {
        return "car:" + car + "\n" + "office:" + office;
    }

    @PostConstruct
    public void postConstruct1() {
        System.out.println("postConstruct1");
    }
    
    //初始化后,功能同 <bean ... init-method="postContruct2" .. />
    @PostConstruct
    public void postConstruct2() {
        System.out.println("postConstruct2");
    }
    
    //销毁前,功能同 <bean ... destroy-method="preContruct2" .. />
    @PreDestroy
    public void preDestroy1() {
        System.out.println("preDestroy1");
    }

}

Main 函数:
public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] locations = { "annoations5/beans.xml" };
         ClassPathXmlApplicationContext ctx =  new ClassPathXmlApplicationContext(locations);

//        ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);
        Boss boss = (Boss) ctx.getBean("boss");
        System.out.println(boss);
        
        ctx.destroy();
        

    }

测试结果:
postConstruct1
postConstruct2
car:brand:  红旗 CA72, price: 2000.0
office:Office No: 002
preDestroy1

@Component

某个属性类不需要在 XML 中定义对应的 bean

@Component
public class Car {
    
    private String brand;
    private double price;
    
    @Override
    public String toString() {
        return "brand: " + brand + ", price: " + price;
    }
...

@Scope("prototype")
@Component("boss")
public class Boss {

	private Car car;
	
	@Resource(name="office2")
	private Office office;
	
	@Override
	public String toString() {
		return "Car :" + car + "\noffice: " + office;
	}
...

XML配置
<context:component-scan base-package="com.qtj.spring.annotations6">    
      <context:include-filter type="regex" expression="com\.qtj\.spring\.annotations6\..*"/>
      <!-- need aspectj jar -->
      <!-- context:exclude-filter type="aspectj" expression="com\.qtj\.spring\.annotations6\.util..*"/ -->
    </context:component-scan>
<bean id="boss2" class="com.qtj.spring.annotations6.Boss"/>

@Repository@Service 和 @Controller 在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的

详见参考文档

spring2.5 context配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    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-2.5.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config />  

   <!--
    该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入
   <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
   
   该 BeanPostProcessor 将自动起作用,对标注 @Resource 的 Bean 进行自动注入
   <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
 
    -->      
</beans>

分享到:
评论

相关推荐

    spring 2.5

    在Spring 2.5中,最重要的改进之一是对Java 5的全面支持,包括注解(Annotations)和泛型(Generics)。这一变化使得开发者能够更方便地使用依赖注入,通过在类或方法上直接使用注解来声明依赖,而不是依赖XML配置。...

    spring2.5.2开发参考手册pdf,spring2.5.ppt,Spring2.5-中文参考手册.chm

    它可能会重点介绍新特性,如XML配置的简化(如@Annotations)、Bean定义的增强(如Prototype scope和Lazy initialization)、表达式语言(Spring Expression Language, SpEL)以及对JSR-303 Bean Validation的支持。...

    Spring2.5架构图

    Spring 2.5 是 Spring 框架的一个重要版本,它在 2008 年发布,引入了许多新特性和改进,提升了框架的灵活性和性能。在这个版本中,Spring 框架进一步完善了其核心特性,如依赖注入、AOP(面向切面编程)以及对 Java...

    struts2.0 spring2.5 hibernate3.2组合的jar包集合

    Struts2.0、Spring2.5和Hibernate3.2是Java Web开发中经典的三大框架,它们的组合常被用于构建高效的企业级应用。这个压缩包集合提供了这三大框架的兼容性版本,旨在解决集成过程中可能出现的jar包冲突问题。 **...

    配置整合DWR3.0和Spring2.5使用annotation注解

    在本文中,我们将探讨如何将Direct Web Remoting (DWR) 3.0与Spring 2.5框架整合,并利用注解(Annotation)进行配置。DWR是一个允许JavaScript与Java服务器端进行交互的库,而Spring 2.5引入了对注解的强大支持,...

    spring2.5 jar包

    Spring 2.5 是一个重要的Java应用程序框架,主要用于构建企业级的Java应用。这个框架以其依赖注入(Dependency Injection,简称DI)和面向切面编程(Aspect-Oriented Programming,简称AOP)为核心特性,极大地简化...

    Spring-Reference_zh_CN.rar_spring_spring 2.5_spring framework_sp

    《Spring Framework 2.5 开发参考手册》是Spring框架的重要文档,主要涵盖了Spring 2.5版本的核心特性和使用方法。Spring是一个开源的应用程序框架,它为Java平台提供了全面的基础设施支持,使得开发者能够更方便地...

    spring2.5源码包

    Spring 2.5 引入了对 JSR-250(Common Annotations for the Java Platform)的支持,使得在类和方法上使用注解来声明切面变得更为方便。`org.springframework.aop` 包中的类和接口展示了 AOP 的实现,包括切面...

    spring2.5jar包.zip

    9. **common-annotations.jar**:包含JSR-250规范定义的标准注解,如@PostConstruct和@PreDestroy,这些注解在Spring中用于标记初始化和销毁方法,使得Spring容器能够自动调用。 综上所述,Spring 2.5的Jar包集合...

    spring 2.5 docs

    Spring 2.5是该框架的一个重要版本,发布于2008年,它引入了许多关键特性,提升了灵活性和可扩展性。以下是Spring 2.5版本的一些主要知识点: 1. **依赖注入(Dependency Injection, DI)**:Spring 2.5对依赖注入...

    struts1.2+hibernate3.5+spring2.5框架搭建

    Struts1.2、Hibernate3.5和Spring2.5是经典的Java企业级开发框架组合,被称为SSH(Struts-Spring-Hibernate)架构。这个框架集合提供了强大的模型-视图-控制器(MVC)设计模式支持,数据持久化以及依赖注入等功能,...

    hibernate3[1].2+spring2.5+struts2.1配置过程

    本文将详细介绍如何在Windows XP环境下,使用MyEclipse 7.0和Tomcat 6.0容器,配置一个基于Hibernate 3.2、Spring 2.5和Struts2.1的项目。 首先,创建一个新的Web工程,命名为"ssh",选择Java EE 5.0规范。接着,...

    struts2.0 spring2.5 hibernate3.0

    Struts2.0、Spring2.5和Hibernate3.0是Java Web开发中经典的三大框架,它们各自在应用程序的不同层面提供了强大的支持,并且通过合理的整合,可以构建出高效、灵活的MVC(Model-View-Controller)架构的应用程序。...

    Spring2.5学习笔记

    ### Spring2.5 学习笔记详解 #### 一、Spring 框架简介 Spring 是一个开源的轻量级 Java 开发框架,主要用于简化企业级应用的开发工作。Spring 提供了一系列强大的功能,比如控制反转 (Inversion of Control, IOC)...

    spring2.5 -3.0 hibernate3.3 jar包说明

    ### Spring 2.5 - 3.0 与 Hibernate 3.3 Jar 包详解 在探讨Spring 2.5到3.0以及Hibernate 3.3的Jar包之前,我们先简要回顾一下这两个框架的基本概念。 #### Spring 框架简介 Spring是一个开源的应用框架,它提供了...

    Spring2.5的新特性

    &lt;br&gt;新发布的Spring2.5继续坚持了这个发展趋向,特别是为那些使用Java 5或更新版本java的开发人员提供了进一步简化而强大的新特性。这些新特性包括:注解驱动的依赖性注入(annotation-driven dependency ...

    Spring 2.5 Libraries

    7. **增强的Java 5支持**:Spring 2.5开始全面支持Java 5,包括枚举类型、注解(Annotations)和泛型。注解在Spring中被广泛用于配置、AOP和元数据等方面,显著减少了XML配置。 8. **Spring Expression Language ...

    ecside+struts2+spring2.5+hibernate3.2部分源代码

    标题 "ecside+struts2+spring2.5+hibernate3.2部分源代码" 描述了一个基于四个核心技术的项目,它们分别是ECSide、Struts2、Spring 2.5 和 Hibernate 3.2。这些技术是Java开发中的重要组件,尤其在构建企业级应用时...

    hibernate3.2+spring2.5+struts2.1配置过程.doc

    在本文中,我们将详细探讨如何配置一个基于Java的Web应用程序,使用Hibernate3.2作为持久层框架,Spring2.5作为应用上下文管理和依赖注入工具,以及Struts2.1作为MVC框架。这个组合通常被称为SSH(Struts2、Spring、...

    SSH 手动搭建Spring2.5+Strut1.3+Hibernate3.5

    本教程将详细介绍如何手动搭建Spring 2.5、Struts 1.3和Hibernate 3.5的集成环境。 **1. Spring 2.5** Spring是一个全面的企业级应用框架,它提供了AOP(面向切面编程)、DI(依赖注入)以及事务管理等功能。在...

Global site tag (gtag.js) - Google Analytics