天津网站优化推广方案如何建设网站咨询跳转页面
news/
2025/9/24 3:12:04/
文章来源:
天津网站优化推广方案,如何建设网站咨询跳转页面,遂宁门户网站建设先进工作单位,python 建设网站spring aop示例最近#xff0c;我们介绍了Spring Profiles的概念。 此概念是针对不同部署环境的轻松配置区分符。 直接的用例#xff08;已提出#xff09;是对相关的类进行注释#xff0c;以便Spring根据活动的配置文件加载适当的类。 但是#xff0c;这种方法可能并不… spring aop示例 最近我们介绍了Spring Profiles的概念。 此概念是针对不同部署环境的轻松配置区分符。 直接的用例已提出是对相关的类进行注释以便Spring根据活动的配置文件加载适当的类。 但是这种方法可能并不总是适用于常见的用例……通常配置密钥是相同的并且每个值只会随环境而变化。 在本文中我想提出一种模式来支持按环境加载配置数据 而无需为每个概要文件即针对每个环境创建/维护多个类。 在整个文章中假设每个部署环境的数据库定义例如用户名或连接URL不同我将以数据库连接配置为例。 主要思想是使用一个类来加载配置即一个类用于DB连接定义然后将适当的实例注入其中以保存正确的概要文件配置数据。 为了方便和清楚起见该过程分为三个阶段 阶段1 基础设施 步骤1.1 –创建一个包含所有配置数据的属性文件 步骤1.2 –为每个配置文件创建注释 步骤1.3 –确保在上下文加载期间加载了配置文件 阶段2 实施配置文件模式 步骤2.1 –创建属性界面 步骤2.2 –为每个配置文件创建一个类 步骤2.3 –创建一个包含所有数据的抽象文件 阶段3 使用模式 步骤3.1 –使用模式的示例 弹簧轮廓图–阶段1基础准备 此阶段将建立使用Spring Profile和配置文件的初始基础设施。 步骤1.1 –创建一个包含所有配置数据的属性文件 假设您有一个maven风格的项目请在src / main / resources / properties中为每个环境创建一个文件例如 my_company_dev.properties my_company_test.properties my_company_production.properties my_company_dev.properties内容的示例 jdbc.url jdbcmysql// localhost3306 / my_project_db db.username dev1 db.password dev1 hibernate.show_sql true my_company_production.properties内容的示例 jdbc.url jdbcmysql//10.26.26.263306 / my_project_db db.username prod1 db.password fdasjkladsof8aualwnlulw344uwj9l34 hibernate.show_sql false 步骤1.2 –为每个配置文件创建注释 在src.main.java.com.mycompany.annotation中为每个Profile创建注释例如 Target(ElementType.TYPE)
Retention(RetentionPolicy.RUNTIME)
Profile(DEV)
public interface Dev {
}Target(ElementType.TYPE)
Retention(RetentionPolicy.RUNTIME)
Profile(PRODUCTION)
public interface Production {
} 为每个配置文件创建一个枚举 公共接口MyEnums { public enum Profile{
DEV,
TEST,
PRODUCTION
} 步骤1.3 –确保在上下文加载期间加载了配置文件 定义一个系统变量以指示代码在哪个环境中运行。 在Tomcat中转到$ {tomcat.di} /conf/catalina.properties并插入一行 profile DEV根据您的环境 定义一个类来设置活动配置文件 public class ConfigurableApplicationContextInitializer implementsApplicationContextInitializerconfigurableapplicationcontext {Overridepublic void initialize(ConfigurableApplicationContext applicationContext) {String profile System.getProperty(profile);if (profilenull || profile.equalsIgnoreCase(Profile.DEV.name())){applicationContext.getEnvironment().setActiveProfiles(Profile.DEV.name()); }else if(profile.equalsIgnoreCase(Profile.PRODUCTION.name())){applicationContext.getEnvironment().setActiveProfiles(Profile.PRODUCTION.name()); }else if(profile.equalsIgnoreCase(Profile.TEST.name())){applicationContext.getEnvironment().setActiveProfiles(Profile.TEST.name()); }}
} 确保在上下文加载期间加载了该类 在项目web.xml中插入以下内容 context-paramparam-namecontextInitializerClasses/param-nameparam-valuecom.matomy.conf.ConfigurableApplicationContextInitializer/param-value
/context-param 阶段2实施配置文件模式 此阶段利用我们之前构建的基础架构并实现配置文件模式。 步骤2.1 –创建属性界面 为您拥有的配置数据创建一个接口。 在我们的情况下该接口将提供对四个配置数据项的访问。 所以看起来像这样 public interface SystemStrings {String getJdbcUrl();
String getDBUsername();
String getDBPassword();
Boolean getHibernateShowSQL();
//..... 步骤2.2 –为每个配置文件创建一个类 开发配置文件示例 Dev //Notice the dev annotation
Component(systemStrings)
public class SystemStringsDevImpl extends AbstractSystemStrings implements SystemStrings{public SystemStringsDevImpl() throws IOException {//indication on the relevant properties filesuper(/properties/my_company_dev.properties);}
} 生产资料示例 Prouction //Notice the production annotation
Component(systemStrings)
public class SystemStringsProductionImpl extends AbstractSystemStrings implements SystemStrings{public SystemStringsProductionImpl() throws IOException {//indication on the relevant properties filesuper(/properties/my_company_production.properties);}
} 上面的两个类是属性文件与相关环境之间进行绑定的位置。 您可能已经注意到这些类扩展了一个抽象类。 这项技术很有用因此我们不需要为每个Profile定义每个getter从长远来看这是无法管理的实际上这样做是没有意义的。 蜜糖和蜂蜜位于下一步其中定义了抽象类。 步骤2.3 –创建一个包含所有数据的抽象文件 public abstract class AbstractSystemStrings implements SystemStrings{//Variables as in configuration properties file
private String jdbcUrl;
private String dBUsername;
private String dBPassword;
private boolean hibernateShowSQL;public AbstractSystemStrings(String activePropertiesFile) throws IOException {//option to override project configuration from externalFileloadConfigurationFromExternalFile();//optional..//load relevant propertiesloadProjectConfigurationPerEnvironment(activePropertiesFile); }private void loadProjectConfigurationPerEnvironment(String activePropertiesFile) throws IOException {Resource[] resources new ClassPathResource[ ] { new ClassPathResource( activePropertiesFile ) };Properties props null;props PropertiesLoaderUtils.loadProperties(resources[0]);jdbcUrl props.getProperty(jdbc.url);dBUsername props.getProperty(db.username); dBPassword props.getProperty(db.password);hibernateShowSQL new Boolean(props.getProperty(hibernate.show_sql));
}//here should come the interface getters.... 阶段3使用模式 您可能还记得在前面的步骤中我们定义了一个配置数据接口。 现在我们将在需要每个环境不同数据的类中使用该接口 。 请注意该示例是与Spring博客中给出的示例的主要区别因为现在我们不需要为每个概要文件创建一个类 因为在这种情况下我们在概要文件中使用相同的方法并且仅改变数据 。 步骤3.1 –使用模式的示例 Configuration
EnableTransactionManagement
//DB connection configuration class
//(dont tell me youre still using xml... ;-)
public class PersistenceConfig {Autowiredprivate SystemStrings systemStrings; //Spring will wire by active profileBeanpublic LocalContainerEntityManagerFactoryBean entityManagerFactoryNg(){LocalContainerEntityManagerFactoryBean factoryBean new LocalContainerEntityManagerFactoryBean();factoryBean.setDataSource( dataSource() );factoryBean.setPersistenceUnitName(my_pu); JpaVendorAdapter vendorAdapter new HibernateJpaVendorAdapter(){{// JPA propertiesthis.setDatabase( Database.MYSQL);
this.setDatabasePlatform(org.hibernate.dialect.MySQLDialect);this.setShowSql(systemStrings.getShowSqlMngHibernate());//is set per environemnt.. }}; factoryBean.setJpaVendorAdapter( vendorAdapter );factoryBean.setJpaProperties( additionalProperties() );return factoryBean;}
//...
Beanpublic ComboPooledDataSource dataSource(){ComboPooledDataSource poolDataSource new ComboPooledDataSource();try {poolDataSource.setDriverClass( systemStrings.getDriverClassNameMngHibernate() );} catch (PropertyVetoException e) {e.printStackTrace();} //is set per environemnt..poolDataSource.setJdbcUrl(systemStrings.getJdbcUrl());poolDataSource.setUser( systemStrings.getDBUsername() );poolDataSource.setPassword( systemStrings.getDBPassword() );//.. more properties... return poolDataSource;}
} 我将不胜感激和改进。 请享用 参考来自我们JCG合作伙伴 Gal Levinsky的Spring Profile模式来自Gal Levinsky的博客博客。 翻译自: https://www.javacodegeeks.com/2012/08/spring-profile-pattern-example_7.htmlspring aop示例
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/914688.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!