显示标签为“Spring”的博文。显示所有博文
显示标签为“Spring”的博文。显示所有博文

2020年10月19日星期一

@Transactional导致的问题

 https://groups.google.com/g/mybatis-user/c/cat2sbCD3rI?pli=1

spring项目中使用了@Transactional,导致了insert语句执行完之后,无法select查询到结果。需要将insert和select分开成两个事务才能解决。

2017年9月13日星期三

spring boot结合hibernate使用中的一些问题

org.hibernate.MappingException: composite-id class must implement Serializable
如果指定了多个@Id,这个类必须implement Serializable


org.xml.sax.SAXException: null:11: Element <defaultCache> does not allow attribute "maxEntriesLocalHeap".
因为ehcache版本过低,需要在maven中单独引入,不能用hibernate默认的版本


org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
参考https://stackoverflow.com/questions/26203446/spring-hibernate-could-not-obtain-transaction-synchronized-session-for-current
必须要指定@EnableTransactionManagement

2017年9月12日星期二

spring boot使用bazel编译运行时无法注入bean

使用到了azure-storage-spring-boot-starter中的bean,所以我增加了@ComponentScan({"com.xxx", "com.microsoft.azure"}),还是无法注入bean。

最后增加了@PropertySource("classpath:application.properties")显示指定application.properties文件(配置文件中有azure storage连接的配置)才解决问题。不知道啥原因,瞎打瞎碰解决了问题。

2017年3月2日星期四

spring data jpa中repository.save中文乱码

mysql编码设置正常,自己使用insert语句中文也显示正常,所以问题在spring。

数据库的url需要加上编码设置,类似:
jdbc:mysql://localhost:3306/springexample?characterEncoding=utf-8

2017年2月27日星期一

spring boot data jpa: operations allowed after connection closed连接异常

参考http://www.jianshu.com/p/1626d41572f2

#以下为连接池的相关参数配置
spring.datasource.primary.max-idle=10
spring.datasource.primary.max-wait=10000
spring.datasource.primary.min-idle=5
spring.datasource.primary.initial-size=5
spring.datasource.primary.validation-query=SELECT 1
spring.datasource.primary.test-on-borrow=false
spring.datasource.primary.test-while-idle=true
spring.datasource.primary.time-between-eviction-runs-millis=18800

2017年2月26日星期日

spring boot打包错误Unable to find a single main class from the following candidates

因为spring发现了多个类似@SpringBootApplication的入口类,解决方法:
在pom.xml的<properties></properties>中增加:
<start-class>your/main/class</start-class>

参考:http://www.jianshu.com/p/b521f819b06a

2016年12月18日星期日

Intellij中spring jpa entity "cannot resolve symbol"

see this link: http://stackoverflow.com/questions/12420996/intellij-idea-highlights-entity-class-names-with-cannot-resolve-symbol-in-jp

选择project structure中的facet,新建JPA,新增一个JPA Provider的persistence.xml,打开intellij左侧的persistence,右键选择new -> Persistence Unit,done。

2016年12月12日星期一

spring boot注入静态变量

private static String test;

@Value("${test}")
public void setXxx(String test) {
  Test.test = test;
}

遇到一个问题,如果这个static变量是要传给Test类的super方法调用:
public Test() {
  super(test);
}
执行顺序是先调用构造方法,后调用set方法,不能满足需求。

解决方法1:
public Test(@Value("${test}") String test) {
  super(test);
}

解决方法2:
把super父类中的用到test变量逻辑抽成了protected方法loadTest(test),在子类中调用,不再通过super传参调用:
public Test() {
  super();
}

@PostConstruct
public void init() {
  loadTest(test);
}

spring boot使用@PostConstruct初始化static变量

在类中定义private static变量,然后使用@PostConstruct:
@PostConstruct
public void init() {
}
init()中给static变量赋值

spring boot @Value注解值为空

对spring不熟的原因,使用@Value怎么改都是null。
原因是我在自己的类中(service层用到)使用了@Value,这并不起作用,然后我在类前面加了@Component注解,依然是null。
最后,我在service层@Autowired自动注入了该类,而不是自己new,问题解决了,折腾挺久...
总结一下,如果用Spring注解,不能自己new一个类。

spring boot启动问题

使用了@Configuration注解,启动时候报错:
Caused by: java.lang.IllegalArgumentException: No visible constructors in class ...

发现是我这个类的构造函数设为了private,改为protected后解决...

2016年12月3日星期六

spring boot中log4j打不出日志的问题

打开pom.xml的Dependency Analyzer,查看All Dependencies as Tree,搜索log4j,发现spring-boot-starter-web中引入了log4j-over-slf4j的依赖,并不是我们用的log4j,右键选择exclude将log4j-over-slf4j去掉,然后重新在pom.xml中添加log4j的依赖,问题解决。