I have upgraded Spring Boot version from 2.6.9 to 2.7.0 we were getting below error.
java.lang.IllegalArgumentException: cannot find cache Named "cacheName"
pom.xml:
<dependency>
<groupid>net.sf.ehcache</>
<artifactId>ehcache</>
</dependency>
ehcache.xml:
<cache name="cacheName" logging="true" maxelementsinmemory="1000" eternal="false"
overflowtodisck="false" timetoidleseconds="300" timetoliveseconds="300"
memorystorevictionpolicy="LFU"/>
JAVA Method:
#Cacheable("cacheName")
public List getValues(){
return list;
}
Defined #EnableCaching at below class
#SpringBootApplication
#EnableCachong
public class Application extends SpringBootInitializer{
}
I'm not seeing any cache configuration in your code snippets.
Is the cache implementation supposed to create the cache automatically?
Your application should declare the cache name like this:
spring.cache.cache-names=cacheName
Also, see this issue which might explain the change of behavior.
Related
My web app is build with spring boot 2.1.2.RELEASE, i want to use context path as log dir, but get error when spring boot run.
ERROR Unable to create file ./logs/agilor/${web:contextPath}/logs.log
It look like web lookup is not work.
I have log4j-web in pom.xml
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.12.1</version>
</dependency>
Application is extend from Log4jServletContainerInitializer.
#SpringBootApplication
public class Application extends Log4jServletContainerInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Below is my log4j2.xml.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="LOG_DIR">./logs/agilor/${web:contextPath}</Property>
<Property name="PATTERN">[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{10} - %msg%n</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${LOG_DIR}/logs.log" filePattern="${LOG_DIR}/logs_%d{yyyy-MM-dd}.log">
...
The problem is that Log4j is expecting the the LoggerContext's externalContext field will contain the address of the ServletContext. Spring Boot uses that field as a flag to indicate that Logging has been initialized. This is a bug. I have created LOG4J2-2736 to fix it.
Also, Log4j 2.13.0 was just released. It will let you convert your property to
<Property name="LOG_DIR">./logs/agilor/${spring:spring.application.name}</Property>
if that is what you were trying to do. You would need to include the log4j-spring-cloud-config-client module to get the SpringLookup.
I'm attempting to deploy a Spring Boot (2.0.2) application on JBoss EAP 7.1 server.
The code that's causing the problem is:
import javax.validation.constraints.NotBlank;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
#Component
#Validated
public class AppProperties {
#NotBlank
private String name;
When the application is deployed on JBoss I get the following exception:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
16:44:25,861 ERROR [org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter] (ServerService Thread Pool -- 6 7)
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under 'app' to com.example.security.config.AppProperties:
Property: app.contextpath
Value: /api
Origin: class path resource [application.yml]:5:18
Reason: HV000030: No validator could be found for constraint 'javax.validation.constraints.NotBlank' validating type 'java.lang.String'. Check configuration for 'name'
Action:
Update your application's configuration
I've tried adding the file jboss-deployment-structure.xml with the following contents to WEB-INF/classes:
<jboss-deployment-structure>
<ear-subdeployments-isolated>true</ear-subdeployments-isolated>
<deployment>
<exclude-subsystems>
<subsystem name="jaxrs"/>
</exclude-subsystems>
<exclusions>
<module name="javaee.api"/>
<module name="javax.validation.api"/>
<module name="javax.faces.api"/>
<module name="org.hibernate.validator"/>
</exclusions>
</deployment>
</jboss-deployment-structure>
But, no luck. What's the workaround? Thanks in advance.
Even though this question is a year old, I ran into the same issue and couldn't find a solution.
This, I know, will work for Spring Boot 2.1.x and JBoss 7.1, not sure about versions before that.
We obviously need to exclude org.hibernate.validator and javax.validation.api. What wasn't clear is that we also need to exclude the javax.faces.api (it has a transitive dependency on javax.validation.api). Excluding that javax.faces causes JBoss to fail on start up due to missing jsf libraries. We can then simply exclude the jsf subsystem.
<jboss-deployment-structure>
<deployment>
<exclude-subsystems>
<subsystem name="jsf" />
</exclude-subsystems>
<exclusions>
<module name="javax.validation.api" />
<module name="javax.faces.api" />
<module name="org.hibernate.validator" />
</exclusions>
</deployment>
</jboss-deployment-structure>
Assuming you don't need JSF from jboss, this should work.
According to this article the jboss-deployment-structure.xml shoudl be placed in 'the top level deployment, in META-INF (or WEB-INF for web deployments)'.
So with your current set-up the things configured in the xml are not applied, so if the xml is put in the right location it might work.
javax.validation.constraints.NotBlank is part of Bean Validation 2.0 and thus Java EE 8. I suspect EAP 7.1 does not support this feature yet.
I am using spring boot 1.5.4 with mybatis-spring 3.1. I am able to successfully run my application via eclipse. But when I used commandline (java -jar jarname), I get below exception. my standalone application is stopping. I want to move my app to deploy to different machine with the jar. Please help.
Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'MyClass'. Cause: java.lang.ClassNotFoundException: Cannot find class: MyCLass
at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:120)
at org.apache.ibatis.builder.BaseBuilder.resolveAlias(BaseBuilder.java:149)
at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:116)
... 36 more
I resolved the same issue by moving to autoconfigure(using spring properties) I originally had my db configuration(datasource, session factory) configured in a javaconfig class. I'm removed the config and moved my config to my application properties(yml format) ..
Below is what I have
mybatis:
typeAliasesPackage: com.wiredinformatics.preauth.domain
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/preauth?
useSSL=false&serverTimezone=UTC
username: myuser
password: mypass
dbcp2:
driver: com.mysql.cj.jdbc.Driver
initial-size: 2
max-total: 10
max-idle: 10
min-idle: 5
max-wait-millis: 30000
validation-query: SELECT 1
I haven't had time yet to figure out why having my own java config broke the scanning. It worked ok in eclipse, but failed when running from the command line
I solved this problem!
https://github.com/mybatis/mybatipse/issues/77
#deoxyseia
remove
sessionFactoryBean.setTypeAliasesPackage("com.your.packae.pojo")
change resultType="MyClass" to
resultType="com.your.packae.pojo.MyClass"
repackage
I had a similar problem while working in a Maven project, resulting in the same errors.
My situation is that I have a executable jar with Spring Boot which has a nested jar (which uses regular Spring) as dependency. While running in IntelliJ, there was absolutely no problem, due to the way that IntelliJ finds its classes.
While trying to start the jar locally with java -jar jarname.jar so that I could deploy it on a remote server, MyBatis had trouble scanning the typeAliases and typeHandlers packages.
Since I'm working on a legacy-project which mixes Spring Beans initialized in Java and xml, I had one hell of a time determining the root cause. A lot of answers said to change your resultType to the full classpath like this answer. This works. But in my case that would mean hundreds of changes to our DAO's.
Finally, I got on track following this external link from titanwolf.
Here's what you need to do:
add the following dependency to your POM (the jar which contains your MyBatis setup)
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
set the VFS property on your SqlSessionFactoryBean
xml:
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="vfs" value="org.mybatis.spring.boot.autoconfigure.SpringBootVFS"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
or if you use regular Java for your bean initalizing:
#Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setVfs(SpringBootVFS.class);
return factory;
}
And that's it. Works locally and on remote server. Hope I saved anyone else some headaches.
note: mybatis-config.xml contains my configuration including, but not limited to:
<configuration>
<settings>
<setting name="lazyLoadingEnabled" value="false"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<package name="com.my.path.model"/> <!--wasn't being picked up before fix-->
</typeAliases>
<typeHandlers>
<package name="com.my.path.mybatis"/> <!--wasn't being picked up before fix-->
</typeHandlers>
<mappers>
<mapper resource="sql/my-dao.xml"/>
<mappers>
</configuration>
You can trace your own errors better by enabling logging for MyBatis. Just add the following line to your application.properties file:
logging.level.org.mybatis=DEBUG
After 12 hours of trying I don't seem to be able to get Spring load time weaving working on Tomcat.
Spring 4.2.1
Hibernate 4.3.11
Tomcat 8.09
I am trying to get an #Entity autowired.
The weaver output always says:
not weaving 'mypackage.MyEntity'
unless I also use a #Configuration annotation on it. It will then weave but I get back A SPRIGNCGLIB proxy where all the properties are null.
If I remove the #Configuration annotation (I don't think it should be there anyway) then I don't get any weaving and #Autowired property is always null.
This is my configuration:
applicationContext-beans.xml
<context:component-scan base-package="my.package" />
<context:spring-configured />
<context:load-time-weaver />
classes/META-INF/aop.xml
<aspectj>
<weaver options="-Xreweavable">
<include within="my.package.*"/>
</weaver>
<aspects>
<aspect name="org.springframework.beans.factory.aspectj.AbstractInterfaceDrivenDependencyInjectionAspect"/>
</aspects>
MyEntity.java
package my.package;
#Entity
#Table(name = "user")
#Configurable
public class User {
private Encrypter encrypter; // THE CLASS I WANT INJECTED
#Autowired
public void setEncrypter(Encrypter encrypter) {
this.encrypter = encrypter;
}
}
context.xml
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
The Tomcat lib folder has (I am not sure it needs both of these):
spring-instrument-4.2.1.RELEASE.jar
spring-tomcat-weaver-2.5.6.SEC03.jar
The apps WEB-INF/lib folder has:
aspectjweaver-1.8.6.jar
spring-aop-4.2.1.RELEASE.jar
spring-aspects-4.2.1.RELEASE.jar
I have tried starting Tomcat with
-javaagent:D:/my/path/to/server/apache-tomcat-8.0.9/lib/spring-instrument-4.2.1.RELEASE.jar
but it didn't help and according to the Spring LTW documentation the context.xml fragment is the preferred way to do this.
Does anyone have any ideas?
I have a small web application in development using Maven, Spring MVC and Spring Data Mongo. I am getting a java.lang.NoSuchMethodError when one of my Controllers attempts to access a method defined in a custom repository. The same method works fine when exercised via a JUnit 4 test extending AbstractJUnit4SpringContextTests and using a near-identical XML configuration file.
Standard repository:
public interface IndividualRepository extends MongoRepository<Individual, String>, IndividualRepositoryCustom {
...
}
Custom interface:
public interface IndividualRepositoryCustom {
Individual findByIdentifier(String identifierType, String identifierValue);
}
Custom implementation:
public class IndividualRepositoryImpl implements IndividualRepositoryCustom {
#Autowired
private MongoTemplate mongoTemplate;
#Override
public Individual findByIdentifier(String identifierType, String identifierValue) {
String locator = String.format("identifiers.%s", identifierType);
return mongoTemplate.findOne(query(where(locator).is(identifierValue)), Individual.class);
}
}
dataaccess-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
<mongo:repositories base-package="com.myco.dataaccess"/>
<mongo:mongo host="mongo.myco.com" port="27017"/>
<mongo:db-factory dbname="test" mongo-ref="mongo"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo"/>
<constructor-arg value="test"/>
</bean>
</beans>
In my JUnit test I have (excerpt):
#Autowired
private IndividualRepository individualRepo;
...
List<Individual> foundList = individualRepo.findAll();
assertNotNull(foundList);
assertTrue(foundList.size() > 0);
Individual found = individualRepo.findByIdentifier("someid", "123456");
assertNotNull(found);
assertEquals("Bob", found.getFirstName());
The test passes fine, calling both findAll() (standard Repository method) and findByIdentifier() (custom method). The latter fails with NoSuchMethodError when called by a Controller running in a web application in Jetty, while the same Controller can call findAll() with no issues.
This turned out to be nothing to do with Spring Data, but an issue with the way I was using the maven-jetty-plugin with my multi-module build.
Basically, I was running mvn jetty:run for my web module which had a dependency on my dataaccess module (where my JUnit tests lived). As I was rebuilding the project with mvn clean package, the latest versions were not being placed in my local repo, and therefore were not being picked up by the mvn jetty:run process running alongside my build process. Problem was solved by building with mvn clean install.
So, as usual, the error message was spot on - the method indeed did not exist in the version of the JAR that Jetty was being supplied.