一springの紹介

3352 ワード

1 Spring   
Spring          (Inversion of Control ,IoC)     (AOP)     .             .

2        
public class PersonServiceBean {
     private PersonDao personDao = new PersonDaoBean();
	
      public void save(Person person){
            personDao.save(person);
     }
}

PersonDaoBean             。                         ,
                   。                 ,            。

3     

                  ,  PersonServiceBean        :
public class PersonServiceBean {
     private PersonDao personDao ;
    //       ,               PersonServiceBean,       setter      。
     public PersonServiceBean(PersonDao personDao){
         this.personDao=personDao;
     }	
      public void save(Person person){
            personDao.save(person);
     }
}

         :    ,                   。

4      Spring
      ,      spring           
          ,           。
    

               , :      、      。            ,
                 .            。

          ,                。

     AOP  ,             、        。

          ,              , : JdbcTemplate、 HibernateTemplate。

Spring                , :  Hibernate、JPA、Struts ,          。


5     Spring,              
  ,    spring,                  
public void payment(){
       Bean1.update();//    
       Bean2.save();//      
}
       Spring,           ,      ?
 1        :  Bean1.update() Bean2.save()         。
 2        :    Bean1.update()        ,         。
public class Bean1 { 
      public void update(){//  :         
	 Connection conn = null;
	 conn.setAutoCommit(false);
     	 Statement.executeUpdate(“update account set amount=? where id=?"); 	
      }
}
public class Bean2 {
      public void save(){//  :         
	 Connection conn = null;
	 conn.setAutoCommit(false);
     	 Statement.executeUpdate(“insert into Log (content) values (?)");
      }
}


  Spring,                                
1.  Bean1.update() Bean2.save()          
2.    Bean1.update()        ,       。
@Transactional(propagation=Propagation.Required)
public void payment(){
       Bean1.update();//    
       Bean2.save();//    
}
 public class Bean1 {
      @Transactional(propagation=Propagation.Required)
      public void update(){
                    executeUpdate(“update account set amount=? where id=?"); 	
      }
}
public class Bean2 {
      @Transactional(propagation=Propagation.RequiresNew)
      public void save(){
	executeUpdate(“insert into Log (content) values (?)");
      }
}



6             
        spring       ,      ?                    ,           .
       ,      java         ,                    .

  spring  ,        ,                ,        ,
          ,           , :    spring    ,                  ,
       spring        ,          。  EJB              EJB        ,
        。


  !end!