Spring 3 EL Annotation 版本 简单maven工程

pom.xml:

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" 
 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4   <modelVersion>4.0.0</modelVersion>
 5   <groupId>com.zenger.spring</groupId>
 6   <artifactId>spring001</artifactId>
 7   <version>0.0.1-SNAPSHOT</version>
 8   <properties>
 9       <junit.v>4.8.2</junit.v>
10       <spring.v>3.1.1.RELEASE</spring.v>
11   </properties>
12   <dependencies>
13       <!-- junit -->
14       <dependency>
15           <groupId>junit</groupId>
16          <artifactId>junit</artifactId>
17          <version>${junit.v}</version>
18       </dependency>
19       <!-- spring -->
20       <dependency>
21           <groupId>org.springframework</groupId>
22         <artifactId>spring-core</artifactId>
23         <version>${spring.v}</version> 
24       </dependency>
25       <dependency>
26         <groupId>org.springframework</groupId>
27         <artifactId>spring-context</artifactId>
28         <version>${spring.v}</version> 
29     </dependency>
30     <dependency>
31         <groupId>org.springframework</groupId>
32         <artifactId>spring-expression</artifactId>
33         <version>${spring.v}</version>
34     </dependency>
35     <dependency>
36         <groupId>org.springframework</groupId>
37         <artifactId>spring-web</artifactId>
38         <version>${spring.v}</version>
39     </dependency>
40   </dependencies>
41 </project>

beans.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="
 7                http://www.springframework.org/schema/beans
 8             http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 9              http://www.springframework.org/schema/context
10             http://www.springframework.org/schema/context/spring-context-3.1.xsd
11             ">    
12     <!-- ################# scan ################ -->
13     <context:component-scan base-package="com.zenger.service"></context:component-scan>
14 </beans>

Item.java:

 1 package com.zenger.service;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.stereotype.Component;
 5 
 6 @Component(value="itemBean")
 7 public class Item {
 8     @Value(value="item B")
 9     private String name;
10     @Value(value="10")
11     private int qty;
12     public String getName() {
13         return name;
14     }
15     public void setName(String name) {
16         this.name = name;
17     }
18     public int getQty() {
19         return qty;
20     }
21     public void setQty(int qty) {
22         this.qty = qty;
23     }
24     @Override
25     public String toString() {
26         return "Item [name=" + name + ", qty=" + qty + "]";
27     }
28 }

Custom.java:

 1 package com.zenger.service;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.stereotype.Component;
 5 
 6 @Component(value="customBean")
 7 public class Custom {
 8     @Value(value="#{itemBean}")
 9     private Item item;
10     @Value(value="my item...")
11     private String itemName;
12     public Item getItem() {
13         return item;
14     }
15     public void setItem(Item item) {
16         this.item = item;
17     }
18     public String getItemName() {
19         return itemName;
20     }
21     public void setItemName(String itemName) {
22         this.itemName = itemName;
23     }
24 }

===================test=======================

 1 @Test
 2 public void test_generator() {
 3 
 4   ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
 5 
 6   Custom custom = context.getBean(Custom.class);
 7 
 8   System.out.println(custom.getItem());
 9   System.out.println(custom.getItemName());
10 
11 }

ok! Annotation简单易懂。

posted on 2013-09-02 16:03  zenger1025  阅读(239)  评论(0)    收藏  举报

导航