gpt4 book ai didi

java - 未调用 Spring Java 配置

转载 作者:行者123 更新时间:2023-11-30 08:25:40 24 4
gpt4 key购买 nike

我开始使用 Spring 进行依赖注入(inject)。

我添加了 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 {
}

但是,当我尝试在 Component(更准确地说是 Repository)中 @Autowired

@Repository
public class MyCategoryManager implements CategoryManager {

@Autowired
CategoryDAO categoryDAO;

}

categoryDAO 为 null。此外,我看到在 Debug模式下单步执行代码时它从不执行配置文件。

我错过了什么?

我不想在 Spring 中使用 XML,但我是否仍需要添加应用程序上下文文件?

编辑:

按照@Gummyball 的回答,我添加了配置。它没有帮助。

我使用 Reflections util 初始化 MyCategoryManager 类:

Set<?> managers = Reflections.getSubTypesOf(CategoryManager.class);

会不会是这个原因?我该如何克服它?

最佳答案

是的,您需要通过向 web.xml 添加一个监听器类来引导 Spring。通过提供您的 @Configuration 类(即您的 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 ,我制作了这个将 Spring 与 Jersey 一起使用的 hello world 示例。

您需要具有以下依赖项: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

网络.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>

这里的技巧是告诉 Jersey 要扫描哪些包。或者,您可以使用 register(.class) 单独注册每个组件。

我的应用程序.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!";
}
}

并在 Jersey 资源中使用 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 你会得到“Using MyCategoryManager!”

关于java - 未调用 Spring Java 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22092292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com