Groovy, jruby as Spring beans - java

I am trying to inject a groovy class as a bean but getting a java.io.FileNotFoundException exception.
Reference: Spring in Action, 2ed
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'coconut' defined in class path resource [aspects.xml]: Cannot resolve reference to bean 'lime' while setting bean property 'lime'; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'lime': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'lime': Could not determine scripted object type for GroovyScriptFactory: script source locator [classpath:band.Lime.groovyy]; nested exception is java.io.FileNotFoundException: class path resource [band.Lime.groovy] cannot be opened because it does not exist
My Groovy File - Lime.groovy:
class Lime implements band.Lime{
void drink(){
print "You drink Lime!"}
}
My pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties></properties>
<groupId>com.gmail#qwertygoog</groupId>
<artifactId>RockWithAspects</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>3.0.0</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
So, any advice?
upd.-here is class of Coconut
public class Coconut {
private Lime lime;
public Coconut() {
}
public void drinkFast(){
System.out.println("You drink coconut ...");
lime.drink();
}
#Autowired
public void setLime(Lime lime) {
this.lime = lime;
}
}
And my XML is like:
<?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:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang.xsd
"
>
<bean id="coconut" class="band.Coconut">
<!-- <property name="lime" ref="lime"/>-->
</bean>
<lang:groovy id="lime"
script-source="classpath:Lime.groovy"
/>
</beans>

Related

Problem with Spring Boot Camel application

I am still very new to using Apache Camel and am trying to adapt an example provided by someone (https://www.javainuse.com/camel/camel-consume-rest) to essentially do the same thing the original application does, except by using a combination of Spring Boot and Camel with XML route configuration.
I downloaded the original application code (both the Camel consumer and the HTTP server) and got those to run and execute properly very easily.
Now, I want to modify the application a little so that Spring Boot is used to do all the behind-the-scenes configuration, and use the XML DSL to configure the Camel routes. The original Camel consumer application does not use Spring at all.
I made the changes that seemed to make sense, but cannot get the application to run. I get the following exception:
Exception in thread "main" java.lang.IllegalAccessError: tried to access method org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames(Ljava/lang/Class;Ljava/lang/ClassLoader;)Ljava/util/List; from class org.springframework.boot.SpringApplication
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:418)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:412)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:268)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:249)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)
at org.test.ApplicationMain.main(ApplicationMain.java:12)
I do not understand what it's complaining about. I'm including resources below so that people can look at what I'm doing. I apologize in advance for the amount of code.
ApplicationMain.java:
#SpringBootApplication
#ImportResource("classpath:camel-context.xml")
public class ApplicationMain {
public static void main(String[] args) {
SpringApplication.run(ApplicationMain.class, args);
}
}
Configurer.java:
#Configuration
#ComponentScan("org.test")
public class Configurer {
#Bean
public MyProcessor myProcessor() {
return new MyProcessor();
}
}
application.yml:
---
camel:
springboot:
name: CamelConsumer
main-run-controller: true
camel-context.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<import resource="classpath:routes/routes.xml" />
<camelContext id="camel-context" xmlns="http://camel.apache.org/schema/spring">
<routeContextRef ref="routes" />
</camelContext>
</beans>
routes/routes.xml:
Note: I do not know whether I'm using the correct syntax for the elements in the route definition below. I'm trying to adapt it from the original, which was done in Java DSL. I'm also not sure whether or not I need the processor bean definition, since I also have the bean defined in the Configurer above.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="myProcessor" class="org.test.processor.MyProcessor" />
<routeContext id="routes" xmlns="http://camel.apache.org/schema/spring">
<route id="httpTest">
<from uri="file:C:/inboxREST?noop=true" />
<setHeader headerName="HTTP_METHOD">
<simple>GET</simple>
</setHeader>
<to uri="http://localhost:8080/employee?id=5" />
<process ref="myProcessor" />
</route>
</routeContext>
</beans>
MyProcessor.java:
public class MyProcessor implements Processor {
public MyProcessor() {
}
public void process(Exchange exchange) throws Exception {
System.out.println(exchange.getIn().getBody(String.class));
}
}
pom.xml:
Note: You may notice a mix of Spring Boot Camel starters as well as Camel dependencies for Camel components (like HTTP). It's not clear to me which of these I actually need. Do the Spring Boot starters remove the need to include Camel component dependencies?
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javainuse</groupId>
<artifactId>apache-camel-consume</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath />
</parent>
<properties>
<java.version>1.8</java.version>
<apache-camel-springboot.version>3.1.0</apache-camel-springboot.version>
<apache-camel.version>3.1.0</apache-camel.version>
</properties>
<dependencies>
<!-- Camel BOM -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-dependencies</artifactId>
<version>${apache-camel-springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Camel Starter -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>${apache-camel-springboot.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot</artifactId>
<version>${apache-camel-springboot.version}</version>
</dependency>
<!-- Camel HTTP -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-http</artifactId>
<version>${apache-camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-http-starter</artifactId>
<version>${apache-camel-springboot.version}</version>
</dependency>
<!-- Camel Jackson -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jackson-starter</artifactId>
<version>${apache-camel-springboot.version}</version>
</dependency>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<version>2.4.0</version>
</plugin>
</plugins>
</build>
</project>
Argh. Very tricky and hard to find problem.
It turns out that there were dependencies in my maven repo (.m2/repository) that I needed to clean out. I think they were brought in by the original project indicated at the start of the post.
Then, I needed to edit the eclipse project properties to remove the old (and completely unneeded) jars. Once this was done and a clean build completed, the application started as it should.

Error while integrating spring with hibernate

I'm trying to integrate spring with hibernate but i get this error:
[2017-10-06 05:26:14,475] Artifact spring-mvc-hibernate-example:war: java.io.IOException: com.sun.enterprise.admin.remote.RemoteFailureException: Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDaoImp': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getSessionFactory' defined in com.boraji.tutorial.spring.config.AppConfig: Invocation of init method failed; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution. Please see server.log for more details.
Here are some files:
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.boraji.tutorial.spring</groupId>
<artifactId>spring-mvc-hibernate-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<!-- Spring MVC Dependency -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- Spring ORM -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- Mysql Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<!-- Hibernate ORM -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- Hibernate-C3P0 Integration -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- Hibernate Validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
<!-- JSTL Dependency -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- Servlet Dependency -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- JSP Dependency -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.5.4-Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.8.0-alpha2</version>
</dependency>
</dependencies>
</project>
Initializer:
public class MyWebAppInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { AppConfig.class };
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { WebConfig.class };
}
#Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
application config:
#Configuration
#PropertySource("classpath:db.properties")
#EnableTransactionManagement
#ComponentScans(value = { #ComponentScan("com.boraji.tutorial.spring.dao"),
#ComponentScan("com.boraji.tutorial.spring.service") })
public class AppConfig {
#Autowired
private Environment env;
#Bean
public LocalSessionFactoryBean getSessionFactory() {
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
Properties props = new Properties();
// Setting JDBC properties
props.put(DRIVER, env.getProperty("mysql.driver"));
props.put(URL, env.getProperty("mysql.url"));
props.put(USER, env.getProperty("mysql.user"));
props.put(PASS, env.getProperty("mysql.password"));
// Setting Hibernate properties
props.put(SHOW_SQL, env.getProperty("hibernate.show_sql"));
props.put(HBM2DDL_AUTO, env.getProperty("hibernate.hbm2ddl.auto"));
props.put(DIALECT, env.getProperty("hibernate.dialect"));
// Setting C3P0 properties
props.put(C3P0_MIN_SIZE,
env.getProperty("hibernate.c3p0.min_size"));
props.put(C3P0_MAX_SIZE,
env.getProperty("hibernate.c3p0.max_size"));
props.put(C3P0_ACQUIRE_INCREMENT,
env.getProperty("hibernate.c3p0.acquire_increment"));
props.put(C3P0_TIMEOUT,
env.getProperty("hibernate.c3p0.timeout"));
props.put(C3P0_MAX_STATEMENTS,
env.getProperty("hibernate.c3p0.max_statements"));
factoryBean.setHibernateProperties(props);
factoryBean.setAnnotatedClasses(User.class);
return factoryBean;
}
#Bean
public HibernateTransactionManager getTransactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(getSessionFactory().getObject());
return transactionManager;
}
}
db.properties:
# MySQL properties
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost/test
mysql.user=root
mysql.password=root
# Hibernate propert[2017-10-06 05:26:14,475] Artifact spring-mvc-hibernate-example:war: java.io.IOException: com.sun.enterprise.admin.remote.RemoteFailureException: Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDaoImp': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getSessionFactory' defined in com.boraji.tutorial.spring.config.AppConfig: Invocation of init method failed; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution. Please see server.log for more details.ies
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
hibernate.dialect=org.hibernate.dialect.MySQLDialect
#C3P0 properties
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.acquire_increment=1
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=150
UserDao:
#Repository
public class UserDaoImp implements UserDao {
#Autowired
private SessionFactory sessionFactory;
public void save(User user) {
sessionFactory.getCurrentSession().save(user);
}
public List<User> list() {
#SuppressWarnings("unchecked")
TypedQuery<User> query = sessionFactory.getCurrentSession().createQuery("from User");
return query.getResultList();
}
}
I saw lots of similar problems here but it didn't help. What could go wrong in my application?
DRIVER, URL, USER and PASS are all static fields. Make sure you imported all of them. Plus, make sure you imported the spring Environment (not the hibernate).
import static org.hibernate.cfg.Environment.*;
import org.springframework.core.env.Environment;

Spring boot exception for a very simple app

My application has this. It has not hibernate or jpa stuff it yet. I added only in libraries in pom.xml:
#SpringBootApplication
#ComponentScan("com.ma.vegshopping")
public class VegShoppingApplication {
public static void main(String[] args) {
SpringApplication.run(VegShoppingApplication.class, args);
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ma</groupId>
<artifactId>VegShopping</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>VegShopping</name>
<description>vegetable shopping list</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-social-facebook</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
When try to start app, I get following exception:
ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
When u put the JPA hibernate jars are brought into classpath. And when Hibernate is available in classpath Spring Boot AutoConfiguration is picked for Creating the EntityManager.
This is the default behaviour of Spring boot.
Since you dont have any datasource for Hibernate the startup is failing.
Try creating a datasource by giving properties in your application.properties / yml file.
If u dont want to use hibernate or jpa then remove jpa starter from your pom.xml

Trying to use my Service in 2 classes : org.springframework.beans.factory.BeanCreationException: Error creating bean with name

Trying to use my AccountService (#Service) in 2 classes getting me this Error:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.mz.springContextWorld.repositories.AccountRepository com.mz.springContextWorld.services.AccountService.accountRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountRepository': Cannot resolve reference to bean 'neo4jTemplate' while setting bean property 'neo4jTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.data.neo4j.config.Neo4jConfiguration#0': Cannot resolve reference to bean 'graphDatabaseService' while setting bean property 'graphDatabaseService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'graphDatabaseService': FactoryBean threw exception on object creation; nested exception is java.lang.RuntimeException: Error starting org.neo4j.kernel.EmbeddedGraphDatabase, /home/matthias/IdeaProjects/springContextWorld/target/neo4j-db-plain
here is my github repo: https://github.com/mzober/springContextWorld/tree/CollectorManager_ErrorBranch
and here is the use of my accountService:
#Autowired
AccountService accountService;
public LoginPanel() {
//errorline here
this.accountService=GuiFactory.getInstance().mainWindow().getAccountService();
init();
addComponents();
}
...
#Autowired
AccountService accountService;
public RegistryAndLoginListener() {
//no error here
accountService=GuiFactory.getInstance().mainWindow().getAccountService();
}
...getting the accountService from the context-configuration:
<context:annotation-config/>
<context:spring-configured/>
<context:component-scan base-package="com.mz.springContextWorld.domain"/>
<context:component-scan base-package="com.mz.springContextWorld.repositories"/>
<context:component-scan base-package="com.mz.springContextWorld.gui.listener"/>
<context:component-scan base-package="com.mz.springContextWorld.gui.components"/>
<context:component-scan base-package="com.mz.springContextWorld.gui.creational"/>
<context:component-scan base-package="com.mz.springContextWorld.services"/>
<neo4j:config storeDirectory="target/neo4j-db-plain"
base-package="com.mz.springContextWorld.domain"/>
<neo4j:repositories base-package="com.mz.springContextWorld.repositories"/>
<tx:annotation-driven />
... load the configuration in MainWindow (of my demo-application of Spring-Data-Neo4j)
#Autowired
public AccountService accountService;
public MainWindow() {
super(PROJECTNAME);
context= new ClassPathXmlApplicationContext("spring/spring-config.xml");
accountService=(AccountService)context.getBean("accountService");
...
}
public AccountService getAccountService(){return this.accountService;}
the AccountService:
#Service
#Transactional
public class AccountService {
#Autowired
AccountRepository accountRepository;
public AccountService(){
}
#Transactional
public Iterable<Account> getAll() {
return accountRepository.findAll();
}...
here is my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mz.springContextWorld</groupId>
<artifactId>springContextWorld</artifactId>
<packaging>jar</packaging>
<version>3.2.1.RELEASE</version>
<name>springContextWorld</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.0.7.RELEASE</spring.version>
<spring-data-neo4j.version>${project.version}</spring-data-neo4j.version>
<slf4j.version>1.7.5</slf4j.version>
<neo4j.version>2.1.5</neo4j.version>
<junit.version>4.11</junit.version>
</properties>
<repositories>
<repository>
<id>neo4j-release-repository</id>
<name>Neo4j Maven 2 release repository</name>
<url>http://m2.neo4j.org/releases</url>
</repository>
</repositories>
<dependencies>
<!--spring data-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>${spring-data-neo4j.version}</version>
</dependency>
<!--spring data end-->
<!--hibernate-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.1.Final</version>
</dependency>
<!--hibernate end-->
<!--test-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-kernel</artifactId>
<version>${neo4j.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<!--test end-->
<!--Loading Related dependencies ...logging-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--logging end-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
... and i'm trying to set the txmode to proxy, and more but the error is still there...
ty for helping
Seems your neo4j db has failed to start. Check last log line : Error starting org.neo4j.kernel.EmbeddedGraphDatabase, /home/matthias/IdeaProjects/springContextWorld/target/neo4j-db-plain

ClassNotFoundException: org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

I am completely a beginner at Spring (you can see that in my code :) ). I just wanted to test the class RestTemplate but I got a ClassNotFoundException.
So the code is:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.client.RestTemplate;
public class RestClient {
private RestTemplate restTemplate;
public String getJiraIssueAsJson(){
Object o = restTemplate.getForObject(..., Object.class);
System.out.println("..."+o.getClass());
return null;
}
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("rest-client-context.xml");
RestClient restClient = context.getBean("restClient", RestClient.class);
restClient.getJiraIssueAsJson();
}
}
context.xml
<beans ...>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverte‌​‌​r"/>
</list>
</property>
</bean>
<bean id="restClient" class="org.googlecode.happymarvin.jiraexplorer.RestClient">
<property name="restTemplate" ref="restTemplate"/>
</bean>
</beans>
pom.xml
<project ...>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.googlecode.happymarvin</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>jiraminer</artifactId>
<name>Happy Marvin JIRA Miner</name>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jackson-version>1.9.13</jackson-version>
</properties>
</project>
parent pom.xml
<project ...>
<modelVersion>4.0.0</modelVersion>
<groupId>com.googlecode.happymarvin</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Happy Marvin parent project</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.version>4.0.0.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
</dependencies>
</project>
Exception
Jan 07, 2014 10:18:24 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#730eb2f0: startup date [Tue Jan 07 10:18:24 GMT 2014]; root of context hierarchy
Jan 07, 2014 10:18:24 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [rest-client-context.xml]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in class path resource [rest-client-context.xml]: Cannot create inner bean 'org.springframework.http.converter.json.MappingJacksonHttpMessageConverte‌​‌​r#77624896' of type [org.springframework.http.converter.json.MappingJacksonHttpMessageConverte‌​‌​r] while setting bean property 'messageConverters' with key [0]; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.http.converter.json.MappingJacksonHttpMessageConverte‌​‌​r] for bean with name 'org.springframework.http.converter.json.MappingJacksonHttpMessageConverte‌​‌​r#77624896' defined in class path resource [rest-client-context.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingJacksonHttpMessageConverte‌​‌​r
...
Caused by: java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingJacksonHttpMessageConverte‌​‌​r
I have this exception when I try to run the main method from eclipse.
I can think of something like the spring jars cannot be seen but I don't know why...
Can you please help me?
The first major version of Jackson is no longer supported in Spring 4. The class you want to use is now
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter. Make sure that you have com.fasterxml.jackson.core/jackson-core/2.x.x on your classpath.
I faced same issue. Fixed using org.springframework.http.converter.json.MappingJackson2HttpMessageConverter instead of org.springframework.http.converter.json.MappingJacksonHttpMessageConverte‌r
i tried to copy paste your spring beans to a project but something strange is wrong with
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverte‌​‌​r"/>
this line, specifically there seems to be some invisible characters before the last r in Converter try typing that classname again manually.
if this is the case then its the craziest thing i have seen for sometime :D
Also MappingJacksonHttpMessageConverter is deprecated in 4.0.0 there is something newer. And you will need to add dependencies for jackson as well to get things working. This should be helpful.
You need to add the following to your pom.xml (not parent pom.xml)
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
</dependencies>
I had the issue, got it fixed by adding following dependencies in pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
Found it here!
Programmaticaly you can do your configuration like this:
public class AppConfiguration {
...
#Bean
public HttpMessageConverters customConverters() {
HttpMessageConverter<?> jacksonMessageConverter = new MappingJackson2HttpMessageConverter();
// HttpMessageConverter<?> another = ...
return new HttpMessageConverters(jacksonMessageConverter);
}
}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
it's worked for me

Categories

Resources