2
我一直在尝试使用自动装配,但未能获得自动装配。这里是代码片段,自动装配未在春季发生
应用程序上下文文件:
<context:annotation-config />
<context:component-scan base-package="com.shapes" />
<bean id = "triangle" class = "com.shapes.Triangle" autowire="byName"></bean>
三角类:
@Component
public class Triangle implements Shape {
@Override
public void draw() {
System.out.println("In draw");
}
}
主要类:
public class MainShapes {
@Autowired
private Triangle triangle;
/**
* @param args
*/
public static void main(String[] args) {
MainShapes shapes = new MainShapes();
shapes.triangle.draw();
}
}
它启动你的自动装配Autowired豆你在哪里,你启动应用程序''上下文文件?你必须首先加载'context'来使用'ClassPathxmlApplicationcontext()'初始化bean' – SRy
检查这个例子:http://www.mkyong.com/spring3/spring-3-hello-world-example/ – SRy