Framework knows how to do and leave what to do to user with interface programming and dynamic configuration.html
What is Struts?java
Structs's process sequence for requestweb
Hibernate(Linq to SQL)spring
Spring數據庫
Spring is an open source framework created to address the complexity of enterprise application development. One of the chief advantages of the Spring framework is its layered architecture, which allows you to be selective about which of its components you use while also providing a cohesive framework for J2EE application development.緩存
In this first article in my three-part Spring Series, I introduce you to the Spring framework. I start by describing the functionality of the framework in terms of its underlying modules and then discuss two of the most interesting modules, Spring aspect-oriented programming (AOP), and the Inversion of Control (IOC) container. I then use several examples to demonstrate the workings of the IOC container in a typical application use case scenario. The examples will also lay the foundation for an expanded discussion, later in this series, of the how the Spring framework implements AOP constructs through Spring AOP.session
Each of the modules (or components) that comprise the Spring framework can stand on its own or be implemented jointly with one or more of the others. The functionality of each component is as follows:app
BeanFactory
, an implementation of the Factory pattern. The BeanFactory
applies the Inversion of Control (IOC) pattern to separate an application's configuration and dependency specification from the actual application code.Spring framework functionality can be used in any J2EE server and most of it also is adaptable to non-managed environments. A central focus of Spring is to allow for reusable business and data-access objects that are not tied to specific J2EE services. Such objects can be reused across J2EE environments (Web or EJB), standalone applications, test environments, and so on, without any hassle.dom
Focus on:Interface-Oriented Programming & Inversion Of Control ( all concreate class is defined in spring application context.xml with object's property).ide
Take one example to understand how the spring application works.
Step1: We define our interfaces which the application will use. we will interface-oriented programming.
1 public interface CreateCreditCardAccountInterface 2 { 3 public CreditLinkingInterface getCreditLinkingInterface(); 4 public void setCreditLinkingInterface(CreditLinkingInterface creditLinkingInterface); 5 public CreditRatingInterface getCreditRatingInterface(); 6 public void setCreditRatingInterface(CreditRatingInterface creditRatingInterface); 7 public EmailInterface getEmailInterface(); 8 public void setEmailInterface(EmailInterface emailInterface); 9 public void createCreditCardAccount(ICustomer icustomer) throws Exception; 10 }
In CreateCreditCardAccountInterface, we use Interface CreditLinkingInterface
1 public interface CreditLinkingInterface 2 { 3 public String getUrl(); 4 public void setUrl(String url); 5 public void linkCreditBankAccount(ICustomer icustomer) throws Exception ; 6 }
In CreditLinkingInterface, we use interface ICustomer and CreditRatingInterface
1 //Interface holds customer information. 2 public interface ICustomer { 3 public String getCustomerId(); 4 public void setCustomerId(String customerId); 5 public String getFirstName(); 6 public void setFirstName(String firstName); 7 public IAddress getIAddress(); 8 public void setIAddress(IAddress address); 9 public String getLastName(); 10 public void setLastName(String lastName); 11 public String getSsnId(); 12 public void setSsnId(String ssnId); 13 public String getEmailAddress(); 14 public void setEmailAddress(String emailAddress); 15 public boolean isCreditRating() ; 16 public void setCreditRating(boolean creditRating); 17 }
1 public interface CreditRatingInterface 2 { 3 public boolean getUserCreditHistoryInformation(ICustomer iCustomer); 4 }
In ICustomer interface, we use interface and IAddress
1 public interface IAddress 2 { 3 public String getAddress1() ; 4 public void setAddress1(String address1) ; 5 public String getAddress2(); 6 public void setAddress2(String address2); 7 public String getCity() ; 8 public void setCity(String city); 9 public String getCountry() ; 10 public void setCountry(String country); 11 public String getState(); 12 public void setState(String state) ; 13 public String getZipCode() ; 14 public void setZipCode(String zipCode) ; 15 }
Step2: Now we shall config our application bean with our bussiness logic, these bean will create some concreate objects by Spring using Java Reflection.
1 ?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 3 <beans> 4 <bean id="createCreditCard" class="springexample.creditcardaccount.CreateCreditCardAccount"> 5 <property name="creditRatingInterface"> 6 <ref bean="creditRating" /> 7 </property> 8 <property name="creditLinkingInterface"> 9 <ref bean="creditLinking" /> 10 </property> 11 <property name="emailInterface"> 12 <ref bean="email" /> 13 </property> 14 </bean> 15 <bean id="creditLinking" class="springexample.creditlinking.CreditLinking"> 16 <property name="url"> 17 <value>http://localhost/creditLinkService</value> 18 </property> 19 </bean> 20 <bean id="creditRating" class="springexample.creditrating.CreditRating"> 21 </bean> 22 <bean id="email" class="springexample.email.Email"> 23 <property name="smtpHost"> 24 <value>localhost</value> 25 </property> 26 <property name="fromEmail"> 27 <value>mycompanyadmin@mycompanyadmin.com</value> 28 </property> 29 <property name="userId"> 30 <value>myuserid</value> 31 </property> 32 <property name="password"> 33 <value>mypassword</value> 34 </property> 35 </bean> 36 </beans>
Step3:OK, all is done, Application can run
1 package springexample.client; 2 import org.springframework.context.support.ClassPathXmlApplicationContext; 3 import springexample.creditcardaccount.CreateCreditCardAccountInterface; 4 import springexample.domain.Customer; 5 import springexample.domain.ICustomer; 6 public class CreateCreditAccountClient 7 { 8 public static void main(String[] args) 9 { 10 try 11 { 12 System.out.println("CreateCreditAccountClient started"); 13 ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {"springexample-creditaccount.xml"}); 14 System.out.println("Classpath loaded"); 15 ICustomer icustomer = new Customer(); 16 icustomer.setCustomerId("1A2B3C"); 17 icustomer.setEmailAddress("xxx@xx.com"); 18 icustomer.setFirstName("xxx"); 19 icustomer.setFirstName("yyy"); 20 21 CreateCreditCardAccountInterface creditCardAccount = (CreateCreditCardAccountInterface) 22 appContext.getBean("createCreditCard"); 23 creditCardAccount.createCreditCardAccount(icustomer); 24 System.out.println("CreateCreditAccountClient end"); 25 } 26 catch(Exception e) 27 { 28 e.printStackTrace(); 29 } 30 } 31 }
1 CreateCreditCardAccountInterface creditCardAccount = (CreateCreditCardAccountInterface) 2 3 appContext.getBean("createCreditCard"); //This line code acturally can be converted to following code. 4 5 //email = Class.forName("springexample.email.Email"); 6 //emial.setSmtpHost("localhost"); 7 //emial.setFromEmail("mycompanyadmin@mycompanyadmin.com"); 8 //emial.setPassword("myuserid"); 9 //emial.setUserId("mypassword"); 10 11 //creditRating = Class.forName("springexample.creditrating.CreditRating"); 12 //creditLinking = Class.forName("springexample.creditlinking.CreditLinking"); 13 //creditLinking.setUrl("http://localhost/creditLinkService"); 14 15 //creditCardAccount = Class.forName("springexample.creditcardaccount.CreateCreditCardAccount"); 16 //creditCardAccount.setCreditLinkingInterface(creditLinking); 17 //creditCardAccount.setCreditRatingInterface(creditLinking); 18 //creditCardAccount.setEmailInterface(creditLinking); 19 20 creditCardAccount.createCreditCardAccount(icustomer);