我的 spring.xml 如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
">
<context:component-scan base-package="com.mkyong.service" />
<context:annotation-config />
</beans>
我有 Test1 课
package com.mkyong.service;
import org.springframework.stereotype.Component;
@Component("test1")
public class Test1 {
public int i=1;
}
我有一个应用类
package com.mkyong.common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.service.Test1;
public class App
{
@Autowired
Test1 test1;
public static void main( String[] args )
{
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring.xml");
App app=new App();
System.out.println(app.test1);
}
}
但我得到的输出为空。我不能正确地自动装配它。我做错了什么?