cdi上下文依赖注入

cdi是web容器调用对象的方式,通过依赖注入的方式解耦容器与java对象, 为了解决EJB、javabean被web层组件引用困难的问题 。

cdi是j2ee规范,spring,jboss seam,ejb都实现了cdi规范。所以spring的依赖注入是遵从j2ee的cdi规范的。

所以了解java规范协议是很有益的,让你知道为什么厂商支持这种技术,为了什么目的采用这种技术等等问题。

logback开发测试环境日志文件位置定制

logback日志文件生成位置需要指定绝对路径,这块不能指定项目相对路径,但是开发环境和测试环境路径又不一致。所以使用springProfile来制定开发还是测试路径。

<springProfile name="prod">
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/home/xxx/software/xxx/logs/server.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/home/xxx/software/xxx/logs/%d{yyyy-MM,aux}/server-%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100M</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>-->
<maxHistory>10</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>

</appender>
</springProfile>
<springProfile name="test">
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/home/xxx/software/test/xxx/logs/server.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/home/xxx/software/test/xxx/logs/%d{yyyy-MM,aux}/server-%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100M</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>-->
<maxHistory>10</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>

</appender>
</springProfile>

springboot primary标签

springboot中一个接口多个实现类,需要加@Primary标签

要不然,spring不知道该调用哪个实现。

示例代码:

@Primary
@Bean(name = "sjDb")
@ConfigurationProperties(prefix = "spring.sj")
public DataSource sjDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "tmwDb")
@ConfigurationProperties(prefix = "spring.tmw")
public DataSource tmwDataSource() {
return DataSourceBuilder.create().build();
}

spring jpa 带条件排序分页

@RequestMapping("/house_avg_price")
public List<HouseAvgDto> house_avg_price(String l, String d, Integer p){
Sort sort = new Sort(Sort.Direction.DESC, "signdate")
.and(new Sort(Sort.Direction.ASC, "location"));
PageRequest pageable = new PageRequest(p, 10,sort);
Specification<AreaAvgPriceTmw> specification = (root, cq, cb) -> {
List<Predicate> predicate = new ArrayList<>();
if(org.apache.commons.lang.StringUtils.isNotEmpty(l)){
predicate.add(cb.equal(root.<String>get("location"),l));
}
if(StringUtils.isNotEmpty(d)){
predicate.add(cb.equal(root.get("signdate"),d));
}
Predicate[] pre = new Predicate[predicate.size()];
return cq.where(predicate.toArray(pre)).getRestriction();
};
final Page<AreaAvgPriceTmw> all = areaAvgPriceTmwRepository.findAll(specification, pageable);
Iterator<AreaAvgPriceTmw> areaAvgPriceTmwEntities = all.iterator();

List<HouseAvgDto> houseAvgDtoList = new ArrayList<>();
while (areaAvgPriceTmwEntities.hasNext()){
AreaAvgPriceTmw v = areaAvgPriceTmwEntities.next();
HouseAvgDto houseAvgDto = new HouseAvgDto();
houseAvgDto.setId(v.getId());
houseAvgDto.setBroough(v.getLocation());
houseAvgDto.setSignDate(v.getSigndate());
houseAvgDto.setNewHouseAvgPrice(v.getAvgpriceNet());
houseAvgDto.setNewHouseAvgPriceShow(v.getAvgpriceNetShow());
houseAvgDto.setSecHouseAvgPrice(v.getAvgpriceSec());
houseAvgDto.setSecHouseAvgPriceShow(v.getAvgpriceSecShow());
houseAvgDtoList.add(houseAvgDto);
}
return houseAvgDtoList;
}