Java RESTfulWeb服务教程中的自动连接
我已经下载了教程zip fom。它包含带有main方法的应用程序类Java RESTfulWeb服务教程中的自动连接,java,spring,rest,maven,Java,Spring,Rest,Maven,我已经下载了教程zip fom。它包含带有main方法的应用程序类 package hello; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.context.annotation.ComponentScan; @ComponentScan @
package hello;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
迎宾课
package hello;
public class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
}
和迎宾控制器
package hello;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
}
这很好,但是我想使用Springbeans(使用applicationContext.xml)创建一些问候对象,所以我创建了src/main/resources目录(我使用maven来配置本教程),并将applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name = "greeting" class="hello.Greeting">
<constructor-arg index="0" value = "25" />
<constructor-arg index="1" value = "Hello!" />
</bean>
</beans>
我怎样才能使这项建设工作?
谢谢。同时添加@Qualifier注释:
@Autowired
@Qualifier("greeting")
Greeting gr;
这意味着Spring没有创建任何Greeting
类的bean,所以我猜Spring没有导入您的文件。
建议使用@ImportResource
因此,这将是一种悲哀:
@ComponentScan
@ImportResource("classpath:/applicationContext.xml")
@EnableAutoConfiguration
public class Application {
...
}
异常仅仅意味着它无法创建bean
GreetingController
,因为它依赖于您的Greeting
类(当您添加它并用@Autowire
注释时)
可能是找不到applicationContext.xml
确保在web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/location/of/your/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
上下文配置位置
/位置/of/your/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
希望这有帮助
@ComponentScan
@ImportResource("classpath:/applicationContext.xml")
@EnableAutoConfiguration
public class Application {
...
}
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/location/of/your/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>