public interface IPersonService {
 public abstract void Save();
}
public class PersonDaoBean implements IPersonDao {
 
 public void add(){
  System.out.println("这是personDaoBean的Add()方法");
 }
}
public class PersonServiceBean implements IPersonService {
 private IPersonDao iPersonDao;
 
 public IPersonDao getIPersonDao() {
  return iPersonDao;
 }
 public void setIPersonDao(IPersonDao personDao) {
  iPersonDao = personDao;
 }
 public void Save(){
  iPersonDao.add();
 }
 
 
}
<bean id="personDaoBean" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
 <bean id="personService"
  class="cn.itcast.service.impl.PersonServiceBean">
  <property name="IPersonDao" ref="personDaoBean"></property>
  </bean>
   
public void instanceSpring() {
  ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
    "beans.xml");
  IPersonService ipersonService = (IPersonService) ctx
    .getBean("personService");
  ipersonService.Save();
  ctx.close();
  // ctx.registerShutdownHook();
 }
输出:这是personDaoBean的Add()方法
			
			
	
		 
	
			
			
			public class PersonServiceBean implements IPersonService {
 public void init(){
  
  System.out.println("我是初始化函数");
 }
 
 public PersonServiceBean(){
  System.out.println("我是构造函数");
 }
 public void Save(){
  System.out.println("save方法");
 }
 
 public void cleanup(){
  System.out.println("cleanup方法");
 }
}
<bean id="personService"
  class="cn.itcast.service.impl.PersonServiceBean" 
   init-method="init" destroy-method="cleanup"/>
@Test public void instanceSpring(){
  ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
  IPersonService ipersonService=(IPersonService)ctx.getBean("personService");
 ctx.close();
			
			
	
		 
	
			
			
			Bean的作用域 
.singleton 
 在每个Spring IoC容器中一个bean定义只有一个对象实例。默认情况下会在容器启动时初始化bean,但我们可以指定Bean节点的lazy-init=“true”来延迟初始化bean,这时候,只有第一次获取bean会才初始化bean。如:
 <bean id="xxx" class="cn.itcast.OrderServiceBean" lazy-init="true"/>
如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init=“true“,如下:
<beans default-lazy-init="true“ ...>
.prototype 
 每次从容器获取bean都是新的对象。
 
.request 
.session 
.global session
 
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  PersionSevice ps=(PersionSevice)ctx.getBean("persionServiceBean");
  PersionSevice ps2=(PersionSevice)ctx.getBean("persionServiceBean");
  System.out.println(ps==ps2);
 输出:true
可见spring容器默认的bean的产生方式是单例
 
改
 <bean id="persionServiceBean" class="cn.com.xinli.service.impl.PersionServiceBean" scope="prototype"></bean>  
 
这时候输出:false ,显然ps与ps2就不一样。
			
			
	
		 
	
			
			
			三种实例化bean的方式
 
1.使用类构造器实例化
<bean id=“orderService" class="cn.itcast.OrderServiceBean"/>
 
 
2.使用静态工厂方法实例化
<bean id="persionServiceBean2" class="cn.com.xinli.service.impl.PersionServiceBeanFactory" factory-method="createPersionServiceBean"/>
 
public class PersionServiceBeanFactory
{
 public static PersionServiceBean createPersionServiceBean()
 {
  return new PersionServiceBean();
 }
}
 
例子:
 
(1).首先写工厂类.他其中包含产生我们的业务bean的方法
    - package cn.com.xinli.service.impl;    
    
 -    
    
 -    
    
 - public class PersionServiceBeanFactory    
    
 - {    
    
 -     public static PersionServiceBean createPersionServiceBean()    
    
 -     {    
    
 -         return new PersionServiceBean();    
    
 -     }    
    
 - }   
 
 
package cn.com.xinli.service.impl;
public class PersionServiceBeanFactory
{
public static PersionServiceBean createPersionServiceBean()
{
return new PersionServiceBean();
}
}
 
(2).改写beans.xml :包含工厂类类名和产生业务bean的方法名字
 
 <bean id="persionServiceBean2" class="cn.com.xinli.service.impl.PersionServiceBeanFactory" factory-method="createPersionServiceBean"/>
 
 (3) 测试
 
    - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");    
    
 -         PersionSevice ps=(PersionSevice)ctx.getBean("persionServiceBean2");    
    
 -                                                          
    
 -         ps.save();   
 
 
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersionSevice ps=(PersionSevice)ctx.getBean("persionServiceBean2");
ps.save();
 (4) 结果
 
2009-05-24 14:34:00,781  INFO (PersionServiceBean.java:12) - 我是save()方法!
 
3.使用实例工厂方法实例化:
 
<bean id="PersionServiceBeanFactory" class="cn.com.xinli.service.impl.PersionServiceBeanFactory"></bean>
    <bean id="persionServiceBean3" factory-bean="PersionServiceBeanFactory" factory-method="createPersionServiceBean2"></bean>
 
public  PersionServiceBean createPersionServiceBean2()
 {
  return new PersionServiceBean();
 }
 
例子:
 
(1). 首先写工厂类.他其中包含产生我们的业务bean的方法 ,在已有代码的基础上
    - package cn.com.xinli.service.impl;    
    
 -    
    
 -    
    
 - public class PersionServiceBeanFactory    
    
 - {    
    
 -     public static PersionServiceBean createPersionServiceBean()    
    
 -     {    
    
 -         return new PersionServiceBean();    
    
 -     }    
    
 -         
    
 -     <SPAN style="COLOR: #ff0000">public  PersionServiceBean createPersionServiceBean2()    
    
 -     {    
    
 -         return new PersionServiceBean();    
    
 -     }</SPAN>    
    
 -         
    
 - }   
 
 
package cn.com.xinli.service.impl;
public class PersionServiceBeanFactory
{
public static PersionServiceBean createPersionServiceBean()
{
return new PersionServiceBean();
}
public  PersionServiceBean createPersionServiceBean2()
{
return new PersionServiceBean();
}
}
 
 
(2).改写beans.xml :写两个bean,一个是工厂bean,一个是利用工厂bean产生业务bean的bean.
 
    - <bean id="PersionServiceBeanFactory" class="cn.com.xinli.service.impl.PersionServiceBeanFactory"></bean>   
    
 -     <bean id="persionServiceBean3" factory-bean="PersionServiceBeanFactory" factory-method="createPersionServiceBean2"></bean>   
 
 
<bean id="PersionServiceBeanFactory" class="cn.com.xinli.service.impl.PersionServiceBeanFactory"></bean>
<bean id="persionServiceBean3" factory-bean="PersionServiceBeanFactory" factory-method="createPersionServiceBean2"></bean>
 
 (3) 测试
 
    - ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");    
    
 -         PersionSevice ps=(PersionSevice)ctx.getBean("persionServiceBean3");    
    
 -                                                          
    
 -         ps.save();   
 
 
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersionSevice ps=(PersionSevice)ctx.getBean("persionServiceBean3");
ps.save();
 (4) 结果
 
2009-05-24 14:49:17,812  INFO (PersionServiceBean.java:12) - 我是save()方法!
 
 (5) 注意,其实方式2和方式3的区别就在 工厂类中是如何产生业务bean的,方式2是static方式,方式3不是
			
			
	
		 
	
			
			
			 前几天时间学习了Struts2.感觉还是有些新的认识。今天开始学习spring,认真阅读了夏昕翻译的文档,虽然只看了前几张,但总算有个入门,并且有个初步认识,并且看到了其他网友写的如何学习spring,感觉还是很不错。感谢对此提供的宝贵建议。
下面是应用的一些话:
SpringFramework是根据Rod Johnson著名的《Expert One-on-One J2EE Design and Development》而开发的J2EE应用程序框架。目前主要根据Rod Johnson和Juergen Hoeller而进行开发的。 Spring是J2EE应用程序框架,不过,更严格地讲它是针对Bean的生命周期进行管理的轻量级容器(Lightweight container),可以单独利用Spring构筑应用程序,也可以和Struts,Webwork,Tapestry等众多Web应用程序框架组合使用,并且可以与Swing等桌面应用程序API组合。
 1、如何学习Spring?
你可以通过下列途径学习spring:
(1) spring下载包中doc目录下的MVC-step-by-step和sample目录下的例子都是比较好的spring开发的例子。
(2) AppFuse集成了目前最流行的几个开源轻量级框架或者工具Ant,XDoclet,Spring,Hibernate(iBATIS),JUnit,Cactus,StrutsTestCase,Canoo's WebTest,Struts Menu,Display Tag Library,OSCache,JSTL,Struts 。
 你可以通过AppFuse源代码来学习spring。
 AppFuse网站:http://raibledesigns.com/wiki/Wiki.jsp?page=AppFuse 
(3)Spring 开发指南(夏昕)(http://www.xiaxin.net/Spring_Dev_Guide.rar) 
一本spring的入门书籍,里面介绍了反转控制和依赖注射的概念,以及spring的bean管理,spring的MVC,spring和hibernte,iBatis的结合。
 (4) spring学习的中文论坛
 SpringFramework中文论坛(http://spring.jactiongroup.net)
 Java视线论坛(http://forum.javaeye.com)的spring栏目
 2、利用Spring框架编程,console打印出log4j:WARN Please initialize the log4j system properly?
 说明你的log4j.properties没有配置。请把log4j.properties放到工程的classpath中,eclipse的classpath为bin目录,由于编译后src目录下的文件会拷贝到bin目录下,所以你可以把log4j.properties放到src目录下。
 这里给出一个log4j.properties的例子:
 log4j.rootLogger=DEBUG,stdout
 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
 log4j.appender.stdout.layout.ConversionPattern=%d %5p (%F:%L) - %m%n
 3、出现 java.lang.NoClassDefFoundError?
 一般情况下是由于你没有把必要的jar包放到lib中。
 比如你要采用spring和hibernate(带事务支持的话),你除了spring.jar外还需要hibernat.jar、aopalliance.jar、cglig.jar、jakarta-commons下的几个jar包。
http://www.springframework.org/down...,提供两种zip包 
建议你下载开发包。这个zip解压缩后比后者多一个lib目录,其中有hibernate、j2ee、dom4j、aopalliance、jakarta-commons等常用包。
4、java.io.FileNotFoundException: Could not open class path resource [....hbm.xml],提示找不到xml文件?原因一般有两个:
 (1)该xml文件没有在classpath中。
 (2)applicationContext-hibernate.xml中的xml名字没有带包名。比如:
 User.hbm.xml 错,改为: com/yz/spring/domain/User.hbm.xml
 net.sf.hibernate.dialect.MySQLDialect true
 5、org.springframework.beans.NotWritablePropertyException: Invalid property 'postDao' of bean class?
 出现异常的原因是在application-xxx.xml中property name的错误。 中name的名字是与bean的set方法相关的,而且要注意大小写。比如
 public class PostManageImpl extends BaseManage implements PostManage
 {
 private PostDAO dao = null;
 public void setPostDAO(PostDAO postDAO)
{
 this.dao = postDAO;
 }
 }
 那么xml的定义应该是:
 对 错
 6、Spring中如何实现事务管理?
 首先,如果使用mysql,确定mysql为InnoDB类型。
 事务管理的控制应该放到商业逻辑层。你可以写个处理商业逻辑的JavaBean,在该JavaBean中调用DAO,然后把该Bean的方法纳入spring的事务管理。
 比如:xml文件定义如下:
 class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED
 com.yz.spring.service.implement.UserManageImpl就是我们的实现商业逻辑的JavaBean。我们通过parent元素声明其事务支持。
 7、如何管理Spring框架下更多的JavaBean?
 JavaBean越多,spring配置文件就越大,这样不易维护。为了使配置清晰,我们可以将JavaBean分类管理,放在不同的配置文件中。 应用启动时将所有的xml同时加载。
 比如:
 DAO层的JavaBean放到applicationContext-hibernate.xml中,商业逻辑层的JavaBean放到applicationContext-service.xml中。然后启动类中调用以下代码载入所有的ApplicationContext。
 String[] paths = {"com/yz/spring/dao/hibernate/applicationContext-hibernate.xml", "com/yz/spring/service/applicationContext-service.xml"};
 ctx = new ClassPathXmlApplicationContext(paths);
 8、web应用中如何加载ApplicationContext?
 可以通过定义web.xml,由web容器自动加载。
 context
 org.springframework.web.context.ContextLoaderServlet
 1 contextConfigLocation /WEB-INF/applicationContext-hibernate.xml /WEB-INF/applicationContext-service.xml
 9、在spring中如何配置的log4j?
 在web.xml中加入以下代码即可。
 log4jConfigLocation /WEB-INF/log4j.properties
 10、Spring框架入门的编程问题解决了,我该如何更深地领会Spring框架呢?
 这两本书你该去看看。这两本书是由Spring的作者Rod Johnson编写的。
 Expert One on one J2EE Design and Development
 Expert One on one J2EE Development Without EJB
 你也该看看martinfowler的Inversion of Control Containers and the Dependency Injection pattern。
http://www.martinfowler.com/articles/injection.html
再好好研读一下spring的文档。
http://www.jactiongroup.net/reference/html/  
 
			
			
	
		 
	
			
			
			    必须学会的知识点:
   struts2 、Hibernate3.3、spring 2.5