标签:style blog class code java c
所需jar包:
bean:
1 package com.myspring.bean; 2 3 public class User { 4 private String id; 5 private String name; 6 7 public String getId() { 8 return id; 9 } 10 11 public void setId(String id) { 12 this.id = id; 13 } 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 }
service:
1 package com.myspring.service; 2 3 import java.util.List; 4 5 import com.myspring.bean.User; 6 import com.myspring.util.ApplicationContextUtil; 7 8 public class UserService { 9 private User u; 10 private List<String> l; 11 12 public User getU() { 13 return u; 14 } 15 16 public void setU(User u) { 17 this.u = u; 18 } 19 20 public List<String> getL() { 21 return l; 22 } 23 24 public void setL(List<String> l) { 25 this.l = l; 26 } 27 28 public void callUser() { 29 System.out.println("hey " + u.getName()); 30 System.out.println("List --" + l); 31 } 32 33 public static void main(String[] args) { 34 ((UserService) ApplicationContextUtil.getCtx().getBean("userService")) 35 .callUser(); 36 } 37 }
util:
1 package com.myspring.util; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.FileSystemXmlApplicationContext; 5 6 final public class ApplicationContextUtil { 7 private static ApplicationContext ctx; 8 9 private ApplicationContextUtil() { 10 11 } 12 13 static { 14 ctx = new FileSystemXmlApplicationContext( 15 "config/applicationContext.xml"); 16 } 17 18 public static ApplicationContext getCtx() { 19 return ctx; 20 } 21 }
applicationContext:
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" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 10 <context:annotation-config /> 11 <context:component-scan base-package="com"/> 12 <bean id="user" class="com.myspring.bean.User"> 13 <property name="id" value="5" /> 14 <property name="name" value="nevermore" /> 15 </bean> 16 17 <bean id="userService" class="com.myspring.service.UserService"> 18 <property name="u" ref="user" /> 19 <property name="l"> 20 <list> 21 <value>1</value> 22 <value>新野</value> 23 <value>u belong 2 me</value> 24 </list> 25 </property> 26 </bean> 27 </beans>
运行:UserService
hey nevermore
List --[1, 新野, u belong 2 me]
标签:style blog class code java c
原文地址:http://www.cnblogs.com/yurnero0716/p/3725579.html