我正在尝试用一个非常简单的例子来教我自己的Spring Java注释配置。但是我收到错误NoSuchBeanDefinitionException。
我的Pom文件有:
<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>SpringDemo</groupId>
<artifactId>SpringDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- JavaConfig need this library -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
</project>
然后我的配置类有:
package org.jorkata;
@Configuration
public class JavaConfig {
@Bean
public Triangle triangle(){
return new Triangle();
}
}
我在我的包中定义了一个简单的类:
package org.jorkata;
public class Triangle {
private int val;
public int getVal() {
return val;
}
public void setVal(int val) {
this.val = val;
}
public void returnVal(){
System.out.println(getVal());
}
}
在我的应用课程中,我有:
package org.jorkata;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
public class DrawingApplication {
public static void main(String[] args){
ApplicationContext ctx =
new AnnotationConfigApplicationContext(JavaConfig.class);
Triangle triangle1 = (Triangle) ctx.getBean(Triangle.class);
Triangle tr = ctx.getBean(Triangle.class);
}
}
我得到错误线程“main”org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义[org.jorkata.Triangle]类型的限定bean