spring-使用MyEcilpse創建demo


使用spring的一個小demo,先看一下結果:

  

目錄結構(web+spring):

              

 

spring所需架包:

              

 

 

 

applicationContext.xml文件:

 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:p="http://www.springframework.org/schema/p"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
 7     
 8     <!-- 添加容器。並創建對象 -->
 9     <bean id="userInfo" class="com.spring.entity.UserInfo">
10         <property name="userId" value="001"></property>
11         <property name="userName" value="龍偉"></property>
12         <property name="userSex" value="男"></property>
13         <property name="userAge" value="18"></property>
14     </bean>
15     
16     <!-- 通過IOC 機制創建dao對象(實現類對象) -->
17     <bean id="uDao" class="com.spring.dao.imlp.UserInfoImlp"/>
18     <!-- 賦值 將值賦給action對象的屬性 -->
19     <bean id="userAtion" class="com.spring.ation.UserInfoAtion">
20         <property name="userDao" ref="uDao"></property>
21     </bean>
22     
23     <!-- 
24         容器創建對象方式
25         scope="singletion" 單例模式, 只有一個對象(官方推薦使用)
26         scope="prototype"  容器可以創建多個對象,  默認
27         scope="request"    
28         scope="session"
29      -->
30      <bean id="emp" class="com.spring.entity.EmpInfo" scope="prototype">
31          <property name="name" value="龍龍"></property>
32          <property name="age" value="20"></property>
33          <property name="dptInfo" ref="dept1"></property>
34      </bean>
35      
36      <!-- 部門對象 -->
37      <bean id="dept1" class="com.spring.entity.DeptInfo">
38          <!-- 給部分字段設置注入 -->
39          <property name="deptName" value="技術部"></property>
40      </bean>
41      
42      <!-- 集合的注入 -->
43      <bean id="detp" class="com.spring.entity.DeptInfo">
44          <!-- 給部分字段設置注入 -->
45          <property name="deptName" value="財務部"></property>
46          
47          <!-- set集合 -->
48          <property name="set">
49              <set>
50                  <value>20</value>
51                  <value>50</value>
52                  <value>78</value>
53              </set>
54          </property>
55          <!-- list集合 -->
56          <property name="list">
57              <list>
58                  <value>李四</value>
59                  <value>趙六</value>
60                  <value>王二</value>
61              </list>
62          </property>
63          
64          <!-- map集合 -->
65          <property name="map">
66              <map>
67                  <entry key="sex" value="女"></entry>
68                  <entry key="address" value="重慶"></entry>
69              </map>
70          </property>
71      </bean>
72      
73      <!-- 使用靜態工廠 -->
74      <bean id="staticFactory" class="com.spring.entity.StaticFactory" factory-method="getUerInfo">
75          <constructor-arg value="u1"></constructor-arg>
76      </bean>
77 </beans>
View Code

web.xml文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 3   <display-name>Spring-code-1</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   <servlet>
13     <description>JAX-RS Tools Generated - Do not modify</description>
14     <servlet-name>JAX-RS Servlet</servlet-name>
15     <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
16     <load-on-startup>1</load-on-startup>
17   </servlet>
18   <servlet-mapping>
19     <servlet-name>JAX-RS Servlet</servlet-name>
20     <url-pattern>/jaxrs/*</url-pattern>
21   </servlet-mapping>
22   <listener>
23     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
24   </listener>
25   <context-param>
26     <param-name>contextConfigLocation</param-name>
27     <param-value>classpath:applicationContext.xml</param-value>
28   </context-param>
29 </web-app>
View Code

 UserInfoAtion.java:

 1 package com.spring.ation;
 2 
 3 import com.spring.dao.IUserInfo;
 4 
 5 public class UserInfoAtion {
 6     private IUserInfo userDao;
 7 
 8     public IUserInfo getUserDao() {
 9         return userDao;
10     }
11 
12     public void setUserDao(IUserInfo userDao) {
13         this.userDao = userDao;
14     }
15     
16     public void add(){
17         userDao.add();
18     }
19     
20     public void query(){
21         userDao.query();
22     }
23 }
View Code

IUserInfo.java:

 1 package com.spring.dao;
 2 
 3 import java.util.List;
 4 
 5 public interface IUserInfo {
 6     /**
 7      * 添加
 8      */
 9     void add();
10     /**
11      * 查看
12      */
13     List<String> query();
14 }
View Code

UserInfoImlp.java:

 1 package com.spring.dao.imlp;
 2 
 3 import java.util.List;
 4 
 5 import com.spring.dao.IUserInfo;
 6 
 7 public class UserInfoImlp implements IUserInfo{
 8 
 9     @Override
10     public void add() {
11         System.out.println("UserInfoImlp添加");
12     }
13 
14     @Override
15     public List<String> query() {
16         System.out.println("UserInfoImlp查詢");
17         return null;
18     }
19     
20 }
View Code

DeptInfo.java:

 1 package com.spring.entity;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 import java.util.Set;
 6 
 7 public class DeptInfo {
 8     private String deptName;
 9 
10     private List list;
11     private Set set;
12     private Map map;
13 
14     public DeptInfo() {
15         super();
16     }
17 
18     public DeptInfo(String deptName, List list, Set set, Map map) {
19         super();
20         this.deptName = deptName;
21         this.list = list;
22         this.set = set;
23         this.map = map;
24     }
25 
26     public String getDeptName() {
27         return deptName;
28     }
29 
30     public void setDeptName(String deptName) {
31         this.deptName = deptName;
32     }
33 
34     public List getList() {
35         return list;
36     }
37 
38     public void setList(List list) {
39         this.list = list;
40     }
41 
42     public Set getSet() {
43         return set;
44     }
45 
46     public void setSet(Set set) {
47         this.set = set;
48     }
49 
50     public Map getMap() {
51         return map;
52     }
53 
54     public void setMap(Map map) {
55         this.map = map;
56     }
57 
58     @Override
59     public String toString() {
60         return "DeptInfo [deptName=" + deptName + ", list=" + list + ", set=" + set + ", map=" + map + "]";
61     }
62 
63 }
View Code

EmpInfo.java:

 1 package com.spring.entity;
 2 
 3 public class EmpInfo {
 4     private String name;
 5     private Integer age;
 6     private DeptInfo dptInfo;
 7 
 8     public EmpInfo() {
 9         super();
10     }
11 
12     public EmpInfo(String name, Integer age, DeptInfo dptInfo) {
13         super();
14         this.name = name;
15         this.age = age;
16         this.dptInfo = dptInfo;
17     }
18 
19     public String getName() {
20         return name;
21     }
22 
23     public void setName(String name) {
24         this.name = name;
25     }
26 
27     public Integer getAge() {
28         return age;
29     }
30 
31     public void setAge(Integer age) {
32         this.age = age;
33     }
34 
35     public DeptInfo getDptInfo() {
36         return dptInfo;
37     }
38 
39     public void setDptInfo(DeptInfo dptInfo) {
40         this.dptInfo = dptInfo;
41     }
42 
43     @Override
44     public String toString() {
45         return "EmpInfo [name=" + name + ", age=" + age + ", dptInfo=" + dptInfo + "]";
46     }
47 }
View Code

StaticFactory.java:

 1 package com.spring.entity;
 2 
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 
 6 public class StaticFactory {
 7     private static Map<String, UserInfo> map = new HashMap<>();
 8     
 9     static{
10         //Integer userId, String userName, String userSex, Integer userAge
11         map.put("u1", new UserInfo(1, "龍龍", "男", 16));
12         map.put("u2", new UserInfo(1, "婷婷", "女", 22));
13         map.put("u3", new UserInfo(1, "浩浩", "男", 14));
14         map.put("u4", new UserInfo(1, "天天", "男", 18));
15         map.put("u5", new UserInfo(1, "江江", "男", 19));
16         map.put("u6", new UserInfo(1, "靜靜", "女", 12));
17         map.put("u7", new UserInfo(1, "坤坤", "男", 18));
18         map.put("u8", new UserInfo(1, "王五", "男", 19));
19     }
20     
21     public static UserInfo getUerInfo(String key){
22         return map.get(key);
23     }
24 }
View Code

UserInfo.java:

 1 package com.spring.entity;
 2 
 3 public class UserInfo {
 4     private Integer userId;
 5     private String userName;
 6     private String userSex;
 7     private Integer userAge;
 8 
 9     public UserInfo() {
10         super();
11     }
12 
13     public UserInfo(Integer userId, String userName, String userSex, Integer userAge) {
14         super();
15         this.userId = userId;
16         this.userName = userName;
17         this.userSex = userSex;
18         this.userAge = userAge;
19     }
20 
21     public Integer getUserId() {
22         return userId;
23     }
24 
25     public void setUserId(Integer userId) {
26         this.userId = userId;
27     }
28 
29     public String getUserName() {
30         return userName;
31     }
32 
33     public void setUserName(String userName) {
34         this.userName = userName;
35     }
36 
37     public String getUserSex() {
38         return userSex;
39     }
40 
41     public void setUserSex(String userSex) {
42         this.userSex = userSex;
43     }
44 
45     public Integer getUserAge() {
46         return userAge;
47     }
48 
49     public void setUserAge(Integer userAge) {
50         this.userAge = userAge;
51     }
52 
53     @Override
54     public String toString() {
55         return "UserInfo [userId=" + userId + ", userName=" + userName + ", userSex=" + userSex + ", userAge=" + userAge
56                 + "]";
57     }
58 
59 }
View Code

Test.java:

 1 package test;
 2 
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 
 5 import com.spring.ation.UserInfoAtion;
 6 import com.spring.entity.EmpInfo;
 7 import com.spring.entity.StaticFactory;
 8 import com.spring.entity.UserInfo;
 9 
10 public class Test {
11     public static void main(String[] args) {
12         // 獲取配置文件
13         ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext("applicationContext.xml");
14         
15         // 獲得用戶對象
16         UserInfo usent = bf.getBean("userInfo", UserInfo.class);
17         System.out.println("對象的信息為:" + usent);
18         
19         // 獲得ation對象
20         UserInfoAtion ua = bf.getBean("userAtion", UserInfoAtion.class);
21         ua.add();
22         ua.query();
23         
24         EmpInfo empInfo = bf.getBean("emp", EmpInfo.class);
25         System.out.println("empInfo: " + empInfo);
26         System.out.println("對象的信息行為:" + empInfo.getDptInfo().getDeptName());
27         
28         // 獲得工廠
29         UserInfo user = bf.getBean("staticFactory", UserInfo.class);
30         System.out.println(user);
31     }
32 }
View Code

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com