4

I am doing my best to use little to no XML here. I have made a very simple program but it is not working. Hoping someone could help me out.

public class App { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Logger.class); Logger logger = ctx.getBean(Logger.class); logger.writeConsole("Hello there"); logger.writeFile("Hi again"); ctx.close(); } } 

Interface

public interface LogWriter { public void write(String text); } 

FileWriter

public class FileWriter implements LogWriter { public void write(String text) { System.out.println("FileWriter: " + text); } } 

ConsoleWriter

 public class ConsoleWriter implements LogWriter{ public void write(String text) { System.out.println("Console Writer: "+text); } } 

Logger

public class Logger { @Autowired private ConsoleWriter consoleWriter; @Autowired private FileWriter fileWriter; public void setConsoleWriter(ConsoleWriter consoleWriter) { this.consoleWriter = consoleWriter; } public void setFileWriter(FileWriter fileWriter) { this.fileWriter = fileWriter; } public void writeFile(String text) { fileWriter.write(text); } public void writeConsole(String text) { consoleWriter.write(text); } @Bean public Logger getLogger(){ return new Logger(); } } 

Error

Jul 02, 2015 2:52:49 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@246b179d: startup date [Thu Jul 02 14:52:49 CEST 2015]; root of context hierarchy Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.core.GenericTypeResolver.resolveReturnTypeForGenericMethod(Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Class; at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:650) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:575) at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1344) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:356) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:327) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:644) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461) at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:73) at com.main.application.App.main(App.java:10) 

This is my XML file but I am trying to get away from using XML and just annotations so I made this simple to understand program as practice.

<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:annotation-config></context:annotation-config> </beans> 
3
  • @TilmanHausherr my object is to not use XML. I know there is a way to do it. I just thought my issue would helps others trying to do that same thing. Am I wrong is assuming I have to use XML. I have used JHipster before and it has only one XML file which is bare bones. It is all POJO and Annotations. Commented Jul 2, 2015 at 13:26
  • Maybe this? stackoverflow.com/questions/26050047/… Commented Jul 2, 2015 at 13:29
  • Your Logger should have @Configuration else it won't be used as a configuration class. Next you should separate your configuration from the application code (single responsibility. Judging from the stack trace you have some mixing of jar files (jars from different versions of the framework). Commented Jul 2, 2015 at 13:38

2 Answers 2

5

This would do without any xml configuration

public class App { public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class); Logger logger = ctx.getBean(Logger.class); logger.writeConsole("Hello there"); logger.writeFile("Hi again"); } } 

Config class

@Configuration @ComponentScan public class Config { } 

Console Writer

@Component public class ConsoleWriter implements LogWriter{ public void write(String text) { System.out.println("Console Writer: "+text); } } 

FileWriter

@Component public class FileWriter implements LogWriter { public void write(String text) { System.out.println("FileWriter: " + text); } } 

Looger

@Component public class Logger { @Autowired private ConsoleWriter consoleWriter; @Autowired private FileWriter fileWriter; public void setConsoleWriter(ConsoleWriter consoleWriter) { this.consoleWriter = consoleWriter; } public void setFileWriter(FileWriter fileWriter) { this.fileWriter = fileWriter; } public void writeFile(String text) { fileWriter.write(text); } public void writeConsole(String text) { consoleWriter.write(text); } 

}

Interface LogWriter

public interface LogWriter { public void write(String text); } 
Sign up to request clarification or add additional context in comments.

2 Comments

Perez I am still getting org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.beans.ConsoleWriter] found for dependency:
All the clases are in the same package that the Config.class ? because if not you need to modify @ComponentScan("package where the class found")
4

Assuming you are using Spring 3.2. Your App.java should look like this:

@Configuration @ComponentScan public class App { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(App.class); Logger logger = ctx.getBean(Logger.class); logger.writeConsole("Hello there"); logger.writeFile("Hi again"); ctx.close(); } } 

@Configuration: tells Spring that this class is a configuration Class

@ComponentScan: tells Spring to scan all classes from that package.

No more needs of xml with that starting point.

FileWriter, ConsoleWriter and Logger are Components. When you define Components you can use @Autowired annotation on those classes. You need to add the @Component annotation on these Classes.

Note that @Bean getLogger() method will make an exception on runtime because Spring will find two definitions with name "logger". So to make your application run correctly you need to remove that method.

2 Comments

I did everything you said and I am getting a No qualifying bean of type [com.beans.ConsoleWriter] found for dependency I cleaned the project too just to make sure so caching was going on.
@Componentscan in a class that have package com.app will scan com.app.** So if App.java is in an other package branch then your beans it won't find your beans. use @ComponentScan({"com.beans","com.app"})

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.