Spring 3 EL XML 版本 简单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     <bean id="item" class="com.zenger.service.Item">
13         <property name="name" value="item A"></property>
14         <property name="qty" value="12"></property>
15     </bean>
16     <bean id="custom" class="com.zenger.service.Custom">
17         <property name="item" value="#{item}"></property>
18         <property name="itemName" value="#{item.name}"></property>
19     </bean>
20 </beans>

Custom.java:

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

Item.java:

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

Manage.java:

 1 import org.junit.Test;
 2 import org.springframework.beans.factory.BeanFactory;
 3 import org.springframework.beans.factory.xml.XmlBeanFactory;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 import org.springframework.core.io.FileSystemResource;
 7 
 8 import com.zenger.service.Custom;
 9 import com.zenger.service.Customer;
10 import com.zenger.service.CustomerService;
11 import com.zenger.service.GeneratorHelper;
12 import com.zenger.service.GeneratorSpringHelper;
13 import com.zenger.service.GreetService;
14 import com.zenger.service.IGenerator;
15 import com.zenger.service.JsonGenerator;
16 import com.zenger.service.StringGenerator;
17 
18 
19 public class Manage {
20     
21     @Test
22     public void test_el() {
23         ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
24         Custom custom = context.getBean(Custom.class);
25 //        Custom custom = (Custom) context.getBean("customBean");
26         System.out.println(custom.getItem());
27         System.out.println(custom.getItemName());
28     }
29     
30 
31 }

 

posted on 2013-09-02 15:57  zenger1025  阅读(319)  评论(0)    收藏  举报

导航