`
jetway
  • 浏览: 474412 次
  • 性别: 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>

分享到:
评论

相关推荐

    Spring2.5的新特性

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

    spring-property-annotations:Spring 2.5.x 组件的带注释的配置属性

    该项目仅用于 Spring 2.5.x 支持。 ##入门Spring房产注解定义了一个新的子类的的PropertyAnnotationAndPlaceholderConfigurer? 可以按如下方式使用: ###Maven 首先,如果您使用的是 Maven 2/3,请将以下依赖项...

    Struts2+Spring2.5+Hibernate3.3整合开发之Sring与Hibernate整合

    一、整合开发时Hibernate、Spring需要的JAR文件。 hibernate核心安装包下的(下载路径:http://www.hibernate.org/,点击“Hibernate Core”右边的“Downloads”): hibernate3.jar lib\bycode\cglib\hibernate-...

    Struts2.0+Springframework2.5+ibatis2.3完美整合用户登录及增删改查

    下面是Spring2.5注解功能的介绍 Spring支持JSR-250注解: Java EE5中引入了“Java平台的公共注解(Common Annotations for the Java Platform)”,而且该公共注解从Java SE 6一开始就被包含其中。 2006年5月,BEA...

    spring和hibernate jar包

    spring 2.5 -3.0以及hibernate3.0的jar包包括annotations

    Spring-Reference_zh_CN(Spring中文参考手册)

    2. Spring 2.0 的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的...

    Spring中文帮助文档

    2.7. 移植到Spring 2.5 2.7.1. 改变 2.8. 更新的样例应用 2.9. 改进的文档 I. 核心技术 3. IoC(控制反转)容器 3.1. 简介 3.2. 基本原理 - 容器和bean 3.2.1. 容器 3.2.2. 实例化容器 3.2.3. 多种bean ...

    Spring API

    2. Spring 2.0和 2.5的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 新的bean作用域 2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件...

    Spring 2.0 开发参考手册

    2.5. Web层 2.5.1. Spring MVC的表单标签库 2.5.2. Spring MVC合理的默认值 2.5.3. Portlet 框架 2.6. 其他特性 2.6.1. 动态语言支持 2.6.2. JMX 2.6.3. 任务规划 2.6.4. 对Java 5(Tiger)的支持 2.7. ...

    struts2.1.6+spring2.0+hibernate3.2常用配置包

    MyEclipse8.0中自带的struts2版本是2.1.6,spring版本有2.0,2.5的,hibernate版本较多些至3.2,首先选版本就选择最优的,struts2没的选只有2.1.6版的,所以先导入struts2支持,然后是spring选的是2.0,问题就出在...

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    2.5. Importing and mixing configurations 2.5.1. Referencing XML configuration in JavaConfig 2.5.2. Referencing JavaConfig in XML configuration 2.6. Summary Chapter 3. Advanced wiring 3.1. Environments...

    spring chm文档

    2.5. Web层 2.5.1. Spring MVC的表单标签库 2.5.2. Spring MVC合理的默认值 2.5.3. Portlet 框架 2.6. 其他特性 2.6.1. 动态语言支持 2.6.2. JMX 2.6.3. 任务规划 2.6.4. 对Java 5(Tiger)的支持 2.7. ...

    Spring Security 中文教程.pdf

    secured-annotations 和jsr250-annotations 属性 B.3.1.2. 安全方法使用&lt;protect-pointcut&gt; B.3.1.3. &lt;after-invocation-provider&gt; 元素 B.3.2. LDAP命名空间选项 B.3.2.1. 使用&lt;ldap-server&gt; 元素定义LDAP...

    Spring Security-3.0.1中文官方文档(翻译版)

    secured-annotations 和jsr250-annotations 属性 B.3.1.2. 安全方法使用&lt;protect-pointcut&gt; B.3.1.3. &lt;after-invocation-provider&gt; 元素 B.3.2. LDAP 命名空间选项 B.3.2.1. 使用&lt;ldap-server&gt; 元素定义...

    SpringSecurity 3.0.1.RELEASE.CHM

    secured-annotations和jsr250-annotations属性 B.3.1.2. 安全方法使用&lt;protect-pointcut&gt; B.3.1.3. &lt;after-invocation-provider&gt; 元素 B.3.2. LDAP命名空间选项 B.3.2.1. 使用元素定义LDAP服务器 B.3.2.2. ...

    web项目常用jar包及说明.zip

    10.struts2-spring-plugin-2.1.8.jar(struts2与spring集成时使用的) Spring需要的jar包: 1.spring.jar(里面含有spring的所有核心类库) 2.commons-logging-1.1.1.jar(ASF出品的日志包,struts2 2、spring、...

    前期JAVAWEB开发所需jar包资源

    commons-io-2.5.jar;commons-logging-1.2.jar;jackson-annotations-2.2.3.jar;jackson-core-2.2.3.jar;jackson-databind-2.2.3.jar;jstl.jar;mchange-commons-java-0.2.12.jar;mysql-connector-java-5.1.48-...

    SSH2用到的jar文件集合

    commons-lang-2.5.jar commons-logging.jar commons-pool.jar dom4j-1.6.1.jar freemarker-2.3.8.jar hibernate3.jar hibernate-annotations-3.4.0.GA.jar hibernate-commons-annotations-3.1.0.GA.jar javassist-...

    struts-2.5.10-all所有jar包

    hamcrest-core-1.3.jar,jackson-annotations-2.6.0.jar,jackson-core-2.6.1.jar,jackson-databind-2.6.1.jar,javassist-3.20.0-GA.jar,jcl-over-slf4j-1.7.6.jar,jcommander-1.12.jar,json-lib-2.3-jdk15.jar,juli-...

    开发用jar包合集

    servlet-api-2.5-20081211.jar simple-4.1.21.jar slf4j-api-1.7.7.jar snakeyaml-1.6.jar sonar-batch-bootstrapper-2.9.jar spring-aop-5.0.6.RELEASE-javadoc.jar spring-aop-5.0.6.RELEASE-sources.jar ...

Global site tag (gtag.js) - Google Analytics