1、@Value${} 取不到值 以及使用Environment.getProperty取到的值为null
可以在xml配置里新加一个PropertyPlaceholderConfigurer,将相应的properties放入;
<bean id="propertyConfigurerForProject1"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:/xx.properties</value>
<value>classpath:/jdbc.properties</value>
</list>
</property>
</bean>
进而可以在进一步,重写 PropertyPlaceholderConfigurer,这样可以在java代码的config中使用自定义的函数getProperty来获取属性值:
<bean id="propertyPlaceHolder" class="PropertyPlaceHolder">
<property name="locations">
<list>
<value>classpath:/other.properties</value>
</list>
</property>>
</bean>
public class PropertyPlaceHolder extends PropertyPlaceholderConfigurer {
private static Map<String,String> propertyMap;
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
propertyMap = new HashMap<>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
propertyMap.put(keyStr, value);
}
}
public static String getProperty(String name) {
return propertyMap.get(name);
}
}
2、Jenkins发版报错:找不到xx.properties
pom中的build新增<recourse>内容,将recourses文件夹下的配置文件include进去:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
3、对于spring-boot的web项目,发版成功后访问网页报错:重定向;同时在日志里报错:Error resolving template “xxx”, template might not exist or might not be accessible by any of the configured Template Resolvers
可以先去代码里看一下相应的界面:
日志显示了“-------------------toLogin”,所以说明这个controler是可以访问的,问题在于return到一个html的时候,找不到相应的界面;
首先检查一下spring-boot-starter-thymeleaf依赖 ,没问题;而html文件都放在resources的templates文件夹里面,在项目运行的时候,会生成一个target目录,里面有一个classes目录,然后打开发现并没有templates文件夹,所以需要新增resources的include:
<include>**/*.*</include>,即在resources的所有目录下的所有文件全部打包,错误就解决了,然后可以运行,classes下面配置文件及.html文件也都有了
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.*</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
4、找不到主类main( could not find or load main class )
spring-boot打包jar结构:
在MANIFEST.MF文件里面会有一个启动类
所以需要使用spring-boot-maven的插件来打包(同时可以指定mainClass):
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.3.RELEASE</version>
<configuration>
<mainClass>com.WebApplication</mainClass>
</configuration>
</plugin>
5、jenkins对spring-boot的编译docker镜像 会有一步读取tar.gz文件
报错截图:
所以可以新增一个maven-assembly插件,配置distribution.xml,生成tar.gz文件: