* SpringµÄÓŵãÓÐʲô?
1. SpringÊÇ·Ö²ãµÄ
¼Ü¹¹£¬Äã¿ÉÒÔÑ¡ÔñʹÓÃ
ÄãÐèÒªµÄ²ã¶ø²»Óùܲ»ÐèÒªµÄ²¿·Ö
2. SpringÊÇPOJO±à³Ì£¬POJO±à³ÌʹµÃ¿É³ÖÐø¹¹½¨ºÍ¿É²âÊÔÄÜÁ¦Ìá¸ß
3. ÒÀÀµ×¢ÈëºÍIoCʹµÃJDBC²Ù×÷¼òµ¥»¯
4. SpringÊÇ¿ªÔ´µÄÃâ·ÑµÄ
5. SpringʹµÃ¶ÔÏó¹ÜÀí¼¯Öл¯ºÏ¼òµ¥»¯
* ÃèÊöÒ»ÏÂspringÖÐʵÏÖDI£¨dependency injection£©µÄ¼¸ÖÖ·½Ê½
·½Ê½Ò»£º
½Ó¿Ú×¢È룬ÔÚʵ¼ÊÖеõ½ÁËÆÕ±éÓ¦Ó㬼´Ê¹ÔÚIOCµÄ¸ÅÄîÉÐδȷÁ¢Ê±£¬ÕâÑùµÄ·½·¨Ò²ÒѾƵ·±³öÏÖÔÚÎÒÃǵĴúÂëÖС£
·½Ê½¶þ£ºType2 IoC: Setter injection¶ÔÏó´´½¨Ö®ºó£¬½«±»ÒÀÀµ¶ÔÏóͨ¹ýset·½·¨ÉèÖýøÈ¥
·½Ê½Èý£ºType3 IoC: Constructor injection¶ÔÏó´´½¨Ê±£¬±»ÒÀÀµ¶ÔÏóÒÔ¹¹Ôì·½·¨²ÎÊýµÄ·½Ê½×¢Èë
SpringµÄ·½Ê½
* ¼òµ¥ÃèÊöÏÂIOC(inversion of control)µÄ
Àí½â
Ò»¸öÀàÐèÒªÓõ½Ä³¸ö½Ó¿ÚµÄ·½·¨£¬ÎÒÃÇÐèÒª½«ÀàAºÍ½Ó¿ÚBµÄʵÏÖ¹ØÁªÆðÀ´£¬×î¼òµ¥µÄ·½·¨ÊÇÀàAÖд´½¨Ò»¸ö¶ÔÓÚ½Ó¿ÚBµÄʵÏÖCµÄʵÀý£¬µ«ÕâÖÖ·½·¨ÏÔÈ»Á½ÕßµÄÒÀÀµ£¨Dependency£©Ì«´óÁË¡£¶øIoCµÄ·½·¨ÊÇÖ»ÔÚÀàAÖж¨ÒåºÃÓÃÓÚ¹ØÁª½Ó¿ÚBµÄʵÏֵķ½·¨£¬½«ÀàA£¬½Ó¿ÚBºÍ½Ó¿ÚBµÄʵÏÖC·ÅÈëIoCµÄ ÈÝÆ÷£¨Container£©ÖУ¬Í¨¹ýÒ»¶¨µÄÅäÖÃÓÉÈÝÆ÷£¨Container£©À´ÊµÏÖÀàAÓë½Ó¿ÚBµÄʵÏÖCµÄ¹ØÁª¡£
* Spring¶ÔºÜ¶àORM¿ò¼ÜÌṩÁ˺ܺÃÖ§³Ö£¬ÃèÊöÏÂÔÚspringʹÓÃ
hibernateµÄ·½·¨
ÔÚcontextÖж¨ÒåDataSource£¬´´½¨SessionFactoy£¬ÉèÖòÎÊý£»DAOÀà¼Ì³ÐHibernateDaoSupport£¬ÊµÏÖ¾ßÌå½Ó¿Ú£¬´ÓÖлñµÃHibernateTemplate½øÐоßÌå²Ù×÷¡£ÔÚʹÓÃÖÐÈç¹ûÓöµ½OpenSessionInViewµÄÎÊÌ⣬¿ÉÒÔÌí¼ÓOpenSessionInViewFilter»òOpenSessionInViewInterceptor
* Çë½éÉÜÏÂspringµÄÊÂÎñ¹ÜÀí
springÌṩÁ˼¸¸ö¹ØÓÚÊÂÎñ´¦ÀíµÄÀࣺ
TransactionDefinition //ÊÂÎñÊôÐÔ¶¨Òå
TranscationStatus //´ú±íÁ˵±Ç°µÄÊÂÎñ£¬¿ÉÒÔÌá½»£¬»Ø¹ö¡£
PlatformTransactionManagerÕâ¸öÊÇspringÌṩµÄÓÃÓÚ¹ÜÀíÊÂÎñµÄ»ù´¡½Ó¿Ú£¬ÆäÏÂÓÐÒ»¸öʵÏֵijéÏóÀàAbstractPlatformTransactionManager,ÎÒÃÇʹÓõÄÊÂÎñ¹ÜÀíÀàÀýÈçDataSourceTransactionManagerµÈ¶¼ÊÇÕâ¸öÀàµÄ×ÓÀà¡£
Ò»°ãÊÂÎñ¶¨Òå²½Ö裺
TransactionDefinition td = new TransactionDefinition();
TransactionStatus ts = transactionManager.getTransaction(td);
try
{ //do sth
transactionManager.commit(ts);
}
catch(Exception e){transactionManager.rollback(ts);}
springÌṩµÄÊÂÎñ¹ÜÀí¿ÉÒÔ·ÖΪÁ½Àࣺ±à³ÌʽµÄºÍÉùÃ÷ʽµÄ¡£±à³ÌʽµÄ£¬±È½ÏÁé»î£¬µ«ÊÇ´úÂëÁ¿´ó£¬´æÔÚÖظ´µÄ´úÂë±È½Ï¶à£»ÉùÃ÷ʽµÄ±È±à³ÌʽµÄ¸üÁé»î¡£
±à³ÌʽÖ÷ҪʹÓÃtransactionTemplate¡£Ê¡ÂÔÁ˲¿·ÖµÄÌá½»£¬»Ø¹ö£¬Ò»ÏµÁеÄÊÂÎñ¶ÔÏó¶¨Ò壬Ðè×¢ÈëÊÂÎñ¹ÜÀí¶ÔÏó.
void add()
{
transactionTemplate.execute( new TransactionCallback(){
pulic Object doInTransaction(TransactionStatus ts)
{ //do sth}
}
}
ÉùÃ÷ʽ£º
ʹÓÃTransactionProxyFactoryBean:
PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED,readOnly
ΧÈÆPoxyµÄ¶¯Ì¬´úÀí Äܹ»×Ô¶¯µÄÌá½»ºÍ»Ø¹öÊÂÎñ
org.springframework.transaction.interceptor.TransactionProxyFactoryBean
PROPAGATION_REQUIRED¨CÖ§³Öµ±Ç°ÊÂÎñ£¬Èç¹ûµ±Ç°Ã»ÓÐÊÂÎñ£¬¾Íн¨Ò»¸öÊÂÎñ¡£ÕâÊÇ×î³£¼ûµÄÑ¡Ôñ¡£
PROPAGATION_SUPPORTS¨CÖ§³Öµ±Ç°ÊÂÎñ£¬Èç¹ûµ±Ç°Ã»ÓÐÊÂÎñ£¬¾ÍÒÔ·ÇÊÂÎñ·½Ê½Ö´ÐС£
PROPAGATION_MANDATORY¨CÖ§³Öµ±Ç°ÊÂÎñ£¬Èç¹ûµ±Ç°Ã»ÓÐÊÂÎñ£¬¾ÍÅ׳ö
Òì³£¡£
PROPAGATION_REQUIRES_NEW¨Cн¨ÊÂÎñ£¬Èç¹ûµ±Ç°´æÔÚÊÂÎñ£¬°Ñµ±Ç°ÊÂÎñ¹ÒÆð¡£
PROPAGATION_NOT_SUPPORTED¨CÒÔ·ÇÊÂÎñ·½Ê½Ö´ÐвÙ×÷£¬Èç¹ûµ±Ç°´æÔÚÊÂÎñ£¬¾Í°Ñµ±Ç°ÊÂÎñ¹ÒÆð¡£
PROPAGATION_NEVER¨CÒÔ·ÇÊÂÎñ·½Ê½Ö´ÐУ¬Èç¹ûµ±Ç°´æÔÚÊÂÎñ£¬ÔòÅ׳öÒì³£¡£
PROPAGATION_NESTED¨CÈç¹ûµ±Ç°´æÔÚÊÂÎñ£¬ÔòÔÚǶÌ×ÊÂÎñÄÚÖ´ÐС£Èç¹ûµ±Ç°Ã»ÓÐÊÂÎñ£¬Ôò½øÐÐÓëPROPAGATION_REQUIREDÀàËƵIJÙ×÷¡£
* ÈçºÎÔÚspringµÄapplicationContext.xmlʹÓÃJNDI¶ø²»ÊÇDataSource
¿ÉÒÔʹÓá±org.springframework.jndi.JndiObjectFactoryBean¡±À´ÊµÏÖ¡£Ê¾ÀýÈçÏ£º
<bean id=¡±dataSource¡±>
<property name=¡±jndiName¡±>
<value>java:comp/env/jdbc/appfuse</value>
</property>
</bean>
* ÔÚspringÖÐÊÇÈçºÎÅäÖÃÊý¾Ý¿âÇý¶¯µÄ
org.springframework.jdbc.datasource.DriverManagerDataSource¡±Êý¾ÝÔ´À´ÅäÖÃÊý¾Ý¿âÇý¶¯¡£Ê¾ÀýÈçÏ£º
<bean id=¡±dataSource¡±>
<property name=¡±driverClassName¡±>
<value>org.hsqldb.jdbcDriver</value>
</property>
<property name=¡±url¡±>
<value>jdbc:hsqldb:db/appfuse</value>
</property>
<property name=¡±username¡±><value>sa</value></property>
<property name=¡±password¡±><value></value></property>
</bean>
* springÖеÄapplicationContext.xmlÄܲ»ÄܸÄΪÆäËûÃû×Ö
ContextLoaderListenerÊÇÒ»¸ö
ServletContextListener, ËüÔÚÄãµÄwebÓ¦ÓÃÆô¶¯µÄʱºò³õʼ»¯¡£È±Ê¡Çé¿öÏ£¬ Ëü»áÔÚWEB-INF/applicationContext.xmlÎļþÕÒSpringµÄÅäÖᣠÄã¿ÉÒÔͨ¹ý¶¨ÒåÒ»¸ö<context-param>ÔªËØÃû×ÖΪ¡±contextConfigLocation¡±À´¸Ä±äSpringÅäÖÃÎļþµÄλÖá£Ê¾ÀýÈçÏ£º
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/xyz.xml</param-value>
</context-param>
</listener-class>
</listener>
* ÔÚwebÖÐÈçºÎÅäÖÃspring
ÔÚ
J2EEµÄwebÓ¦ÓÃÀïÃæÅäÖÃspring·Ç³£¼òµ¥£¬×î¼òµ¥µÄÖ»ÐèÒª°ÑspringµÃContextLoaderListenerÌí¼Óµ½ÄãµÄweb.xmlÎļþÀïÃæ¾Í¿ÉÒÔÁË£¬Ê¾ÀýÈçÏ£º
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
* ÔÚspringÖÐÈçºÎ¶¨Òåhibernate Mapping£¿
Ìí¼Óhibernate mapping Îļþµ½web/WEB-INFĿ¼ÏµÄapplicationContext.xmlÎļþÀïÃ档ʾÀýÈçÏ£º
<property name=¡±mappingResources¡±>
<list>
<value>org/appfuse/model/User.hbm.xml</value>
</list>
</property>
* Á½ÖÖÒÀÀµ×¢ÈëµÄÀàÐÍÊÇʲô?
Á½ÖÖÒÀÀµ×¢ÈëµÄÀàÐÍ·Ö±ðÊÇsetter×¢ÈëºÍ¹¹Ôì·½·¨×¢Èë¡£
setter×¢È룺 Ò»°ãÇé¿öÏÂËùÓеÄjava bean, ÎÒÃǶ¼»áʹÓÃsetter·½·¨ºÍgetter·½·¨È¥ÉèÖúͻñÈ¡ÊôÐÔµÄÖµ£¬Ê¾ÀýÈçÏ£º
public class namebean {
String name;
public void setName(String a) {
name = a; }
public String getName() {
return name; }
}
ÎÒÃǻᴴ½¨Ò»¸öbeanµÄʵÀýÈ»ºóÉèÖÃÊôÐÔµÄÖµ£¬springµÄÅäÖÃÎļþÈçÏ£º
<bean id=¡±bean1¡å >
<property name=¡±name¡± >
<value>tom</value>
</property>
</bean>
Spring»áµ÷ÓÃsetName·½·¨À´Ö»ÊÇnameÊìϤΪtom
¹¹Ôì·½·¨×¢È룺¹¹Ôì·½·¨×¢ÈëÖУ¬ÎÒÃÇʹÓôø²ÎÊýµÄ¹¹Ôì·½·¨ÈçÏ£º
public class namebean {
String name;
public namebean(String a) {
name = a;
}
}
ÎÒÃÇ»áÔÚ´´½¨beanʵÀýµÄʱºòÒÔnew namebean(¡±tom¡±)µÄ·½Ê½À´ÉèÖÃnameÊôÐÔ, SpringÅäÖÃÎļþÈçÏ£º
<bean id=¡±bean1¡å >
<constructor-arg>
<value>My Bean Value</value>
</constructor-arg>
</bean>
ʹÓÃconstructor-arg±êÇ©À´ÉèÖù¹Ôì·½·¨µÄ²ÎÊý¡£
* ½âÊÍÒ»ÏÂDependency Injection(DI)ºÍIOC£¨inversion of control£©?
²Î¿¼´ð°¸£ºÒÀÀµ×¢ÈëDIÊÇÒ»¸ö³ÌÐò
Éè¼ÆģʽºÍ¼Ü¹¹Ä£ÐÍ£¬ һЩʱºòÒ²³Æ×÷¿ØÖÆ·´×ª£¬¾¡¹ÜÔÚ¼¼ÊõÉÏÀ´½²£¬ÒÀÀµ×¢ÈëÊÇÒ»¸öIOCµÄÌØÊâʵÏÖ£¬ÒÀÀµ×¢ÈëÊÇÖ¸Ò»¸ö¶ÔÏóÓ¦ÓÃÁíÍâÒ»¸ö¶ÔÏóÀ´Ìṩһ¸öÌØÊâµÄÄÜÁ¦£¬ÀýÈ磺°ÑÒ»¸öÊý¾Ý¿âÁ¬½ÓÒѲÎÊýµÄÐÎʽ´«µ½Ò»¸ö¶ÔÏóµÄ½á¹¹·½·¨ÀïÃæ¶ø²»ÊÇÔÚÄǸö¶ÔÏóÄÚ²¿×ÔÐд´½¨Ò»¸öÁ¬½Ó¡£¿ØÖÆ·´×ªºÍÒÀÀµ×¢ÈëµÄ»ù±¾Ë¼Ïë¾ÍÊÇ°ÑÀàµÄÒÀÀµ´ÓÀàÄÚ²¿×ª»¯µ½ÍⲿÒÔ¼õÉÙÒÀÀµ
Ó¦ÓÿØÖÆ·´×ª£¬¶ÔÏóÔÚ±»´´½¨µÄʱºò£¬ÓÉÒ»¸öµ÷¿ØϵͳÄÚËùÓжÔÏóµÄÍâ½çʵÌ壬½«ÆäËùÒÀÀµµÄ¶ÔÏóµÄÒýÓ㬴«µÝ¸øËü¡£Ò²¿ÉÒÔ˵£¬ÒÀÀµ±»×¢Èëµ½¶ÔÏóÖС£ËùÒÔ£¬¿ØÖÆ·´×ªÊÇ£¬¹ØÓÚÒ»¸ö¶ÔÏóÈçºÎ»ñÈ¡ËûËùÒÀÀµµÄ¶ÔÏóµÄÒýÓã¬Õâ¸ö
ÔðÈεķ´×ª
* SpringÖÐBeanFactoryºÍ
ApplicationContextµÄ×÷ÓúÍÇø±ð
×÷Óãº
1. BeanFactory¸ºÔð¶ÁÈ¡beanÅäÖÃÎĵµ£¬¹ÜÀíbeanµÄ¼ÓÔØ£¬ÊµÀý»¯£¬Î¬»¤beanÖ®¼äµÄÒÀÀµ¹Øϵ£¬¸ºÔðbeanµÄÉùÃ÷ÖÜÆÚ¡£
2. ApplicationContext³ýÁËÌṩÉÏÊöBeanFactoryËùÄÜÌṩµÄ¹¦ÄÜÖ®Í⣬»¹ÌṩÁ˸üÍêÕûµÄ¿ò¼Ü¹¦ÄÜ£º
a. ¹ú¼Ê»¯Ö§³Ö
b. ×ÊÔ´·ÃÎÊ£ºResource rs = ctx. getResource(¡±classpath:config.properties¡±), ¡°file:c:/config.properties¡±
c. ʼþ´«µÝ£ºÍ¨¹ýʵÏÖApplicationContextAware½Ó¿Ú
3. ³£ÓõĻñÈ¡ApplicationContextµÄ·½·¨£º
FileSystemXmlApplicationContext£º´ÓÎļþϵͳ»òÕßurlÖ¸¶¨µÄxmlÅäÖÃÎļþ´´½¨£¬²ÎÊýΪÅäÖÃ
ÎļþÃû»òÎļþÃûÊý×é
ClassPathXmlApplicationContext£º´ÓclasspathµÄxmlÅäÖÃÎļþ´´½¨£¬¿ÉÒÔ´Ójar°üÖжÁÈ¡ÅäÖÃÎļþ
WebApplicationContextUtils£º´ÓwebÓ¦ÓõĸùĿ¼¶ÁÈ¡ÅäÖÃÎļþ£¬ÐèÒªÏÈÔÚweb.xmlÖÐÅäÖ㬿ÉÒÔÅäÖÃ
¼àÌýÆ÷»òÕßservletÀ´ÊµÏÖ
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
ÕâÁ½ÖÖ·½Ê½¶¼Ä¬ÈÏÅäÖÃÎļþΪweb-inf/applicationContext.xml£¬Ò²¿ÉʹÓÃcontext-paramÖ¸¶¨ÅäÖÃÎļþ
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myApplicationContext.xml</param-value>
</context-param>
* ÔÚweb»·¾³ÏÂÈçºÎÅäÖÃapplicationContext.xmlÎļþ
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
»ò£º
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
ͨ¹ýÈçÏ·½·¨È¡³öapplicationContextʵÀý:
ApplicationContext ac=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext);
* ÈçºÎÅäÖÃspring +
struts£¿
ÔÚstruts-config.xml¼ÓÈëÒ»¸ö²å¼þ£¬Í¨¹ýËü¼ÓÔØapplicationContext.xml
ÔÚstruts-config.xmlÐÞ¸Äaction-mapping±ê¼Ç£¬¾ßÌåaction½»¸øÁËDelegateActionProxy?
ͨ¹ýDelegateActionProxy½øÈëÒ»springµÄ»·¾³¡£?
ÔÚspringµÄapplicationContext.xml¼ÓÈë?<bean name=¡±/login¡± class=¡±" singleton=¡±false¡±/>
* Spring ºÍ hibernateµÄÅäÖÃÎļþÖеÄÖ÷ÒªÀàÐÍÓÐÄÄЩ£¿ÈçºÎÅäÖÃ?
ÔÚ
myeclipseÖÐÏȼÓÈëspring»·¾³ÔÙ¼ÓÈëhibernate»·¾³¡£
Èç¹ûspringÓëhibernate
½áºÏÔÚÒ»Æð¿ÉÒÔ²»ÐèÒªhibernate.cfg.xmlÎļþÊÇ·ñÕýÈ·?
spring+hibernateµÄÅäÖÃÎļþÖеÄÖ÷ÒªÀàÓÐÄÇЩ?ÈçºÎÅäÖÃ?
dataSource
sessionFactory:hibernate.cfg.xml
transactionManager
userDao (extends HibernateDaoSupport)
sessionFactory
facade
proxy
sessionFactory
transactionManager
facade
* ÔÚspringÖÐÈçºÎÅäÖÃÈÝÆ÷µÄÊÂÎï¹ÜÀí£¬Ïà¹ØµÄÀàÓÐÄÄЩ?
Datasouce
transactionManager
userDaoҪעÈë
Datasouce
Proxy´úÀí
Target:userDao£º´úÀí¶ÔÏó(Ä¿±ê¶ÔÏó)
transactionAttributes(ÄÇЩ·½·¨ÐèÒªÊÂÎñ´¦Àí)
transactionManager(ÊÂÎñ´¦Àí·þÎñ)
* ÔÚspringÖÐÈçºÎÅä´úÂëµÄÊÂÎñ¹ÜÀíÆ÷
Datasouce
transactionManager
userDaoҪעÈë
Datasouce
transactionManager
ͨ¹ýÈçÏÂÀàʵÏÖ
TransactionTemplate
JdbcTemplate
* SpringÖÐÓм¸ÖÖÊÂÎï¹ÜÀí£¬·Ö±ðÊÇʲô?
´úÂë¹ÜÀíµÄÊÂÎñ´¦Àí
TransactonTemplateµÄexecute·½·¨ÖеÄÄÚ²¿ÀàTransactionCallbackÖеÄdoInTransaction·½·¨ÖÐʹÓá£
public void make()
{
TransactionTemplate jtm=new TransactionTemplate(this.getTransactionManager());
jtm.execute(new myClass1());
}
public class myClass1 implements TransactionCallback
{
public Object doInTransaction(TransactionStatus trans)
{
JdbcTemplate jdbc=new JdbcTemplate(dataSource);
jdbc.execute(¡±insert into customer(customerName) values(¡¯b')¡±);
jdbc.execute(¡±insert into customer(customerName) values(¡¯b')¡±);
return null;
}
}
ÈÝÆ÷¹ÜÀíµÄÊÂÎñ´¦Àí
* springÖеÄjdbcÓ봫ͳµÄjdbcÓÐʲôÇø±ð?
SpringµÄjdbc:½ÚÊ¡´úÂ룬²»¹ÜÁ¬½Ó(Connection)£¬²»¹ÜÊÂÎñ¡¢²»¹ÜÒì³£¡¢²»¹Ü¹Ø±Õ(con.close() ps.close )
JdbcTemplate(dataSource):Ôö¡¢É¾¡¢¸Ä¡¢²é
TransactionTemplate(transactionManager):½øÐÐÊÂÎñ´¦Àí
* SpringÅäÖõÄÖ÷Òª±êÇ©ÓÐʲô?ÓÐʲô×÷ÓÃ?
<beans>
<bean id=¡±¡± class=¡±¡± init=¡±¡± destroy=¡±¡± singleton=¡±¡±>
<property name=¡±¡±>
<value></value>
</property>
<property name=¡±¡±>
<ref local></ref>
</property>
</bean>
</beans>
* ÈçºÎÔÚspringÖÐʵÏÖ¹ú¼Ê»¯?
ÔÚapplicationContext.xml¼ÓÔØÒ»¸öbean
<bean id=¡±messageSource¡± class=¡±org.springframework.context.support.ResourceBundleMessageSource¡±>
<property name=¡±basename¡±>
<value>message</value>
</property>
</bean>
ÔÚsrcĿ¼Ï½¨¶à¸öpropertiesÎļþ?
¶ÔÓÚ·ÇÓ¢ÎĵÄÒªÓÃnative2ascii? -encoding gb2312 Ô´ Ŀת»¯ÎļþÏà¹ØÄÚÈÝ
ÆäÃüÃû¸ñʽÊÇmessage_ÓïÑÔ_¹ú¼Ò¡£?
Ò³ÃæÖеÄÖÐÏÔʾÌáʾÐÅÏ¢£¬¼üÃûÈ¡¼üÖµ¡£?
µ±¸ø¶¨¹ú¼Ò£¬ÏµÍ³»á×Ô¶¯¼ÓÔضÔÓ¦µÄ¹ú¼ÒµÄpropertiesÐÅÏ¢¡£?
ͨ¹ýapplictionContext.getMessage(¡°¼üÃû¡±,¡±²ÎÊý¡±,¡±ÇøÓò¡±)È¡³öÏà¹ØµÄÐÅÏ¢¡£?
* ÔÚspringÖÐÈçºÎʵÏÖ
ʼþ´¦Àí
ʼþ
Extends ApplicationEvent
¼àÌýÆ÷
Implements ApplicationListener
ʼþÔ´
Implements ApplicationContextAware
ÔÚapplicationContext.xmlÖÐÅäÖÃʼþÔ´¡¢¼àÌýÆ÷
Ïȵõ½Ê¼þÔ´£¬µ÷ÓÃʼþÔ´µÄ·½·¨£¬Í¨Öª¼àÌýÆ÷¡£
* ÈçºÎ½«spring¼ÓÈëwebÈÝÆ÷ÖÐ
ÔÚweb.xmlÖмÓÈëÈçÏÂͬÈÝ,ÔÚÆô¶¯web·þÎñÆ÷ʱ¼ÓÔØ/WEB-INF/applicationContext.xmlÖеÄÄÚÈÝ¡£
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
ͨ¹ýÈçÏÂÀàµÃµ½ApplicationContextʵÀý
WebApplicationContextUtils.getWebApplicationContext
* SpringÈçºÎʵÏÖ×ÊÔ´¹ÜÀí?
ʹÓÃ
applicationContext.getResource(¡°classpath:ÎļþÃû¡±):ÔÚsrc¸ùĿ¼Ï£¬ÔÚÀà·¾¶ÏÂ
applicationContext.getResource(¡°classpath:/chap01/ÎļþÃû¡±): ÒÔsrc¸ùĿ¼ÏµĻù×¼ÍùÏÂ×ß¡£
applicationContext.getResource(¡°file:c:/a.properties¡±)£ºÔÚϵͳÎļþĿ¼Ï¡£
* SpringµÄApplicationContextµÄ×÷ÓÃ?
beanFactory
¹ú¼Ê»¯(getMesage)
×ÊÔ´¹ÜÀí:¿ÉÒÔÖ±½Ó¶ÁÈ¡Ò»¸öÎļþµÄÄÚÈÝ(getResource)
¼ÓÈëweb¿ò¼ÜÖÐ(¼ÓÈëÒ»¸öservlet»ò¼àÌýÆ÷)
ʼþ´¦Àí
* springµÄºËÐÄÊÇʲô£¬¸÷ÓÐʲô×÷Óã¿
BeanFactory£º²úÉúÒ»¸öеÄʵÀý£¬¿ÉÒÔʵÏÖ
µ¥Àýģʽ
BeanWrapper£ºÌṩͳһµÄget¼°set·½·¨
ApplicationContext:Ìṩ¿ò¼ÜµÄʵÏÖ£¬°üÀ¨BeanFactoryµÄËùÓй¦ÄÜ
* SpringÖÐaopµÄ¹Ø¼üÃû´ÊÓÐÄÄЩ£¿¸÷ÓÐʲô×÷ÓÃ?
À¹½ØÆ÷: ´úÀí
×°±¸(advice)
Ä¿±ê¶ÔÏó
¹ØÇеã:Ìõ¼þ
Á¬½Óµã:·½·¨¡¢ÊôÐÔ
* SpringÓëstrutsµÄÇø±ð£¿
strusts£ºÊÇÒ»ÖÖ»ùÓÚMVCģʽµÄÒ»¸öweb²ãµÄ´¦Àí¡£
Spring:ÌṩÁËͨÓõķþÎñ£¬ioc/di aop,¹ØÐĵIJ»½ö½öweb²ã£¬Ó¦µ±j2eeÕûÌåµÄÒ»¸ö·þÎñ£¬¿ÉÒÔºÜÈÝÒ×
Èںϲ»Í¬µÄ¼¼Êõstruts hibernate ibatis ejb remote springJDBC springMVC
* springÓëstrutsµÄÃæÊÔÌâ
1.struts
ActionÊDz»ÊÇ
Ḭ̈߳²È«µÄ£¿Èç¹û²»ÊÇ£¬ÓÐʲô·½Ê½¿ÉÒÔ±£Ö¤ActionµÄ
Ḭ̈߳²È«£¿Èç¹ûÊÇ£¬ËµÃ÷ÔÒò
2.MVC£¬·ÖÎöÒ»ÏÂstrutsÊÇÈçºÎʵÏÖMVCµÄ
3.strutsÖеÄ
¼¸¸ö¹Ø¼ü¶ÔÏóµÄ×÷ÓÃ(˵˵¼¸¸ö¹Ø¼ü¶ÔÏóµÄ×÷ÓÃ)
4.spring
˵˵AOPºÍIOCµÄ¸ÅÄîÒÔ¼°ÔÚspringÖÐÊÇÈçºÎÓ¦ÓõÄ
5.HibernateÓÐÄļ¸ÖÖ²éѯÊý¾ÝµÄ·½Ê½
6.load()ºÍget()µÄÇø±ð
»Ø´ð£º
1.Struts1 ActionÊǵ¥Àýģʽ²¢ÇÒ±ØÐëÊÇḬ̈߳²È«µÄ£¬ÒòΪ½öÓÐActionµÄÒ»¸öʵÀýÀ´´¦ÀíËùÓеÄÇëÇó¡£µ¥Àý²ßÂÔ
ÏÞÖÆÁËStruts1 ActionÄÜ×÷µÄÊ£¬²¢ÇÒÒªÔÚ¿ª·¢Ê±ÌرðСÐÄ¡£Action×ÊÔ´±ØÐëÊÇḬ̈߳²È«µÄ»ò
ͬ²½µÄ¡£
Struts2 Action¶ÔÏóΪÿһ¸öÇëÇó²úÉúÒ»¸öʵÀý£¬Òò´ËûÓÐḬ̈߳²È«ÎÊÌâ¡££¨Êµ¼ÊÉÏ£¬servletÈÝÆ÷¸øÿ¸öÇëÇó²úÉúÐí¶à¿É¶ªÆúµÄ¶ÔÏ󣬲¢ÇÒ²»»áµ¼ÖÂÐÔÄܺÍÀ¬»ø»ØÊÕÎÊÌ⣩
2.strutsÊÇÓÃÒ»×éÀà,servlet ºÍjsp¹æ·¶ÊµÏÖmvcµÄ
3.ActionFrom ActionServlet Action struts-config.xml
4.springµÄºËÐľÍÊÇIOC,ͨ¹ýÖ¸¶¨¶ÔÏóµÄ´´½¨°ì·¨,ÃèÊö¶ÔÏóÓë·þÎñÖ®¼äµÄ¹Øϵ,¶ø²»Éú³É¶ÔÏó
5.3ÖÖ,hql Ìõ¼þ²éѯ() ÔÉúsql
6.load()·½·¨ÈÏΪ¸ÃÊý¾ÝÒ»¶¨´æÔÚ,¿ÉÒÔ·ÅÐĵÄʹÓôúÀíÀ´ÑÓʱ¼ÓÔØ ,Èç¹ûʹÓùý³ÌÖÐ
·¢ÏÖÁËÎÊÌâ,¾ÍÅ׳öÒì³£;
get()·½·¨Ò»¶¨Òª»ñÈ¡µ½ÕæʵµÄÊý¾Ý,·ñÔò·µ»Ønull
* Spring, hibernate ,strutsÃæÊÔÌâ
Hibernate¹¤×÷ÔÀí¼°ÎªÊ²Ã´ÒªÓã¿
ÔÀí£º
1. ¶ÁÈ¡²¢
½âÎöÅäÖÃÎļþ
2. ¶ÁÈ¡²¢½âÎöÓ³ÉäÐÅÏ¢£¬´´½¨SessionFactory
3. ´ò¿ªSesssion
4. ´´½¨ÊÂÎñTransation
5. ³Ö¾Ã»¯²Ù×÷
6. Ìá½»ÊÂÎñ
7. ¹Ø±ÕSession
8. ¹Ø±ÕSesstionFactory
ΪʲôҪÓãº
* ¶ÔJDBC·ÃÎÊÊý¾Ý¿âµÄ´úÂë×öÁË·â×°£¬´ó´ó¼ò»¯ÁËÊý¾Ý·ÃÎʲ㷱ËöµÄÖظ´ÐÔ´úÂë¡£
* HibernateÊÇÒ»¸ö»ùÓÚJDBCµÄÖ÷Á÷³Ö¾Ã»¯¿ò¼Ü£¬ÊÇÒ»¸öÓÅÐãµÄORMʵÏÖ¡£ËûºÜ´ó³Ì¶ÈµÄ¼ò»¯DAO²ãµÄ
±àÂ빤×÷
* hibernateʹÓÃJava
·´Éä»úÖÆ£¬¶ø²»ÊÇ×Ö½ÚÂëÔöÇ¿³ÌÐòÀ´ÊµÏÖ͸Ã÷ÐÔ¡£
* hibernateµÄÐÔÄܷdz£ºÃ£¬ÒòΪËüÊǸöÇáÁ¿¼¶¿ò¼Ü¡£Ó³ÉäµÄÁé»îÐԺܳöÉ«¡£ËüÖ§³Ö¸÷ÖÖ¹ØϵÊý¾Ý¿â£¬´ÓÒ»¶ÔÒ»µ½¶à¶Ô¶àµÄ¸÷ÖÖ¸´ÔÓ¹Øϵ¡£
HibernateÊÇÈçºÎÑÓ³Ù¼ÓÔØ?
* Hibernate2ÑÓ³Ù¼ÓÔØʵÏÖ£ºa)ʵÌå¶ÔÏó b)¼¯ºÏ£¨Collection£©
* Hibernate3 ÌṩÁËÊôÐÔµÄÑÓ³Ù¼ÓÔع¦ÄÜ
µ±HibernateÔÚ²éѯÊý¾ÝµÄʱºò£¬Êý¾Ý²¢Ã»ÓдæÔÚÓë
ÄÚ´æÖУ¬µ±³ÌÐòÕæÕý¶ÔÊý¾ÝµÄ²Ù×÷ʱ£¬¶ÔÏó²Å´æÔÚÓëÄÚ´æÖУ¬¾ÍʵÏÖÁËÑÓ³Ù¼ÓÔØ£¬Ëû½ÚÊ¡ÁË·þÎñÆ÷µÄÄڴ濪Ïú£¬´Ó¶øÌá¸ßÁË·þÎñÆ÷µÄÐÔÄÜ¡£
HibernateÖÐÔõÑùʵÏÖÀàÖ®¼äµÄ¹Øϵ?(È磺һ¶Ô¶à¡¢¶à¶Ô¶àµÄ¹Øϵ)
ÀàÓëÀàÖ®¼äµÄ¹ØϵÖ÷ÒªÌåÏÖÔÚ±íÓë±íÖ®¼äµÄ¹Øϵ½øÐвÙ×÷£¬ËüÃǶ¼ÊжԶÔÏó½øÐвÙ×÷£¬ÎÒÃdzÌÐòÖаÑËùÓеıíÓëÀ඼ӳÉäÔÚÒ»Æð£¬ËüÃÇͨ¹ýÅäÖÃÎļþÖеÄ
many-to-one¡¢one-to-many¡¢many-to-many¡¢
˵ÏÂHibernateµÄ
»º´æ»úÖÆ
* ÄÚ²¿»º´æ´æÔÚHibernateÖÐÓÖ½ÐÒ»¼¶»º´æ£¬ÊôÓÚÓ¦ÓÃÊÂÎV»º´æ
* ¶þ¼¶»º´æ£º
a)Ó¦Óü°»º´æ
b)·Ö²¼Ê½»º´æ
Ìõ¼þ£ºÊý¾Ý²»»á±»µÚÈý·½Ð޸ġ¢Êý¾Ý´óСÔڿɽÓÊÜ·¶Î§¡¢Êý¾Ý¸üÐÂƵÂʵ͡¢Í¬Ò»Êý¾Ý±»ÏµÍ³Æµ·±Ê¹Óá¢·Ç ¹Ø¼üÊý¾Ý
c) µÚÈý·½»º´æµÄʵÏÖ
HibernateµÄ
²éѯ·½Ê½
Sql¡¢Criteria,object comptosition
Hql£º
* ÊôÐÔ²éѯ
* ²ÎÊý²éѯ¡¢ÃüÃû²ÎÊý²éѯ
* ¹ØÁª²éѯ
* ·ÖÒ³²éѯ
* ͳ¼Æº¯Êý
ÈçºÎÓÅ»¯Hibernate£¿
* ʹÓÃË«ÏòÒ»¶Ô¶à¹ØÁª£¬²»Ê¹Óõ¥ÏòÒ»¶Ô¶à
* Áé»îʹÓõ¥ÏòÒ»¶Ô¶à¹ØÁª
* ²»ÓÃÒ»¶ÔÒ»£¬Óöà¶Ôһȡ´ú
* ÅäÖöÔÏ󻺴棬²»Ê¹Óü¯ºÏ»º´æ
* Ò»¶Ô¶à¼¯ºÏʹÓÃBag,¶à¶Ô¶à¼¯ºÏʹÓÃSet
* ¼Ì³ÐÀàʹÓÃÏÔʽ¶à̬
* ±í×Ö¶ÎÒªÉÙ£¬±í¹ØÁª²»ÒªÅ¶࣬Óжþ¼¶»º´æ³ÅÑü
Struts¹¤×÷»úÖÆ£¿ÎªÊ²Ã´ÒªÊ¹ÓÃStruts£¿
¹¤×÷»úÖÆ£º
StrutsµÄ¹¤×÷Á÷³Ì:
ÔÚwebÓ¦ÓÃÆô¶¯Ê±¾Í»á¼ÓÔسõʼ»¯ActionServlet,ActionServlet´Óstruts-config.xmlÎļþÖжÁÈ¡ÅäÖÃÐÅÏ¢,°ÑËüÃÇ´æ·Åµ½¸÷ÖÖÅäÖöÔÏóµ±ActionServlet½ÓÊÕµ½Ò»¸ö¿Í»§ÇëÇóʱ,½«Ö´ÐÐÈçÏÂÁ÷³Ì.
(1)¼ìË÷ºÍÓû§ÇëÇóÆ¥ÅäµÄActionMappingʵÀý,Èç¹û²»´æÔÚ,¾Í·µ»ØÇëÇ󷾶ÎÞЧÐÅÏ¢;
(2)Èç¹ûActionFormʵÀý²»´æÔÚ,¾Í´´½¨Ò»¸öActionForm¶ÔÏó,°Ñ¿Í»§Ìá½»µÄ±íµ¥Êý¾Ý±£´æµ½ActionForm¶ÔÏóÖÐ;
(3)¸ù¾ÝÅäÖÃÐÅÏ¢¾ö¶¨ÊÇ·ñÐèÒª±íµ¥ÑéÖ¤.Èç¹ûÐèÒªÑéÖ¤,¾Íµ÷ÓÃActionFormµÄvalidate()·½·¨;
(4)Èç¹ûActionFormµÄvalidate()·½·¨·µ»Ønull»ò·µ»ØÒ»¸ö²»°üº¬ActionMessageµÄActuibErrors¶ÔÏó, ¾Í±íʾ±íµ¥ÑéÖ¤³É¹¦;
(5)ActionServlet¸ù¾ÝActionMappingËù°üº¬µÄÓ³ÉäÐÅÏ¢¾ö¶¨½«ÇëÇóת·¢¸øÄĸöAction,Èç¹ûÏàÓ¦µÄActionʵÀý²»´æÔÚ,¾ÍÏÈ´´½¨Õâ¸öʵÀý,È»ºóµ÷ÓÃActionµÄexecute()·½·¨;
(6)ActionµÄexecute()·½·¨·µ»ØÒ»¸öActionForward¶ÔÏó,ActionServletÔÚ°Ñ¿Í»§ÇëÇóת·¢¸øActionForward¶ÔÏóÖ¸ÏòµÄ
JSP×é¼þ;
(7)ActionForward¶ÔÏóÖ¸ÏòJSP×é¼þÉú³É¶¯Ì¬ÍøÒ³,·µ»Ø¸ø¿Í»§;
ΪʲôҪÓãº
JSP¡¢Servlet¡¢JavaBean¼¼ÊõµÄ³öÏÖ¸øÎÒÃǹ¹½¨Ç¿´óµÄ
ÆóÒµÓ¦ÓÃϵͳÌṩÁË¿ÉÄÜ¡£µ«ÓÃÕâЩ¼¼Êõ¹¹½¨µÄϵͳ·Ç³£µÄ·±ÂÒ£¬ËùÒÔÔÚ´ËÖ®ÉÏ£¬ÎÒÃÇÐèÒªÒ»¸ö¹æÔò¡¢Ò»¸ö°ÑÕâЩ¼¼Êõ×éÖ¯ÆðÀ´µÄ¹æÔò£¬Õâ¾ÍÊÇ¿ò¼Ü£¬Struts±ãÓ¦Ô˶øÉú¡£
»ùÓÚStruts¿ª·¢µÄÓ¦ÓÃÓÉ3Àà×é¼þ¹¹³É£º¿ØÖÆÆ÷×é¼þ¡¢Ä£ÐÍ×é¼þ¡¢ÊÓͼ×é¼þ
StrutsµÄvalidate¿ò¼ÜÊÇÈçºÎÑéÖ¤µÄ£¿
ÔÚstrutsÅäÖÃÎļþÖÐÅäÖþßÌåµÄ
´íÎóÌáʾ£¬ÔÙÔÚFormBeanÖеÄvalidate()·½·¨¾ßÌåµ÷Óá£
˵ÏÂStrutsµÄÉè¼Æģʽ
MVCģʽ: webÓ¦ÓóÌÐòÆô¶¯Ê±¾Í»á¼ÓÔز¢³õʼ»¯ActionServler¡£Óû§Ìá½»±íµ¥Ê±£¬Ò»¸öÅäÖúõÄActionForm¶ÔÏó±»´´½¨£¬²¢±»ÌîÈë±íµ¥ÏàÓ¦µÄÊý¾Ý£¬ActionServler¸ù¾ÝStruts-config.xml ÎļþÅäÖúõÄÉèÖþö¶¨ÊÇ·ñÐèÒª±íµ¥ÑéÖ¤£¬Èç¹ûÐèÒª¾Íµ÷ÓÃActionFormµÄValidate£¨£©ÑéÖ¤ºóÑ¡Ôñ½«ÇëÇó·¢Ë͵½ÄĸöAction£¬Èç¹û Action²»´æÔÚ£¬ActionServlet»áÏÈ´´½¨Õâ¸ö¶ÔÏó£¬È»ºóµ÷ÓÃActionµÄexecute£¨£©·½·¨¡£Execute£¨£©´Ó ActionForm¶ÔÏóÖлñÈ¡Êý¾Ý£¬Íê³ÉÒµÎñÂß¼£¬·µ»ØÒ»¸öActionForward¶ÔÏó£¬ActionServletÔÙ°Ñ¿Í»§ÇëÇóת·¢¸ø ActionForward¶ÔÏóÖ¸¶¨µÄjsp×é¼þ£¬ActionForward¶ÔÏóÖ¸¶¨µÄjspÉú³É¶¯Ì¬µÄÍøÒ³£¬·µ»Ø¸ø¿Í»§¡£
µ¥Àýģʽ
Factory(¹¤³§Ä£Ê½)£º
¶¨ÒåÒ»¸ö»ùÀà===¡·ÊµÏÖ»ùÀà·½·¨£¨×ÓÀàͨ¹ý²»Í¬µÄ·½·¨£©===¡·¶¨ÒåÒ»¸ö¹¤³§ÀࣨÉú³É×ÓÀàʵÀý£©
===¡·¿ª·¢ÈËÔ±µ÷ÓûùÀà·½·¨
Proxy(
´úÀíģʽ)
spring¹¤×÷»úÖƼ°ÎªÊ²Ã´ÒªÓÃ?
1.spring mvcÇëËùÓеÄÇëÇó¶¼Ìá½»¸øDispatcherServlet,Ëü»áίÍÐÓ¦ÓÃϵͳµÄÆäËûÄ£¿é¸ºÔð¸ºÔð¶ÔÇëÇó½øÐÐÕæÕýµÄ´¦Àí¹¤×÷¡£
2.DispatcherServlet²éѯһ¸ö»ò¶à¸öHandlerMapping,ÕÒµ½´¦ÀíÇëÇóµÄController.
3.DispatcherServletÇëÇëÇóÌá½»µ½Ä¿±êController
4.Controller½øÐÐÒµÎñÂß¼´¦Àíºó£¬»á·µ»ØÒ»¸öModelAndView
5.Dispathcher²éѯһ¸ö»ò¶à¸öViewResolverÊÓͼ½âÎöÆ÷,ÕÒµ½ModelAndView¶ÔÏóÖ¸¶¨µÄÊÓͼ¶ÔÏó
6.ÊÓͼ¶ÔÏó¸ºÔðäÖȾ·µ»Ø¸ø¿Í»§¶Ë¡£
ΪʲôÓãº
AOP Èÿª·¢ÈËÔ±¿ÉÒÔ´´½¨·ÇÐÐΪÐԵĹØ×¢µã£¬³ÆΪºáÇйØ×¢µã£¬²¢½«ËüÃDzåÈëµ½Ó¦ÓóÌÐò´úÂëÖС£Ê¹Óà AOP ºó£¬¹«¹²·þÎñ £¨±È ÈçÈÕÖ¾¡¢³Ö¾ÃÐÔ¡¢ÊÂÎñµÈ£©¾Í¿ÉÒÔ·Ö½â³É·½Ãæ²¢Ó¦Óõ½Óò¶ÔÏóÉÏ£¬Í¬Ê±²»»áÔö¼ÓÓò¶ÔÏóµÄ¶ÔÏóÄ£Ð͵ĸ´ÔÓÐÔ¡£
IOC ÔÊÐí´´½¨Ò»¸ö¿ÉÒÔ¹¹Ôì¶ÔÏóµÄÓ¦Óû·¾³£¬È»ºóÏòÕâЩ¶ÔÏó´«µÝËüÃǵÄÐ×÷¶ÔÏó¡£ÕýÈç
µ¥´Ê µ¹Öà Ëù±íÃ÷µÄ£¬IOC ¾ÍÏñ·´ ¹ýÀ´µÄ JNDI¡£Ã»ÓÐʹÓÃÒ»¶Ñ³éÏ󹤳§¡¢·þÎñ¶¨Î»Æ÷¡¢µ¥ÔªËØ£¨singleton£©ºÍÖ±½Ó¹¹Ô죨straight construction£©£¬Ã¿Ò»¸ö¶ÔÏó¶¼ÊÇÓà ÆäÐ×÷¶ÔÏó¹¹ÔìµÄ¡£Òò´ËÊÇÓÉÈÝÆ÷¹ÜÀíÐ×÷¶ÔÏó£¨collaborator£©¡£
Spring¼´Ê¹Ò»¸öAOP¿ò¼Ü£¬Ò²ÊÇÒ»IOCÈÝÆ÷¡£ Spring ×îºÃµÄµØ·½ÊÇËüÓÐÖúÓÚÄúÌæ»»¶ÔÏó¡£ÓÐÁË Spring£¬Ö»ÒªÓà JavaBean ÊôÐÔºÍÅäÖÃÎļþ¼ÓÈëÒÀÀµÐÔ£¨Ð×÷¶ÔÏ󣩡£È»ºó¿ÉÒÔºÜÈÝÒ×µØÔÚÐèҪʱÌæ»»¾ßÓÐÀàËƽӿڵÄÐ×÷¶ÔÏó¡£
* һЩspringÓëhibernateµÄÃæÊÔÌâ
1¡¢ ¼òÊöÄã¶ÔIoC£¨Inversion of Control£©µÄÀí½â£¬ÃèÊöÒ»ÏÂSpringÖÐʵÏÖDI£¨Dependency Injection£©µÄ¼¸ÖÖ·½Ê½¡£
2¡¢ SpringµÄBeanÓжàÖÖ×÷ÓÃÓò£¬°üÀ¨£º
singleton¡¢prototype¡¢request¡¢session¡¢global session¡¢application¡¢
×Ô¶¨Òå
3¡¢ ¼òµ¥ÃèÊöSpring frameworkÓëStrutsµÄ²»Í¬Ö®´¦£¬ÕûºÏSpringÓëStrutsÓÐÄÄЩ·½·¨£¬ÄÄÖÖ×îºÃ£¬ÎªÊ²Ã´£¿
4¡¢ HibernateÖеÄupdate()ºÍsaveOrUpdate()µÄÇø±ð
5¡¢ Spring¶Ô¶àÖÖORM¿ò¼ÜÌṩÁ˺ܺõÄÖ§³Ö£¬¼òµ¥ÃèÊöÔÚSpringÖÐʹÓÃHibernateµÄ·½·¨£¬²¢½áºÏÊÂÎñ¹ÜÀí¡£
´ð°¸£º
1¡¢ºÃÀ³ÎëÔÔò€€€€²»Òª´òµç»°ÕÒÎÒ£¬ÎÒ»á´ò¸øÄãµÄ¡£IoC½«´´½¨µÄÖ°Ôð´ÓÓ¦ÓóÌÐò´úÂë°áµ½ÁË¿ò¼ÜÖС£Spring¶ÔSetter×¢ÈëºÍ¹¹Ôì·½·¨×¢ÈëÌṩ֧³Ö¡££¨Ïê¼ûhttp://martinfowler.com/articles/injection.html£¬ÒÔ¼°http: //www.redsaga.com/spring_ref/2.0/html/beans.html#beans-factory- collaborators£©
2¡¢ ³ýapplication£¨Ïê¼ûSpring framework 2.0 ReferenceµÄ3.4½ÚbeanµÄ×÷ÓÃÓò£©
3¡¢ SpringÊÇÍêÕûµÄһվʽ¿ò¼Ü£¬¶øStruts½öÊÇMVC¿ò¼Ü£¬ÇÒ×ÅÖØÓÚMVCÖеÄC¡£SpringÓÐ
ÈýÖÖ·½Ê½ÕûºÏStruts£ºÊ¹Óà Spring µÄ ActionSupport ÀàÕûºÏ Struts£»Ê¹Óà Spring µÄ DelegatingRequestProcessor ¸²¸Ç Struts µÄ RequestProcessor£»½« Struts Action ¹ÜÀíίÍиø Spring ¿ò¼Ü£¬¶¯×÷ίÍÐ×îºÃ¡££¨Ïê¼ûʹÓÃSpring ¸üºÃµØ´¦ÀíStruts ¶¯×÷£©
Spring 2.0ÐÂÔöÒ»ÖÖ·½Ê½£ºAutowiringRequestProcessor¡££¨Ïê¼ûhttp://www.javaeye.com/topic/24239£©
4¡¢ saveOrUpdate()·½·¨¿ÉÒÔʵÏÖupdate()µÄ¹¦ÄÜ£¬µ«»á¶àЩ²½Ö裬¾ßÌåÈçÏ£º
Èç¹û¶ÔÏóÔÚ¸ÃsessionÖÐÒѾ±»³Ö¾Ã»¯£¬²»½øÐвÙ×÷£»
¶ÔÏóµÄ±êʶ·ûÊôÐÔ(identifier property)ÔÚÊý¾Ý¿âÖв»´æÔÚ»òÕßÊǸöÔÝʱµÄÖµ£¬µ÷ÓÃsave()·½·¨±£´æËü£»
Èç¹ûsessionÖеÄÁíÒ»¸ö¶ÔÏóÓÐÏàͬµÄ±êʶ·ûÅ׳öÒ»¸öÒì³££»
ÒÔÉϽԲ»·ûºÏÔòµ÷ÓÃupdate()¸üÐÂÖ®¡£
5¡¢ ÔÚcontextÖж¨ÒåDataSource£¬´´½¨SessionFactoy£¬ÉèÖòÎÊý£»DAOÀà¼Ì³ÐHibernateDaoSupport£¬ÊµÏÖ¾ßÌå½Ó¿Ú£¬´ÓÖлñµÃHibernateTemplate½øÐоßÌå²Ù×÷¡£
ÔÚʹÓÃÖÐÈç¹ûÓöµ½OpenSessionInViewµÄÎÊÌ⣬¿ÉÒÔÌí¼ÓOpenSessionInViewFilter»òOpenSessionInViewInterceptor¡££¨Ïê¼ûSpring framework 2.0 ReferenceµÄ12.2½ÚHibernate£©
ÉùÃ÷ʽÊÂÎñÐèÉùÃ÷ÊÂÎñ¹ÜÀíÆ÷£¬ÔÚcontextÖÐÉèÖÃÖ¸¶¨ÊôÐÔ£¬ÓÃÈ·¶¨ºÍ¡£
* ÔÚspringÖÐÈçºÎ¸ü¼Ó¸ßЧµÄʹÓÃJDBC
ʹÓÃSpring¿ò¼ÜÌṩµÄÄ£°åÀàJdbcTemplete¿ÉÒÔÊÇJDBC¸ü¼Ó¸ßЧ
´úÂëÈçÏ£ºJdbcTemplate template = new JdbcTemplate(myDataSource);
DAOÀàµÄ
Àý×Ó£º
public class StudentDaoJdbc implements StudentDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
more..
}
ÅäÖÃÎļþ£º
<bean id=¡±jdbcTemplate¡± class=¡±org.springframework.jdbc.core.JdbcTemplate¡±>
<property name=¡±dataSource¡±>
<ref bean=¡±dataSource¡±/>
</property>
</bean>
<bean id=¡±studentDao¡± class=¡±StudentDaoJdbc¡±>
<property name=¡±jdbcTemplate¡±>
<ref bean=¡±jdbcTemplate¡±/>
</property>
</bean>
<bean id=¡±courseDao¡± class=¡±CourseDaoJdbc¡±>
<property name=¡±jdbcTemplate¡±>
<ref bean=¡±jdbcTemplate¡±/>
</property>
</bean>
* ÔÚspringÈçºÎ´´½¨Ò»¸öÊý¾Ý
Á¬½Ó³Ø
<bean id=¡±dataSource¡± class=¡±org.apache.commons.dbcp.BasicDataSource¡±>
<property name=¡±driver¡±>
<value>${db.driver}</value>
</property>
<property name=¡±url¡±>
<value>${db.url}</value>
</property>
<property name=¡±username¡±>
<value>${db.username}</value>
</property>
<property name=¡±password¡±>
<value>${db.password}</value>
</property>
</bean>
* ÔÚspringÖÐÈçºÎÅäÖÃÒ»¸öbeanÀ´´ÓJNDIµ½dataSource
<bean id=¡±dataSource¡± class=¡±org.springframework.jndi.JndiObjectFactoryBean¡±>
<property name=¡±jndiName¡±> <value>java:comp/env/jdbc/myDatasource</value>
</property>
</bean>
* Çë½éÉÜÏÂspringÖÐbeanµÄ×÷ÓÃÓò
ÔÚspring2.0֮ǰbeanÖ»ÓÐ2ÖÖ×÷ÓÃÓò¼´£ºsingleton(µ¥Àý)¡¢non-singleton£¨Ò²³Æ prototype£©£¬Spring2.0ÒÔºó£¬Ôö¼ÓÁËsession¡¢request¡¢global sessionÈýÖÖרÓÃÓÚWebÓ¦ÓóÌÐòÉÏÏÂÎĵÄBean¡£Òò´Ë£¬Ä¬ÈÏÇé¿öÏÂSpring2.0ÏÖÔÚÓÐÎåÖÖÀàÐ͵ÄBean¡£
<bean id=¡±role¡± class=¡±spring.chapter2.maryGame.Role¡± scope=¡±singleton¡±/>
ÕâÀïµÄscope¾ÍÊÇÓÃÀ´ÅäÖÃspring beanµÄ×÷ÓÃÓò£¬Ëü±êʶbeanµÄ×÷ÓÃÓò¡£
ÔÚspring2.0֮ǰbeanÖ»ÓÐ2ÖÖ×÷ÓÃÓò¼´£ºsingleton(µ¥Àý)¡¢non-singleton£¨Ò²³Æ prototype£©£¬Spring2.0ÒÔºó£¬Ôö¼ÓÁËsession¡¢request¡¢global sessionÈýÖÖרÓÃÓÚWebÓ¦ÓóÌÐòÉÏÏÂÎĵÄBean¡£Òò´Ë£¬Ä¬ÈÏÇé¿öÏÂSpring2.0ÏÖÔÚÓÐÎåÖÖÀàÐ͵ÄBean¡£µ±È»£¬Spring2.0¶Ô BeanµÄÀàÐ͵ÄÉè¼Æ½øÐÐÁËÖع¹£¬²¢Éè¼Æ³öÁé»îµÄBeanÀàÐÍÖ§³Ö£¬ÀíÂÛÉÏ¿ÉÒÔÓÐÎÞÊý¶àÖÖÀàÐ͵ÄBean£¬Óû§¿ÉÒÔ¸ù¾Ý×Ô¼ºµÄÐèÒª£¬Ôö¼ÓеÄBeanÀàÐÍ£¬Âú×ã
ʵ¼ÊÓ¦ÓÃÐèÇó¡£
1¡¢singleton×÷ÓÃÓò
µ±Ò»¸öbeanµÄ×÷ÓÃÓòÉèÖÃΪsingleton£¬ÄÇôSpring IOCÈÝÆ÷ÖÐÖ»»á´æÔÚÒ»¸ö¹²ÏíµÄbeanʵÀý£¬²¢ÇÒËùÓжÔbeanµÄÇëÇó£¬Ö»ÒªidÓë¸Ãbean¶¨ÒåÏàÆ¥Å䣬ÔòÖ»»á·µ»ØbeanµÄͬһʵÀý¡£»»ÑÔÖ®£¬µ±°ÑÒ»¸öbean¶¨ÒåÉèÖÃΪsingleton×÷ÓÃÓòʱ£¬Spring IOCÈÝÆ÷Ö»»á´´½¨¸Ãbean¶¨ÒåµÄΨһʵÀý¡£Õâ¸öµ¥Ò»ÊµÀý»á±»´æ´¢µ½µ¥Àý»º´æ£¨singleton cache£©ÖУ¬²¢ÇÒËùÓÐÕë¶Ô¸ÃbeanµÄºóÐøÇëÇóºÍÒýÓö¼½«·µ»Ø±»»º´æµÄ¶ÔÏóʵÀý£¬ÕâÀïҪעÒâµÄÊÇsingleton×÷ÓÃÓòºÍGOFÉè¼ÆģʽÖеĵ¥ÀýÊÇÍêÈ«²»Í¬µÄ£¬µ¥ÀýÉè¼Æģʽ±íʾһ¸öClassLoaderÖÐÖ»ÓÐÒ»¸öclass´æÔÚ£¬¶øÕâÀïµÄsingletonÔò±íʾһ¸öÈÝÆ÷¶ÔÓ¦Ò»¸öbean£¬Ò²¾ÍÊÇ˵µ±Ò»¸öbean±»±êʶΪsingletonʱºò£¬springµÄIOCÈÝÆ÷ÖÐÖ»»á´æÔÚÒ»¸ö¸Ãbean¡£
ÅäÖÃʵÀý£º
<bean id=¡±role¡± class=¡±spring.chapter2.maryGame.Role¡± scope=¡±singleton¡±/>
»òÕß
<bean id=¡±role¡± class=¡±spring.chapter2.maryGame.Role¡± singleton=¡±true¡±/>
2¡¢prototype
prototype×÷ÓÃÓò²¿ÊðµÄbean£¬Ã¿Ò»´ÎÇëÇ󣨽«Æä×¢Èëµ½ÁíÒ»¸öbeanÖУ¬»òÕßÒÔ³ÌÐòµÄ·½Ê½µ÷ÓÃÈÝÆ÷µÄgetBean()·½·¨£©¶¼»á²úÉúÒ»¸öеÄbeanʵÀý£¬Ï൱ÓÚÒ»¸önewµÄ²Ù×÷£¬¶ÔÓÚprototype×÷ÓÃÓòµÄbean£¬ÓÐÒ»µã·Ç³£ÖØÒª£¬ÄǾÍÊÇSpring²»ÄܶÔÒ»¸ö prototype beanµÄÕû¸öÉúÃüÖÜÆÚ¸ºÔð£¬ÈÝÆ÷ÔÚ³õʼ»¯¡¢ÅäÖá¢×°ÊλòÕßÊÇ×°ÅäÍêÒ»¸öprototypeʵÀýºó£¬½«Ëü½»¸ø¿Í»§¶Ë£¬Ëæºó¾Í¶Ô¸ÃprototypeʵÀý²»ÎŲ»ÎÊÁË¡£²»¹ÜºÎÖÖ×÷ÓÃÓò£¬ÈÝÆ÷¶¼»áµ÷ÓÃËùÓжÔÏóµÄ³õʼ»¯ÉúÃüÖÜÆڻص÷·½·¨£¬¶ø¶Ôprototype¶øÑÔ£¬ÈκÎÅäÖúõÄÎö¹¹ÉúÃüÖÜÆڻص÷·½·¨¶¼½«²»»á±»µ÷Óá£Çå³ýprototype×÷ÓÃÓòµÄ¶ÔÏó²¢ÊÍ·ÅÈκÎprototype beanËù³ÖÓеݺ¹ó×ÊÔ´£¬¶¼ÊÇ¿Í»§¶Ë´úÂëµÄÖ°Ôð¡££¨ÈÃSpringÈÝÆ÷Êͷű»singleton×÷ÓÃÓòbeanÕ¼ÓÃ×ÊÔ´µÄÒ»ÖÖ¿ÉÐз½Ê½ÊÇ£¬Í¨¹ýʹÓà beanµÄºóÖÃ
´¦ÀíÆ÷£¬¸Ã´¦ÀíÆ÷³ÖÓÐÒª±»Çå³ýµÄbeanµÄÒýÓᣣ©
ÅäÖÃʵÀý£º
<bean id=¡±role¡± class=¡±spring.chapter2.maryGame.Role¡± scope=¡±prototype¡±/>
»òÕß
<beanid=¡±role¡± class=¡±spring.chapter2.maryGame.Role¡± singleton=¡±false¡±/>
3¡¢request
request±íʾ¸ÃÕë¶Ôÿһ´Î
HTTPÇëÇ󶼻á²úÉúÒ»¸öеÄbean£¬Í¬Ê±¸Ãbean½öÔÚµ±Ç°HTTP requestÄÚÓÐЧ£¬ÅäÖÃʵÀý£º
request¡¢session¡¢global sessionʹÓõÄʱºò£¬Ê×ÏÈÒªÔÚ³õʼ»¯webµÄweb.xmlÖÐ×öÈçÏÂÅäÖãº
Èç¹ûÄãʹÓõÄÊÇServlet 2.4¼°ÒÔÉϵÄwebÈÝÆ÷£¬ÄÇôÄã½öÐèÒªÔÚwebÓ¦ÓõÄ
XMLÉùÃ÷Îļþweb.xmlÖÐÔö¼ÓÏÂÊöContextListener¼´¿É£º
<web-app>
¡
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
¡
</web-app>
Èç¹ûÊÇServlet2.4ÒÔÇ°µÄwebÈÝÆ÷,ÄÇôÄãҪʹÓÃÒ»¸öjavax.servlet.FilterµÄʵÏÖ£º
<web-app>
..
<filter>
<filter-name>requestContextFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>requestContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
¡
</web-app>
½Ó׿ȿÉÒÔÅäÖÃbeanµÄ×÷ÓÃÓòÁË£º
<bean id=¡±role¡± class=¡±spring.chapter2.maryGame.Role¡± scope=¡±request¡±/>
4¡¢session
session×÷ÓÃÓò±íʾ¸ÃÕë¶Ôÿһ´Î
HTTPÇëÇ󶼻á²úÉúÒ»¸öеÄbean£¬Í¬Ê±¸Ãbean½öÔÚµ±Ç°HTTP sessionÄÚÓÐЧ£¬ÅäÖÃʵÀý£º
ÅäÖÃʵÀý£º
ºÍrequestÅäÖÃʵÀýµÄÇ°ÌáÒ»Ñù£¬ÅäÖúÃwebÆô¶¯Îļþ¾Í¿ÉÒÔÈçÏÂÅäÖãº
<bean id=¡±role¡± class=¡±spring.chapter2.maryGame.Role¡± scope=¡±session¡±/>
5¡¢global session
global session×÷ÓÃÓòÀàËÆÓÚ±ê×¼µÄHTTP Session×÷ÓÃÓò£¬²»¹ýËü½ö½öÔÚ»ùÓÚportletµÄwebÓ¦ÓÃÖвÅÓÐ
ÒâÒå¡£Portlet¹æ·¶¶¨ÒåÁË
È«¾ÖSessionµÄ¸ÅÄËü±»ËùÓй¹³Éij¸ö portlet webÓ¦Óõĸ÷ÖÖ²»Í¬µÄportletËù¹²Ïí¡£ÔÚglobal session×÷ÓÃÓòÖж¨ÒåµÄbean±»ÏÞ¶¨ÓÚÈ«¾Öportlet SessionµÄÉúÃüÖÜÆÚ·¶Î§ÄÚ¡£Èç¹ûÄãÔÚwebÖÐʹÓÃglobal session×÷ÓÃÓòÀ´±êʶbean£¬ÄÇô£¬web»á×Ô¶¯µ±³ÉsessionÀàÐÍÀ´Ê¹Óá£
ÅäÖÃʵÀý£º
ºÍrequestÅäÖÃʵÀýµÄÇ°ÌáÒ»Ñù£¬ÅäÖúÃwebÆô¶¯Îļþ¾Í¿ÉÒÔÈçÏÂÅäÖãº
<bean id=¡±role¡± class=¡±spring.chapter2.maryGame.Role¡± scope=¡±global session¡±/>
6¡¢×Ô¶¨Òåbean×°Åä×÷ÓÃÓò
ÔÚspring 2.0ÖÐ×÷ÓÃÓòÊÇ¿ÉÒÔÈÎÒâÀ©Õ¹µÄ£¬Äã¿ÉÒÔ×Ô¶¨Òå×÷ÓÃÓò£¬ÉõÖÁÄãÒ²¿ÉÒÔÖØж¨ÒåÒÑÓеÄ×÷ÓÃÓò£¨µ«ÊÇÄã²»Äܸ²¸ÇsingletonºÍ prototype£©£¬springµÄ×÷ÓÃÓòÓɽӿÚorg.springframework.beans.factory.config.ScopeÀ´¶¨Ò壬×Ô¶¨Òå×Ô¼ºµÄ×÷ÓÃÓòֻҪʵÏָýӿڼ´¿É£¬ÏÂÃæ¸ø¸öʵÀý£º
ÎÒÃǽ¨Á¢Ò»¸öÏ̵߳Äscope£¬¸ÃscopeÔÚ±íʾһ¸öÏß³ÌÖÐÓÐЧ£¬´úÂëÈçÏ£º
publicclass MyScope implements Scope ¡{
privatefinal ThreadLocal
threadScope = new ThreadLocal() ¡{
protected Object initialValue() ¡{
returnnew HashMap();
}
};
public Object get(String name, ObjectFactory objectFactory) ¡{
Map scope = (Map) threadScope.get();
Object object = scope.get(name);
if(object==null) ¡{
object = objectFactory.getObject();
scope.put(name, object);
}
return object;
}
public Object remove(String name) ¡{
Map scope = (Map) threadScope.get();
return scope.remove(name);
}
publicvoid registerDestructionCallback(String name,
Runnable callback) ¡{
}
public String getConversationId() ¡{
// TODO Auto-generated method stub
returnnull;
}
}
* Çë½éÉÜ Ò»ÏÂspringµÄbeanµÄÉúÃüÖÜÆÚ
Ò»¡¢BeanµÄ¶¨Òå
Springͨ³£Í¨¹ýÅäÖÃÎļþ¶¨ÒåBean¡£È磺
<?xml version=¡±1.0¡å encoding=¡±UTF-8¡å?>
<beans xmlns=¡±http://www.springframework.org/schema/beans¡±
xmlns:xsi=¡±http://www.w3.org/2001/XML
Schema-instance¡±
xsi:schemaLocation=¡±http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd¡±>
<bean id=¡±HelloWorld¡± class=¡±com.pqf.beans.HelloWorld¡±>
<property name=¡±msg¡±>
<value>HelloWorld</value>
</property>
</bean>
</beans>
Õâ¸öÅäÖÃÎļþ¾Í¶¨ÒåÁËÒ»¸ö±êʶΪ HelloWorld µÄBean¡£ÔÚÒ»¸öÅäÖÃÎĵµÖпÉÒÔ¶¨Òå¶à¸öBean¡£
¶þ¡¢BeanµÄ³õʼ»¯
ÓÐÁ½ÖÖ·½Ê½³õʼ»¯Bean¡£
1¡¢ÔÚÅäÖÃÎĵµÖÐͨ¹ýÖ¸¶¨init-method ÊôÐÔÀ´Íê³É
ÔÚBeanµÄÀàÖÐʵÏÖÒ»¸ö³õʼ»¯BeanÊôÐԵķ½·¨£¬Èçinit()£¬È磺
public class HelloWorld{
public String msg=null;
public Date date=null;
public void init() {
msg=¡±HelloWorld¡±;
date=new Date();
}
¡¡
}
È»ºó£¬ÔÚÅäÖÃÎļþÖÐÉèÖÃinit-mothodÊôÐÔ£º
<bean id=¡±HelloWorld¡± class=¡±com.pqf.beans.HelloWorld¡± init-mothod=¡±init¡± >
</bean>
2¡¢ÊµÏÖ org.springframwork.beans.factory.InitializingBean½Ó¿Ú
BeanʵÏÖInitializingBean½Ó¿Ú£¬²¢ÇÒÔö¼Ó afterPropertiesSet() ·½·¨£º
public class HelloWorld implement InitializingBean {
public String msg=null;
public Date date=null;
public void afterPropertiesSet() {
msg=¡±ÏòÈ«ÊÀ½çÎʺã¡¡±;
date=new Date();
}
¡¡
}
ÄÇô£¬µ±Õâ¸öBeanµÄËùÓÐÊôÐÔ±»SpringµÄBeanFactoryÉèÖÃÍêºó£¬»á×Ô¶¯µ÷ÓÃafterPropertiesSet()·½·¨¶ÔBean½øÐгõʼ»¯£¬ÓÚÊÇ£¬ÅäÖÃÎļþ¾Í²»ÓÃÖ¸¶¨ init-methodÊôÐÔÁË¡£
Èý¡¢BeanµÄµ÷ÓÃ
ÓÐÈýÖÖ·½Ê½¿ÉÒԵõ½Bean²¢½øÐе÷Óãº
1¡¢Ê¹ÓÃBeanWrapper
HelloWorld hw=new HelloWorld();
BeanWrapper bw=new BeanWrapperImpl(hw);
bw.setPropertyvalue(¡±msg¡±,¡±HelloWorld¡±);
system.out.println(bw.getPropertyCalue(¡±msg¡±));
2¡¢Ê¹ÓÃBeanFactory
InputStream is=new FileInputStream(¡±config.xml¡±);
XmlBeanFactory factory=new XmlBeanFactory(is);
HelloWorld hw=(HelloWorld) factory.getBean(¡±HelloWorld¡±);
system.out.println(hw.getMsg());
3¡¢Ê¹ÓÃApplicationConttext
ApplicationContext actx=new FleSystemXmlApplicationContext(¡±config.xml¡±);
HelloWorld hw=(HelloWorld) actx.getBean(¡±HelloWorld¡±);
System.out.println(hw.getMsg());
ËÄ¡¢BeanµÄÏú»Ù
1¡¢Ê¹ÓÃÅäÖÃÎļþÖÐµÄ destory-method ÊôÐÔ
Óë³õʼ»¯ÊôÐÔ init-methodsÀàËÆ£¬ÔÚBeanµÄÀàÖÐʵÏÖÒ»¸ö³·ÏúBeanµÄ·½·¨£¬È»ºóÔÚÅäÖÃÎļþÖÐͨ¹ý destory-methodÖ¸¶¨£¬ÄÇôµ±beanÏú»Ùʱ£¬Spring½«×Ô¶¯µ÷ÓÃÖ¸¶¨µÄÏú»Ù·½·¨¡£
2¡¢ÊµÏÖ org.springframwork.bean.factory.DisposebleBean½Ó¿Ú
Èç¹ûʵÏÖÁËDisposebleBean½Ó¿Ú£¬ÄÇôSpring½«×Ô¶¯µ÷ÓÃbeanÖеÄDestory·½·¨½øÐÐÏú»Ù£¬ËùÒÔ£¬BeanÖбØÐëÌṩDestory·½·¨¡£
* SpringÖÐÈçºÎ»ñÈ¡bean
ͨ¹ýxmlÅäÖÃÎļþ
beanÅäÖÃÔÚxmlÀïÃ棬springÌṩ¶àÖÖ·½Ê½¶ÁÈ¡ÅäÖÃÎļþµÃµ½ApplicationContext.
µÚÒ»ÖÖ·½Ê½£ºFileSystemXmlApplicationContext
ͨ¹ý³ÌÐòÔÚ³õʼ»¯µÄʱºò£¬µ¼ÈëBeanÅäÖÃÎļþ£¬È»ºóµÃµ½BeanʵÀý:
ApplicationContext ac = new FileSystemXmlApplicationContext(¡±applicationContext.xml¡±)
ac.getBean(¡±beanName¡±);
µÚ¶þÖÖ·½Ê½£ºWebApplicationContextUtil
ÔÚB/SϵͳÖÐ,ͨ³£ÔÚweb.xml³õʼ»¯beanµÄÅäÖÃÎļþ£¬È»ºóÓÉWebAppliCationContextUtilµÃµ½ApplicationContext.ÀýÈ磺
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ÆäÖÐ servletContext sc ¿ÉÒÔ¾ßÌå »»³É servlet.getServletContext()»òÕß this.getServletContext() »òÕß request.getSession().getServletContext();
ÁíÍ⣬ÓÉÓÚspringÊÇ×¢ÈëµÄ¶ÔÏó·ÅÔÚServletContextÖеģ¬ËùÒÔ¿ÉÒÔÖ±½ÓÔÚServletContextÈ¡³öWebApplicationContext ¶ÔÏó£º
WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_
APPLICATION_CONTEXT_ATTRIBUTE);
* Spring¿ò¼ÜÓÐÄļ¸²¿·Ö×é³É?
Spring¿ò¼ÜÓÐÆ߸öÄ£¿é×é³É×é³É£¬Õâ7¸öÄ£¿é(»ò×é¼þ)¾ù¿ÉÒÔµ¥¶À´æÔÚ£¬Ò²¿ÉÒÔÓëÆäËüÒ»¸ö»ò¶à¸öÄ£¿éÁªºÏʹÓã¬Ö÷Òª¹¦ÄܱíÏÖÈçÏ£º
? Spring ºËÐÄÈÝÆ÷£¨Core£©£ºÌṩSpring¿ò¼ÜµÄ»ù±¾¹¦ÄÜ¡£ºËÐÄÈÝÆ÷µÄÖ÷Òª×é¼þÊÇBeanFactory£¬ËýÊǹ¤³§Ä£Ê½µÄʵÏÖ¡£BeanFactoryʹÓÿØÖÆ·´×ª£¨Ioc£©Ä£Ê½½«Ó¦ÓóÌÐòµÄÅäÖúÍÒÀÀµÐԹ淶Óëʵ¼ÊµÄÓ¦ÓôúÂë³ÌÐò·Ö¿ª¡£
? Spring AOP£ºÍ¨¹ýÅäÖùÜÀíÌØÐÔ£¬Spring AOPÄ£¿éÖ±½ÓÃæÏò·½ÃæµÄ±à³Ì¹¦Äܼ¯³Éµ½ÁËSpring¿ò¼ÜÖУ¬ËùÒÔ¿ÉÒÔºÜÈÝÒ×µÄʹSpring¿ò¼Ü¹ÜÀíµÄÈκζÔÏóÖ§³Ö AOP¡£Spring AOPÄ£¿éΪ»ùÓÚSpringµÄÓ¦ÓóÌÐòÖеĶÔÏóÌṩÁËÊÂÎñ¹ÜÀí·þÎñ¡£Í¨¹ýʹÓÃSpring AOP£¬²»ÓÃÒÀÀµÓÚEJB×é¼þ£¬¾Í¿ÉÒÔ½«ÉùÃ÷ÐÔÊÂÎñ¹ÜÀí¼¯³Éµ½Ó¦ÓóÌÐòÖС£
? Spring ORM£ºSpring¿ò¼Ü¼¯³ÉÁËÈô¸ÉORM¿ò¼Ü,´Ó¶øÌṩÁËORMµÄ¶ÔÏó¹Øϵ¹¤¾ß,ÆäÖаüÀ¨ JDO¡¢Hibernate¡¢iBatisºÍTopLink¡£ËùÓÐÕâЩ¶¼×ñ´ÓSpringµÄͨÓÃÊÂÎñºÍDAOÒì³£²ã½á¹¹¡£
? Spring DAO£ºJDBC DAO³éÏó²ãÌṩÁËÓÐÒâÒåµÄÒì³£²ã´ÎµÄ½á¹¹£¬¿ÉÓøýṹÀ´¹ÜÀí
Òì³£´¦ÀíºÍ²»Í¬Êý¾Ý¹©Ó¦ÉÌÅ׳öµÄÒì³£´íÎóÐÅÏ¢¡£Òì³£²ã´Î½á¹¹¼ò»¯ÁË´íÎó´¦Àí£¬²¢ÇÒ´ó´óµÄ½µµÍ ÁËÐèÒª±àдµÄÒì³£´úÂëÊýÁ¿£¨ÀýÈ磬´ò¿ªºÍ¹ØϵÁ¬½Ó£©¡£Spring DAOµÄÃæÏòJDBCµÄÒì³£×ñ´ÓͨÓõÄDAOÒì³£²ã½á¹¹¡£
? Spring WEB£ºWebÉÏÏÂÎÄÄ£¿é½¨Á¢ÔÚÉÏÏÂÎÄÄ£¿é£¨Context£©µÄ»ù´¡Ö®ÉÏ£¬Îª»ùÓÚWeb·þÎñµÄÓ¦ÓóÌÐòÌṩÁËÉÏÏÂÎĵķþÎñ¡£ËùÒÔSpring¿ò¼ÜÖ§³Ö Jakarta StrutsµÄ¼¯³É¡£WebÄ£¿é»¹¼ò»¯ÁË´¦Àí¶à²¿·ÖÇëÇó¼°½«ÇëÇó²ÎÊý°ó¶¨µ½Óò¶ÔÏóµÄ¹¤×÷¡£
? SpringÉÏÏÂÎÄ£¨Context£©£ºSpringÉÏÏÂÎÄÊÇÒ»¸öÅäÖÃÎļþ£¬ÏòSpring¿ò¼ÜÌṩÉÏÏÂÎÄÐÅÏ¢¡£SpringÉÏÏÂÎÄ°üÀ¨ÆóÒµ·þÎñ£¬ÀýÈç JNDI¡¢EJB¡¢µç×ÓÓʼþ¡¢¹ú¼Ê»¯Ð£ÑéºÍµ÷¶È¹¦ÄÜ¡£
? Spring MVC£ºSpringµÄMVC¿ò¼ÜÊÇÒ»¸öÈ«¹¦ÄܵĹ¹½¨WebÓ¦ÓóÌÐòµÄMVCʵÏÖ¡£Í¨¹ý²ßÂÔ½Ó¿Ú£¬MVC¿ò¼Ü±ä³ÉΪ¸ß¶È¿ÉÅäÖõģ¬MVCÈÝÄɵĴóÁ¿ÊÓͼ¼¼Êõ£¬°üÀ¨JSP¡¢Velocity¡¢Tiles¡¢iTextºÍPol
* ʹÓÃspringÓÐʲôºÃ´¦?
¡ôSpringÄÜÓÐЧµØ×éÖ¯ÄãµÄÖмä²ã¶ÔÏó,ÎÞÂÛÄãÊÇ·ñÑ¡ÔñʹÓÃÁËEJB¡£Èç¹ûÄã½ö½öʹÓÃÁËStruts»òÆäËûµÄ°üº¬ÁËJ2EEÌØÓÐAPIsµÄframework£¬Äã»á·¢ÏÖSpring¹Ø×¢ÁËÒÅÁôϵÄÎÊÌ⣬¡£
¡ôSpringÄÜÏû³ýÔÚÐí¶à¹¤³ÌÉ϶ÔSingletonµÄ¹ý¶àʹÓ᣸ù¾ÝÎҵľÑ飬ÕâÊÇÒ»¸öÖ÷ÒªµÄÎÊÌ⣬Ëü¼õÉÙÁËϵͳµÄ¿É²âÊÔÐÔºÍÃæÏò¶ÔÏóÌØÐÔ¡£
¡ôSpringÄÜÏû³ýʹÓø÷ÖÖ¸÷Ñù¸ñʽµÄÊôÐÔ¶¨ÖÆÎļþµÄÐèÒª,ÔÚÕû¸öÓ¦Óú͹¤³ÌÖУ¬¿Éͨ¹ýÒ»ÖÖ Ò»Öµķ½·¨À´½øÐÐÅäÖá£Ôø¾¸Ðµ½ÃÔ»ó£¬Ò»¸öÌض¨ÀàÒª²éÕÒÃԻðãµÄÊôÐÔ
¹Ø¼ü×Ö»òϵͳÊôÐÔ,Ϊ´Ë²»µÃ²»¶ÁJavadocÄËÖÁÔ´±àÂëÂð£¿ÓÐÁËSpring£¬Äã¿É ºÜ¼òµ¥µØ¿´µ½ÀàµÄJavaBeanÊôÐÔ¡£µ¹ÖÿØÖƵÄʹÓÃ(ÔÚÏÂÃæÌÖÂÛ)°ïÖúÍê³ÉÕâÖÖ¼ò»¯¡£
¡ôSpringÄÜͨ¹ý½Ó¿Ú¶ø²»ÊÇÀà´Ù½øºÃµÄ±à³Ì
Ï°¹ß£¬¼õÉÙ±à³Ì´ú¼Ûµ½¼¸ºõΪÁã¡£
¡ôSpring±»Éè¼ÆΪÈÃʹÓÃËü´´½¨µÄÓ¦Óþ¡¿ÉÄÜÉÙµÄÒÀÀµÓÚËûµÄAPIs¡£ÔÚSpringÓ¦ÓÃÖеĴó¶àÊýÒµÎñ¶ÔÏóûÓÐÒÀÀµÓÚSpring¡£
¡ôʹÓÃSpring¹¹½¨µÄÓ¦ÓóÌÐòÒ×ÓÚµ¥Ôª²âÊÔ¡£
¡ôSpringÄÜʹEJBµÄʹÓóÉΪһ¸öʵÏÖÑ¡Ôñ,¶ø²»ÊÇÓ¦Óüܹ¹µÄ±ØȻѡÔñ¡£ÄãÄÜÑ¡ÔñÓÃPOJOs»òlocal EJBsÀ´ÊµÏÖÒµÎñ½Ó¿Ú£¬È´²»»áÓ°Ïìµ÷ÓôúÂë¡£
¡ôSpring°ïÖúÄã½â¾öÐí¶àÎÊÌâ¶øÎÞÐèʹÓÃEJB¡£SpringÄÜÌṩһÖÖEJBµÄÌæ»»ÎËüÃÇÊÊÓÚÐí¶àwebÓ¦Óá£ÀýÈç,SpringÄÜʹÓÃAOPÌṩÉùÃ÷ÐÔÊÂÎñ¶ø²»Í¨¹ýʹÓÃEJBÈÝÆ÷£¬Èç¹ûÄã½ö½öÐèÒªÓëµ¥¸öµÄÊý¾Ý¿â´ò½»µÀ£¬ÉõÖÁ²»ÐèÒªJTAʵÏÖ¡£
¡öSpringΪÊý¾Ý´æÈ¡ÌṩÁËÒ»ÖµĿò¼Ü,²»ÂÛÊÇʹÓÃJDBC»òO/R mapping²úÆ·£¨ÈçHibernate£©¡£
SpringȷʵʹÄãÄÜͨ¹ý×î¼òµ¥¿ÉÐеĽâ¾ö°ì·¨½â¾öÄãµÄÎÊÌâ¡£ÕâЩÌØÐÔÊÇÓкܴó¼ÛÖµµÄ¡£
×ܽáÆðÀ´£¬SpringÓÐÈçÏÂÓŵ㣺
¡ôµÍÇÖÈëʽÉè¼Æ£¬´úÂëÎÛȾ¼«µÍ
¡ô ¶ÀÁ¢ÓÚ¸÷ÖÖ
Ó¦Ó÷þÎñÆ÷£¬¿ÉÒÔÕæÕýʵÏÖWrite Once,Run AnywhereµÄ³Ðŵ
¡ôSpringµÄDI»úÖƽµµÍÁËÒµÎñ¶ÔÏóÌæ»»µÄ¸´ÔÓÐÔ
¡ôSpring²¢²»ÍêÈ«ÒÀÀµÓÚSpring£¬¿ª·¢Õß¿É×ÔÓÉÑ¡ÓÃSpring¿ò¼ÜµÄ²¿·Ö»òÈ«²¿
* ʲôÊÇspring£¬ËüÓÐʲôÌصã?
SpringÊÇÒ»¸öÇáÁ¿¼¶µÄ¿ØÖÆ·´×ª(IoC)ºÍÃæÏòÇÐÃæ(AOP)µÄÈÝÆ÷¿ò¼Ü¡£
¡ôÇáÁ¿¡ª¡ª´Ó´óСÓ뿪ÏúÁ½·½Ãæ¶øÑÔSpring¶¼ÊÇÇáÁ¿µÄ¡£ÍêÕûµÄSpring¿ò¼Ü¿ÉÒÔÔÚÒ»¸ö´óСֻÓÐ1MB¶àµÄJARÎļþÀï·¢²¼¡£²¢ ÇÒSpringËùÐèµÄ´¦Àí¿ªÏúÒ²ÊÇ΢²»×ãµÀµÄ¡£´ËÍ⣬SpringÊÇ·ÇÇÖÈëʽµÄ£ºµäÐ͵أ¬SpringÓ¦ÓÃÖеĶÔÏó²»ÒÀÀµÓÚSpringµÄÌض¨Àà¡£
¡ô¿ØÖÆ·´×ª¡ª¡ªSpringͨ¹ýÒ»ÖÖ³Æ×÷¿ØÖÆ·´×ª£¨IoC£©µÄ¼¼Êõ´Ù½øÁËËÉñîºÏ¡£µ±Ó¦ÓÃIoC£¬Ò»¸ö¶ÔÏóÒÀÀµµÄÆäËü¶ÔÏó»áͨ¹ý±»¶¯µÄ·½Ê½´«µÝ½øÀ´£¬¶ø²»ÊÇÕâ¸ö¶ÔÏó×Ô¼º´´½¨»òÕß²éÕÒÒÀÀµ¶ÔÏó¡£Äã¿ÉÒÔÈÏΪIoCÓëJNDIÏà·´¡ª¡ª²» ÊǶÔÏó´ÓÈÝÆ÷ÖвéÕÒÒÀÀµ£¬¶øÊÇÈÝÆ÷ÔÚ¶ÔÏó³õʼ»¯Ê±²»µÈ¶ÔÏóÇëÇó¾ÍÖ÷¶¯½«ÒÀÀµ´«µÝ¸øËü¡£
¡ôÃæÏòÇÐÃ桪¡ªSpringÌṩÁËÃæÏòÇÐÃæ±à³ÌµÄ·á¸»Ö§³Ö£¬ÔÊÐíͨ¹ý·ÖÀëÓ¦ÓÃµÄ ÒµÎñÂß¼Óëϵͳ¼¶·þÎñ£¨ÀýÈçÉó¼Æ£¨auditing£©ºÍÊÂÎñ£¨£©¹ÜÀí£©½øÐÐÄÚ¾ÛÐԵĿª·¢¡£Ó¦ÓöÔÏóֻʵÏÖËüÃÇÓ¦¸Ã×öµÄ¡ª¡ªÍê³ÉÒµÎñÂß¼¡ª¡ª½ö´Ë¶øÒÑ¡£ËüÃÇ ²¢²»¸ºÔð£¨ÉõÖÁÊÇÒâʶ£©ÆäËüµÄϵͳ¼¶¹Ø×¢µã£¬ÀýÈçÈÕÖ¾»òÊÂÎñÖ§³Ö¡£
¡ôÈÝÆ÷¡ª¡ªSpring°üº¬²¢¹ÜÀíÓ¦ÓöÔÏóµÄÅäÖúÍÉúÃüÖÜÆÚ£¬ÔÚÕâ¸öÒâÒåÉÏËüÊÇ Ò»ÖÖÈÝÆ÷£¬Äã¿ÉÒÔÅäÖÃÄãµÄÿ¸öbeanÈçºÎ±»´´½¨¡ª¡ª»ùÓÚÒ»¸ö¿ÉÅäÖÃÔÐÍ£¨prototype£©£¬ÄãµÄbean¿ÉÒÔ´´½¨Ò»¸öµ¥¶ÀµÄʵÀý»òÕßÿ´ÎÐèҪʱ¶¼Éú ³ÉÒ»¸öеÄʵÀý¡ª¡ªÒÔ¼°ËüÃÇÊÇÈçºÎÏ໥¹ØÁªµÄ¡£È»¶ø£¬Spring²»Ó¦¸Ã±»»ìͬÓÚ´«Í³µÄÖØÁ¿¼¶µÄEJBÈÝÆ÷£¬ËüÃǾ³£ÊÇÅÓ´óÓ뱿Öصģ¬ÄÑÒÔʹÓá£
¡ô¿ò¼Ü¡ª¡ªSpring¿ÉÒÔ½«¼òµ¥µÄ×é¼þÅäÖá¢×éºÏ³ÉΪ¸´ÔÓµÄÓ¦Óá£ÔÚSpringÖУ¬Ó¦ÓöÔÏó±»ÉùÃ÷ʽµØ×éºÏ£¬µäÐ͵ØÊÇÔÚÒ»¸ö
XMLÎļþÀï¡£SpringÒ²ÌṩÁ˺ܶà»ù´¡¹¦ÄÜ£¨ÊÂÎñ¹ÜÀí¡¢³Ö¾Ã»¯¿ò¼Ü¼¯³ÉµÈµÈ£©£¬½«Ó¦ÓÃÂß¼µÄ¿ª·¢Áô¸øÁËÄã¡£