如题所述
Spring 3.1为è¿ä¸ªé®é¢æä¾äºä¸ä¸ªè§£å³æ¹æ¡ï¼å¦æä½ è¿æ²¡æ为èªå·±ç项ç®å级Springçæ¬ï¼å¯ï¼ä½ 麻ç¦å¤§äºï¼ã
ããSpringå¨å®¹å¨ä¸å¼å ¥EnvironmentåProfileçæ¦å¿µãæ¯ä¸ªåºç¨ç¨åºä¸ä¸æé½æä¸ä¸ªé½å¯ä»¥è®¿Environment对象ã
ããClassPathXmlApplicationContext classPathXmlApplicationContext =
ããnew ClassPathXmlApplicationContext();
ããConfigurableEnvironment configurableEnvironment =
ããclassPathXmlApplicationContext.getEnvironment();
ããæ¯ç§è¿è¡ç¯å¢é½æå¾å¤æ´»å¨Profileç±»å¯ä¾ä½¿ç¨ã大å¤æ°è®²è§£Spring Profileçä¾åé½æ¯å¨å¼å模å¼æç产模å¼ä¸ã对äºä¸åè¿è¡ç¯å¢é®é¢æ¥è¯´ï¼æç解å³æ¹æ¡æ¯ä½¿ç¨ä½¿ç¨å¤ä¸ªProfileæ¥éåºä¸åè¿è¡æ¶ãè¿ä¸ªè§£å³æ¹æ¡çä¼å¿æ¯ä½ å¯ä»¥èªè¡å³å®å¦ä½ä½¿ç¨Profileã
ããé»è®¤æ空æ åµä¸ï¼ä½ æå建çBeanå¨è½½å ¥å®¹å¨ä¸åæ¯æ²¡æProfile对象çãä¸é¢çä¸ä¸ªä¾åãå设ä¸é¢æ¯æçåºç¨ç¨åºä¸ï¼æ°æ®æºå®ä¾çå®ä¹ã
ãã<?xml version="1.0" encoding="UTF-8"?>
ãã<beans xmlns="â¦">
ãã<bean id="dataSource" destroy-method="close"
ããclass="org.apache.commons.dbcp.BasicDataSource">
ãã<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
ãã<property name="url" value="jdbc:hsqldb:hsql://localhost"/>
ãã<property name="username" value="sa"/>
ãã<property name="password" value=""/>
ãã</bean>
ãã</beans>
ããå¨Spring 3.0ä¸ï¼å¢å äºä¸ä¸ªæ°ç容å¨ç±»GenericXmlApplicationContext ï¼å¯ä»¥ä½ä¸ºClassPathXmlApplicationContextåFileSystemXmlApplicationContextä¹å¤çå¦ä¸ä¸ªéæ©ã
ããGenericXmlApplicationContextç±»çç¹ç¹æ¯å¯ä»¥éè¿Setteræ¹æ³å®æææçé ç½®ï¼èæ éä¾é 笨éçæé å¨å»å®æé ç½®ãè®°ä½ï¼å¨åå§å容å¨çåå¤å·¥ä½å®æåï¼éè¦è°ç¨refresh()æ¹æ³å®æå®é çåå§åå·¥ä½ã
ããä¸é¢ç代ç å±ç¤ºäºå¦ä½ä½¿ç¨GenericXmlApplicationContextç±»åå§å容å¨ï¼
ããGenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ããctx.getEnvironment().setActiveProfiles("standalone");
ããctx.load("*Context.xml");
ããctx.refresh();
ããè¿éï¼æå°æ´»å¨Profile设置为âstandaloneâãå¨è¿ä¸ªå·¥ç¨éï¼æå¸æ代ç æ¢å¯ä»¥ä½ä¸ºâstandaloneâè¿è¡å¨åºç¨ç¨åºå®¹å¨ä¹å¤ï¼è¿å¯ä»¥ä½ä¸ºâcontainerâè¿è¡å¨å®¹å¨ä¸ãè¿éï¼æå¯ä»¥è®¾ç½®å¤ä¸ªProfileï¼ä¾å¦ï¼ä¸é¢ç代ç 设置äºProfile为âstandaloneâä¸âactivemqâã
ãã1
ããctx.getEnvironment().setActiveProfiles("standalone", "activemq");
ããè½ç¶åäºä¸é¢çé ç½®ï¼å®é ä¸å¹¶ä¸ä¼å¯¹å½åçé ç½®ä¸ä¸æ产çå½±åï¼å 为è¿æ²¡æé ç½®Profileå®ä¾ãæ以ï¼ä¿®æ¹é ç½®ä¸ä¸æ为ï¼
ãã<?xml version="1.0" encoding="UTF-8"?>
ãã<beans xmlns="â¦" profile="standalone">
ãã<bean id="dataSource" destroy-method="close"
ããclass="org.apache.commons.dbcp.BasicDataSource">
ãã<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
ãã<property name="url" value="jdbc:hsqldb:hsql://localhost"/>
ãã<property name="username" value="sa"/>
ãã<property name="password" value=""/>
ãã</bean>
ãã</beans>
ããåªæå½æ´»å¨Profile设置为âstandaloneâæ¶ï¼æä¼å®ä¾åè¿ä¸ªBeanãProfileæ¯Beançå±æ§ï¼èä¸æ¯ä¸ä¸ªå®ä¾å¯¹è±¡ï¼å æ¤ï¼ä½ æ æ³é ç½®åç¬çBeanæ¥éæ©Profileãå¨è¾æ©çSpringçæ¬ä¸ï¼è¿ä¼å¯¼è´äº§çå¤ä¸ªæ件ï¼èAntçéé 符æ æ³å¨è¿è¡æ¶æ¾å°æ£ç¡®çé ç½®æ件ã
ããå¨Spring 3.1ä¸ï¼<beans/>æ ç¾å¯ä»¥åµå¥å¨<beans/>æ ç¾å ãç°å¨ï¼æ们éæ°ç¼åä¸ä¸æ°æ®æºé ç½®æ件ï¼
ãã<?xml version="1.0" encoding="UTF-8"?>
ãã<beans xmlns="â¦">
ãã<beans profile="standalone">
ãã<bean id="dataSource">
ãã<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
ãã<property name="url" value="jdbc:hsqldb:hsql://localhost"/>
ãã<property name="username" value="sa"/>
ãã<property name="password" value=""/>
ãã</bean>
ãã</beans>
ãã
ãã<beans profile="container">
ãã<jee:jndi-lookup id="dataSource" jndi-name="java:mydatasource"/>
ãã</beans>
ãã</beans>
ããè¿æ ·ï¼å°±å¯ä»¥éè¿ä¸é¢ç代ç å¿«éåæ¢Profileï¼
ããctx.getEnvironment().setActiveProfiles("container");
ããå¦ä¸ç§åæ¢Profileçæ¹æ³æ¯å¨è¿è¡æ¶ä½ä¸ºç³»ç»åæ°ä¼ å ¥ï¼
ãã-Dspring.profiles.active="standalone"
ããæ¤å¤ï¼ä¹å¯ä»¥ä½ä¸ºEar/Warçåå§ååæ°ä¼ å ¥ï¼
ãã<servlet>
ãã<servlet-name>dispatcher</servlet-name>
ãã<servlet-class>
ããorg.springframework.web.servlet.DispatcherServlet
ãã</servlet-class>
ãã<init-param>
ãã<param-name>spring.profiles.active</param-name>
ãã<param-value>production</param-value>
ãã</init-param>
ãã</servlet>
ããSpringå¨å®¹å¨ä¸å¼å ¥EnvironmentåProfileçæ¦å¿µãæ¯ä¸ªåºç¨ç¨åºä¸ä¸æé½æä¸ä¸ªé½å¯ä»¥è®¿Environment对象ã
ããClassPathXmlApplicationContext classPathXmlApplicationContext =
ããnew ClassPathXmlApplicationContext();
ããConfigurableEnvironment configurableEnvironment =
ããclassPathXmlApplicationContext.getEnvironment();
ããæ¯ç§è¿è¡ç¯å¢é½æå¾å¤æ´»å¨Profileç±»å¯ä¾ä½¿ç¨ã大å¤æ°è®²è§£Spring Profileçä¾åé½æ¯å¨å¼å模å¼æç产模å¼ä¸ã对äºä¸åè¿è¡ç¯å¢é®é¢æ¥è¯´ï¼æç解å³æ¹æ¡æ¯ä½¿ç¨ä½¿ç¨å¤ä¸ªProfileæ¥éåºä¸åè¿è¡æ¶ãè¿ä¸ªè§£å³æ¹æ¡çä¼å¿æ¯ä½ å¯ä»¥èªè¡å³å®å¦ä½ä½¿ç¨Profileã
ããé»è®¤æ空æ åµä¸ï¼ä½ æå建çBeanå¨è½½å ¥å®¹å¨ä¸åæ¯æ²¡æProfile对象çãä¸é¢çä¸ä¸ªä¾åãå设ä¸é¢æ¯æçåºç¨ç¨åºä¸ï¼æ°æ®æºå®ä¾çå®ä¹ã
ãã<?xml version="1.0" encoding="UTF-8"?>
ãã<beans xmlns="â¦">
ãã<bean id="dataSource" destroy-method="close"
ããclass="org.apache.commons.dbcp.BasicDataSource">
ãã<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
ãã<property name="url" value="jdbc:hsqldb:hsql://localhost"/>
ãã<property name="username" value="sa"/>
ãã<property name="password" value=""/>
ãã</bean>
ãã</beans>
ããå¨Spring 3.0ä¸ï¼å¢å äºä¸ä¸ªæ°ç容å¨ç±»GenericXmlApplicationContext ï¼å¯ä»¥ä½ä¸ºClassPathXmlApplicationContextåFileSystemXmlApplicationContextä¹å¤çå¦ä¸ä¸ªéæ©ã
ããGenericXmlApplicationContextç±»çç¹ç¹æ¯å¯ä»¥éè¿Setteræ¹æ³å®æææçé ç½®ï¼èæ éä¾é 笨éçæé å¨å»å®æé ç½®ãè®°ä½ï¼å¨åå§å容å¨çåå¤å·¥ä½å®æåï¼éè¦è°ç¨refresh()æ¹æ³å®æå®é çåå§åå·¥ä½ã
ããä¸é¢ç代ç å±ç¤ºäºå¦ä½ä½¿ç¨GenericXmlApplicationContextç±»åå§å容å¨ï¼
ããGenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ããctx.getEnvironment().setActiveProfiles("standalone");
ããctx.load("*Context.xml");
ããctx.refresh();
ããè¿éï¼æå°æ´»å¨Profile设置为âstandaloneâãå¨è¿ä¸ªå·¥ç¨éï¼æå¸æ代ç æ¢å¯ä»¥ä½ä¸ºâstandaloneâè¿è¡å¨åºç¨ç¨åºå®¹å¨ä¹å¤ï¼è¿å¯ä»¥ä½ä¸ºâcontainerâè¿è¡å¨å®¹å¨ä¸ãè¿éï¼æå¯ä»¥è®¾ç½®å¤ä¸ªProfileï¼ä¾å¦ï¼ä¸é¢ç代ç 设置äºProfile为âstandaloneâä¸âactivemqâã
ãã1
ããctx.getEnvironment().setActiveProfiles("standalone", "activemq");
ããè½ç¶åäºä¸é¢çé ç½®ï¼å®é ä¸å¹¶ä¸ä¼å¯¹å½åçé ç½®ä¸ä¸æ产çå½±åï¼å 为è¿æ²¡æé ç½®Profileå®ä¾ãæ以ï¼ä¿®æ¹é ç½®ä¸ä¸æ为ï¼
ãã<?xml version="1.0" encoding="UTF-8"?>
ãã<beans xmlns="â¦" profile="standalone">
ãã<bean id="dataSource" destroy-method="close"
ããclass="org.apache.commons.dbcp.BasicDataSource">
ãã<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
ãã<property name="url" value="jdbc:hsqldb:hsql://localhost"/>
ãã<property name="username" value="sa"/>
ãã<property name="password" value=""/>
ãã</bean>
ãã</beans>
ããåªæå½æ´»å¨Profile设置为âstandaloneâæ¶ï¼æä¼å®ä¾åè¿ä¸ªBeanãProfileæ¯Beançå±æ§ï¼èä¸æ¯ä¸ä¸ªå®ä¾å¯¹è±¡ï¼å æ¤ï¼ä½ æ æ³é ç½®åç¬çBeanæ¥éæ©Profileãå¨è¾æ©çSpringçæ¬ä¸ï¼è¿ä¼å¯¼è´äº§çå¤ä¸ªæ件ï¼èAntçéé 符æ æ³å¨è¿è¡æ¶æ¾å°æ£ç¡®çé ç½®æ件ã
ããå¨Spring 3.1ä¸ï¼<beans/>æ ç¾å¯ä»¥åµå¥å¨<beans/>æ ç¾å ãç°å¨ï¼æ们éæ°ç¼åä¸ä¸æ°æ®æºé ç½®æ件ï¼
ãã<?xml version="1.0" encoding="UTF-8"?>
ãã<beans xmlns="â¦">
ãã<beans profile="standalone">
ãã<bean id="dataSource">
ãã<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
ãã<property name="url" value="jdbc:hsqldb:hsql://localhost"/>
ãã<property name="username" value="sa"/>
ãã<property name="password" value=""/>
ãã</bean>
ãã</beans>
ãã
ãã<beans profile="container">
ãã<jee:jndi-lookup id="dataSource" jndi-name="java:mydatasource"/>
ãã</beans>
ãã</beans>
ããè¿æ ·ï¼å°±å¯ä»¥éè¿ä¸é¢ç代ç å¿«éåæ¢Profileï¼
ããctx.getEnvironment().setActiveProfiles("container");
ããå¦ä¸ç§åæ¢Profileçæ¹æ³æ¯å¨è¿è¡æ¶ä½ä¸ºç³»ç»åæ°ä¼ å ¥ï¼
ãã-Dspring.profiles.active="standalone"
ããæ¤å¤ï¼ä¹å¯ä»¥ä½ä¸ºEar/Warçåå§ååæ°ä¼ å ¥ï¼
ãã<servlet>
ãã<servlet-name>dispatcher</servlet-name>
ãã<servlet-class>
ããorg.springframework.web.servlet.DispatcherServlet
ãã</servlet-class>
ãã<init-param>
ãã<param-name>spring.profiles.active</param-name>
ãã<param-value>production</param-value>
ãã</init-param>
ãã</servlet>
温馨提示:答案为网友推荐,仅供参考