没有XML的Spring Bean Annotation

时间:2015-07-02 13:05:37

标签: java spring-mvc

我正在尽力在这里使用很少甚至没有XML。我做了一个非常简单的程序,但它不起作用。希望有人可以帮助我。

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();
    }

}

接口

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);

   }

}

记录器

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();
    }

}

错误

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)

这是我的XML文件,但我试图摆脱使用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: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>

2 个答案:

答案 0 :(得分:5)

没有任何xml配置

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");
 }
}

配置类

@Configuration
@ComponentScan
public class Config {
}

控制台编写者

@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);
}

}

接口LogWriter

public interface LogWriter {

  public void write(String text);

}

答案 1 :(得分:4)

假设您使用的是Spring 3.2。你的App.java应该是这样的:

@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:告诉Spring这个类是一个配置类

@ComponentScan:告诉Spring扫描该包中的所有类。

不再需要具有该起点的xml。

FileWriter,ConsoleWriter和Logger是组件。定义组件时,可以在这些类上使用@Autowired批注。您需要在这些类上添加@Component注释。

注意@Bean getLogger()方法会在运行时产生异常,因为Spring会找到两个名为“logger”的定义。因此,要使您的应用程序正确运行,您需要删除该方法。