org.apache.ibatis.type.TypeException: The alias 'Criterion' is already mapped to the value 'com.xxx.domain.def.GameListExample$Criterion' - spring-boot

I use mybatis generator to generate domain and mapper. It works well when I generate one table.
When I generate another table , the problem comes. It seems like the Criterion class in GameListExample and BetRecdExample conflicts, but it was generated by mybatis.
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'betRecdMapper' defined in file
[F:\IdeaProjects\game\target\classes\com\good\game\dao\def\BetRecdMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource
[org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]:
Factory method 'sqlSessionFactory' threw exception; nested exception is org.apache.ibatis.type.TypeException: The alias 'Criterion' is already mapped to the value 'com.good.game.domain.def.GameListExample$Criterion'.

Sounds like the same issue as https://github.com/mybatis/generator/issues/461
It's caused by a bug in mybatis-spring 2.0.1.
https://github.com/mybatis/spring/issues/362
Current workaround is to use mybatis-spring 2.0.0 or 2.0.2-SNAPSHOT.

Related

Spring singleton created, destroyed, created again

I am seeing a peculiar behavior in Spring 3.2.5
One of my beans is created as a consequence of AbstractBeanFactory#getType
Then Spring throws an exception along the lines of
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'A' defined in class path resource [config.xml]: Cannot resolve reference to bean 'B' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'B': Requested bean is currently in creation: Is there an unresolvable circular reference?
As the result of that exception, my bean is destroyed.
Later, my bean is created again, from a different getType() call. Tghis time Spring does not have any circular path problems.
Eventually, the entire context is created successfully, with the second copy of my bean retained as singleton.
My question is - is that behavior normal? Or am I doing something wrong? My bean has side effects in its init() method, so if this is a normal behavior of Spring, I'll need to add a destroy() method to it...
Edit:
To clarify the questions about the exception: The beans mentioned in exception are not exactly relevant because the context (including the beans A and B) is eventually created successfully, so there really is no circular dependency. However, for the completeness' sake, bean A takes bean B as constructor argument.

Null pointer exception while initializing listener

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'PurchaseAccount': Invocation of init method failed; nested exception is javax.xml.ws.WebServiceException: java.lang.NullPointerException
can u tell me y this exception is caused?
... How about you provide some more information. There is not way for anyone to know what is wrong with your code when you don't give any. Please also let us know what have you done already to try to solve your problem.

Spring can't created bean when class has $1.class

When I compiled my java's file,and java compiler generated extra class,like example.class,example$1.class,example$2.class in my package,
and My ApplicationContext's file to scan component like this.
<context:component-scan base-package="com.test">
<context:include-filter type="regex" expression="com\.test\..*"/>
then I got this error message.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'example.1' defined in file [/tmp/jetty-0.0.0.0-8050-relayserver.war-_relayserver-any-2814616804903816631.dir/webapp/WEB-INF/classes/com/test/example$1.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.test.example$1]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.test.example$1.<init>()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1040)
For now,I just only set exclude filter to do this.
MyClass$1 (and $2 and so on) classes are generated for anonymous inner classes in your class. Anonymous inner classes are commonly used when using the listener pattern, they are defined like this:
MyInterface listener = new MyInterface() { // this is the anonymous inner class
... // implementation
}
So I guess you did try to use autowire on an anonymous inner class, which I suspect is not supported by spring (hard to say more without code).

Spring - Instanciate a bean with a class which throws an Exception

I'm trying to instanciate a bean which constructor could throw an Exception.
I can't modify this class (given by an external team).
<bean id="myClass" class="myClass" />
The myClass constructor throws Exception.
I've been thinking about extending this class with a Singleton Pattern which is the behavior I want (be sure to instantiate only one instance of MyClass).
Error message :
nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name
'myClass'
defined in class path resource
[.../spring_applicationContext.xml]:
Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException: Could not
instantiate bean class
[myClass]:
Constructor threw exception; nested exception is
java.lang.ExceptionInInitializerError
Thanks in advance for your answers
I think the problem comes from the fact that my constructor throws an Exception.
My question is : with Spring, is it possible to instanciate a bean with a constructor which could throw an exception ?
If no bean scope is specified in bean configuration file, default to singleton. Your bean myClass is a singleton and you dont need to do anything more.
Spring Doc
Spring beans are by default singletons.
You should provide more of the stacktrace - what you're showing indicates that it is not myClass that is the problem, but the ClassPathXmlApplicationContext constructor that throws an exception - the cause usually follows later in the stacktrace.
Cheers,
As per stack trace, looks like there is an unexpected error either in static block or variable while creating an object of class 'myClass'.

Bean creation is failing (spring)

I am trying to create a bean and than trying to inject the same in my Controller but i am getting bean creation failure error.Here is my code
#Service("springSecurityLoginServiceImpl")
public class SpringSecurityLoginServiceImpl implements SpringSecurityLoginService
{
//impl
}
this is how i am trying to inject it in my controller
#Controller
#RequestMapping("springSecurity/login.json")
public class SpringSecurityLoginController
{
#Autowired
#Qualifier("springSecurityLoginServiceImpl")
SpringSecurityLoginService springSecurityLoginService;
}
There is no entry in Spring-MVC-config xml file except these annotation, but when i am starting server facing the following exception
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0'
defined in ServletContext resource [/WEB-INF/config/spring-mvc-config.xml]:
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'springSecurityLoginController':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: com.core.servicelayer.user.SpringSecurityLoginService com.storefront.controllers.pages.SpringSecurityLoginController.springSecurityLoginService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [com.core.servicelayer.user.SpringSecurityLoginService] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true),
#org.springframework.beans.factory.annotation.Qualifier(value=springSecurityLoginServiceImpl)}
i am not sure what i am doing wrong or what extra i have to do
SpringSecurityLoginController class refers SpringSecurityLoginService class, for which a bean isn't defined. That much the error says.
It is true, because you've only defined a bean for the class LoginServiceImpl, which doesn't seem to extend SpringSecurityLoginService in any way.
Spring's bean lookup algorithm first searches for beans of which type is, or extends, SpringSecurityLoginService. Then, it narrows the avaialble options using the Qualifier. In this case, no bean is found in the first place...
See Spring doc:
4.11.3 Fine-tuning annotation-based autowiring with qualifiers
Since autowiring by type may lead to multiple candidates, it is often
necessary to have more control over the selection process. One way to
accomplish this is with Spring's #Qualifier annotation. This allows
for associating qualifier values with specific arguments, narrowing
the set of type matches so that a specific bean is chosen for each
argument.
You need that LoginServiceImpl will implement SpringSecurityLoginService, for instance.
EDIT
Since it was just a typo you might be not including SpringSecurityLoginService's package in component-scan tag, in your spring configuration file (as gkamal has already mentioned). You should have there something like:
<context:component-scan base-package="org.example"/>
where org.example should be replaced by SpringSecurityLoginService's package.

Resources