尝试在没有IDE注释的情况下实现基于javaconfig的方法



嗨,我正在尝试使用@configuration注释实现spring 3.2示例:

  1. 我已经写好了服务接口:intf.java
  2. 实现的服务接口:impl.java
  3. 编写的Appconfig类正在取代基于xml的方法注入bean

这里我用@configuration注释了Appconfig类。

编写一个方法,创建impl.java的实例并返回intf作为引用

@Bean(name="hw")注释此方法。

使用这两个导入:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

4。使用Main方法编写类

在这里,我使用AnnotationConfigApplicationContext创建了ApplicationContext的实例,给定我的Appconfig.class作为上下文引用的参数,称为getBean方法"hw"被转换并在intf ref上接收intf引用,调用我的服务方法。

使用的导入是:

import org.springframework.context.ApplicationContext;<br>
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

设置类路径为spring-context-3.2.8.RELEASE.jar

我收到如下编译错误:

AppConfig.java:1: error: package org.springframework.context.annotation does not exist
import org.springframework.context.annotation.Bean;
                                   ^
AppConfig.java:2: error: package org.springframework.context.annotation does not exist
import org.springframework.context.annotation.Configuration;
                                   ^
AppConfig.java:5: error: cannot find symbol
@Configuration 
                                   ^
symbol: class Configuration
ConfigReadMain.java:1: error: package org.springframework.context does not exist
import org.springframework.context.ApplicationContext;
                                   ^
ConfigReadMain.java:2: error: package org.springframework.context.annotation does not exist
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
                                   ^
AppConfig.java:10: error: cannot find symbol
 @Bean(name="HW") ^ symbol:   class Bean
location: class AppConfig
ConfigReadMain.java:7: error: cannot find symbol
ApplicationContext actxt = new AnnotationConfigApplicationContext(AppConfig.class);
                                   ^
symbol:   class ApplicationContext location: class ConfigReadMain
ConfigReadMain.java:7: error: cannot find symbol
ApplicationContext actxt = new AnnotationConfigApplicationContext(AppConfig.class);
                                   ^
symbol:   class AnnotationConfigApplicationContext
location: class ConfigReadMain
HelloWorldImpl.java:3: error: printHelloWorld(String) in HelloWorldImpl cannot implement printHelloWorld(String) in HelloWorldInf void printHelloWorld(String msg){
                                   ^
attempting to assign weaker access privileges; was public
9 errors

9个编译错误中有8个似乎是因为spring-context JAR不在您的类路径上。我知道您的问题说您已经在类路径中获得了它,但是编译错误表明并非如此。你用什么来编译?您是否使用不同的技术来编辑代码和构建代码?(例如,在Eclipse中编辑,但在Ant中构建。)如果是这样,请确保该JAR位于这两种技术的类路径中。

最后一个似乎是因为你声明你的接口方法为公共,但你的实现类不是公共的。(可能是默认的,可能是受保护的,可能是私有的,我们不知道,因为你没有发布代码,但无论哪种方式,只要把它公开,你就可以了。)

如果您使用命令行来编译和管理spring框架应用程序,那么您完全走错了路!

让一个美妙的IDE,如eclipse与spring插件,完美地适合您的应用程序!

如果你问我,我使用上面所有的东西,我几乎能从我的web应用程序中得到一些东西,你呢?

我的意思是用记事本和命令行运行和编译一个hello world应用程序,

但是像Spring Framework 3这样复杂的应用,你一定是在开玩笑。

是时候使用一个真正的IDE了。

Note: All the keywords in my answer are hypertexts to enable you surf instantly a proper site.

相关内容

最新更新