2013年1月23日 星期三
Precondition of Design by Contract
如果在callee裡面要做precondition的話,我們可以使用org.apache.commons.lang3.Validate,在method一開始執行就進行arguments的驗證,並且丟出合理易懂的exception,避免method執行到很後面時才拋出不太容易理解的exception,也減少stack trace的複雜性有助於debug。
2013年1月11日 星期五
Apache Commons DbUtils
這邊我要介紹Apache Commons DbUtils,在官網的介紹如下:
DbUtils is designed to be:
DbUtils is designed to be:
- Small - you should be able to understand the whole package in a short amount of time.
- Transparent - DbUtils doesn't do any magic behind the scenes. You give it a query, it executes it and cleans up for you.
- Fast - You don't need to create a million temporary objects to work with DbUtils.
DbUtils is not:
- An Object/Relational bridge - there are plenty of good O/R tools already. DbUtils is for developers looking to use JDBC without all the mundane pieces.
- A Data Access Object (DAO) framework - DbUtils can be used to build a DAO framework though.
- An object oriented abstraction of general database objects like a Table, Column, or PrimaryKey.
- A heavyweight framework of any kind - the goal here is to be a straightforward and easy to use JDBC helper library.
2013年1月9日 星期三
Integrate Apache Commons Configuration with Spring
要整合Apache Commons Configuration到Spring中很容易,直接用Commons Configuration API將org.apache.commons.configuration.Configuration物件設定到Spring,成為Spring managed bean就可以了。
原本有個Spring Modules可以直接將Commons Configuration整合成Spring Property Placeholder,但似乎已經被移除了。
以下是利用database做為configuration source的設定方式:
原本有個Spring Modules可以直接將Commons Configuration整合成Spring Property Placeholder,但似乎已經被移除了。
以下是利用database做為configuration source的設定方式:
- 假設你已經設定好DataSource。
- 設定DatabaseConfiguration,記得先開好table。
- Inject Configuration bean到其它class中。
代碼:
import javax.sql.DataSource;
import org.apache.commons.configuration.DatabaseConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpringGlm3CoreConfiguration {
@Bean
@Autowired
public org.apache.commons.configuration.Configuration databaseConfiguration(DataSource dataSource) {
return new DatabaseConfiguration(dataSource, "myconfigs", "key", "value");
}
}
import org.apache.commons.configuration.DatabaseConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpringGlm3CoreConfiguration {
@Bean
@Autowired
public org.apache.commons.configuration.Configuration databaseConfiguration(DataSource dataSource) {
return new DatabaseConfiguration(dataSource, "myconfigs", "key", "value");
}
}
代碼:
import org.apache.commons.configuration.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Company {
@Autowired
private Configuration conf;
public String getCompanyId() {
return conf.getString("company-id");
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Company {
@Autowired
private Configuration conf;
public String getCompanyId() {
return conf.getString("company-id");
}
}
訂閱:
文章 (Atom)