我开始使用Spring进行依赖注入。
我添加了jar spring-context-4.0.1
添加了@Configuration
@Configuration
public class SpringConfig {
@Bean
public CategoryDAO categoryDAO() {
return new HibernateCategoryDAO();
}
}
public interface CategoryDAO{
}
和
public class HibernateCategoryDAO implements CategoryDAO{
}
然而,当我尝试@autowire时
@Repository
public class MyCategoryManager implements CategoryManager {
@Autowired
CategoryDAO categoryDAO;
}
categoryDAO为null,我在调试模式下看到它没有在配置文件中停止。
我错过了什么?
我不想在Spring中使用XML,但是我还需要添加应用程序上下文文件吗?
编辑:
在@Gummyball回答之后,我添加了配置。它没有帮助。
我使用Reflections util:
初始化MyCategoryManager类Set<?> managers = Reflections.getSubTypesOf(CategoryManager.class);
可能是原因吗?我该怎样克服它?
答案 0 :(得分:3)
是的,您需要通过向web.xml添加侦听器类来引导Spring。通过提供@Configuration类(.i.e您的SpringConfig类)作为上下文参数,您的配置类将被Spring选中。
<web-app>
<!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext instead of the default XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<!-- Configuration locations must consist of one or more comma- or space-delimited fully-qualified @Configuration classes. Fully-qualified packages may also be specified for component-scanning -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>your.own.package.SpringConfig</param-value>
</context-param>
<!-- Bootstrap the root application context as usual using ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
// Your jersey config here
</web-app>
更新
基于官方Jersey Spring Example,我制作了这个hello world示例,它将Spring与Jersey一起使用。
您需要具有以下依赖项:spring framework,jersey-container-servlet和jersey-spring3。或者如果使用maven:
的pom.xml
<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>com.stackoverflow.question.jersey.with.spring</groupId>
<artifactId>SpringConfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringConfig</name>
<properties>
<!-- Spring -->
<spring-framework.version>4.0.1.RELEASE</spring-framework.version>
<jersey.version>2.6</jersey.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
</project>
在web.xml中,您需要配置Spring和Jersey
的web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext instead of the default XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<!-- Configuration locations must consist of one or more comma- or space-delimited fully-qualified @Configuration classes. -->
<!-- Fully-qualified packages may also be specified for component-scanning -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.stackoverflow.question.jersey.with.spring.SpringConfig</param-value>
</context-param>
<!-- Bootstrap the root application context as usual using ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Jersey config -->
<servlet>
<servlet-name>SpringConfig</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.stackoverflow.question.jersey.with.spring.MyApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringConfig</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
这里的诀窍是告诉泽西岛要扫描哪个包。或者,您可以使用寄存器(.class)单独注册每个组件。
MyApplication.java
package com.stackoverflow.question.jersey.with.spring;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spring.scope.RequestContextFilter;
/**
* Spring HelloWorld Web Application configuration.
*
* @author Jakub Podlesak (jakub.podlesak at oracle.com)
*/
public class MyApplication extends ResourceConfig
{
/**
* Register JAX-RS application components.
*/
public MyApplication()
{
packages(true, "com.stackoverflow.question.jersey.with.spring");
}
}
您可以通过向@ComponentScan提供包来告诉Spring使用组件扫描。或者,如果您想要更多地控制Spring bean的初始化方式,您可以使用单独的@Bean注释(就像您在问题中所做的那样)来单独指定每个Spring bean。
SpringConfig.java
package com.stackoverflow.question.jersey.with.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.stackoverflow.question.jersey.with.spring")
public class SpringConfig
{
}
根据您对不同CategoryManager实现的评论,我创建了一个帮助器类,它将根据您提供的枚举类型返回不同的CategoryManager(这将是用户提供的值)。
CategoryManagerFactory.java
package com.stackoverflow.question.jersey.with.spring;
import javax.inject.Inject;
import org.springframework.stereotype.Component;
@Component
public class CategoryManagerFactory
{
@Inject
private MyCategoryManager myCategoryManager;
@Inject
private OtherCategoryManager otherCategoryManager;
public CategoryManager obtainCategoryManager(CategoryManagerTypes type)
{
switch (type) {
case MY:
return myCategoryManager;
case OTHER:
return otherCategoryManager;
default:
throw new IllegalArgumentException(String.format("Category %s not supported", type));
}
}
public enum CategoryManagerTypes
{
MY, OTHER;
}
}
MyCategoryManager.java
package com.stackoverflow.question.jersey.with.spring;
import javax.inject.Inject;
import org.springframework.stereotype.Component;
@Component
public class MyCategoryManager implements CategoryManager
{
@Inject
private CategoryDAO categoryDAO;
@Override
public String saySomething()
{
return "Using MyCategoryManager!";
}
}
OtherCategoryManager.java
package com.stackoverflow.question.jersey.with.spring;
import javax.inject.Inject;
import org.springframework.stereotype.Component;
@Component
public class OtherCategoryManager implements CategoryManager
{
@Inject
private CategoryDAO categoryDAO;
@Override
public String saySomething()
{
return "Using OtherCategoryManager!";
}
}
在泽西资源中使用Spring将所有内容整合在一起:
JerseyResource.java
package com.stackoverflow.question.jersey.with.spring;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import com.stackoverflow.question.jersey.with.spring.CategoryManagerFactory.CategoryManagerTypes;
@Path("jersey-hello")
public class JerseyResource
{
@Inject
private CategoryManagerFactory categoryManagerFactory;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getHello(@Context HttpHeaders headers, @QueryParam("category") CategoryManagerTypes category)
{
CategoryManager categoryManager = categoryManagerFactory.obtainCategoryManager(category);
return categoryManager.saySomething();
}
}
转到http://localhost:8080/SpringConfig/jersey-hello?category=MY
,你会得到'使用MyCategoryManager!'
答案 1 :(得分:0)
从你的代码中不清楚Spring如何知道MyCategoryManager。尝试将其添加到配置
@Configuration
public class SpringConfig {
@Bean
public CategoryDAO categoryDAO() {
return new HibernateCategoryDAO();
}
@Bean
public MyCategoryManager myCategoryManager() {
return new MyCategoryManager();
}
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
System.out.println(ctx.getBean(MyCategoryManager.class).categoryDAO);
}