标签: autowired

Spring @Autowired 在没有上下文的情况下工作:annotation-config

我已经测试了自动连接的行为,以防应用程序上下文 xml 文件中缺少 context:annotation-config 元素。令我惊讶的是,它的工作原理是一样的。

所以这是我的问题:即使应用程序上下文配置文件中缺少 context:annotation-config 元素,如何在 ApplicationContext 中注册 AutowiredAnnotationBeanPostProcessor,或者还有什么机制使此配置起作用?

我使用的是 Spring 版本 3.0.6.RELEASE 这是项目 pom 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-    v4_0_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework.samples.spring</groupId>
    <artifactId>spring-utility</artifactId>
    <version>1.0.0.CI-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Spring Utility</name>
    <url>http://www.springframework.org</url>
    <description>
        <![CDATA[
  This project is a minimal jar utility with Spring configuration.
]]>
    </description>
    <properties>
        <maven.test.failure.ignore>true</maven.test.failure.ignore>
        <spring.framework.version>3.0.6.RELEASE</spring.framework.version>
    </properties>   
<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.framework.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin> …
Run Code Online (Sandbox Code Playgroud)

spring annotations autowired

0
推荐指数
1
解决办法
2816
查看次数

NullPointerException 在抽象类的实现中的 Autowired 对象上

我的代码有一个奇怪的问题。我已经实现了template method pattern提供相同算法的不同实现。所以我创建了抽象类:

@Service
public abstract class ExcelRdi_Iupr_Sl {
    @Autowired
    private Environment env;

    private static String PROPERTY_NAME_FILESYSTEM_EXCELPATH = "temporary.excelpath";
    private XSSFWorkbook workbook; 
    private XSSFCellStyle unlockedNumericStyle;
    private XSSFSheet sheet;

    /**
     * Create the excel file with the RDI,IUPR or SL
     * @param car
     * @param fileName
     * @return
     * @throws Exception
     */
    public final String retrieve(Car car, String fileName) throws Exception{
        writeInitialize();
        Map<Integer,Integer> defMap = firstTwoRow(car, sheet);
        elaboration(car, sheet, unlockedNumericStyle, defMap);
        return writeFile(car, fileName);
    }

    /**
     * Import the …
Run Code Online (Sandbox Code Playgroud)

java spring design-patterns abstract autowired

0
推荐指数
1
解决办法
714
查看次数

我正在尝试使用 @qualifier 自动装配我的 bean,但即使在添加限定符之后,我也会收到“required single bean”异常

BubbleSort.java 该类实现SortAlgorithm接口

package com.prakash.Spring.Example;

import org.springframework.stereotype.Component;

@Component
@Qualifier("bubbleSort")
public class BubbleSort implements SortAlgorithm {

    public void sort() {
        System.out.println("Sort from Bubble Sort");
    }
}
Run Code Online (Sandbox Code Playgroud)

QuickSort.java 该类实现了SortAlgorithm接口包com.prakash.Spring.Example;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.context.annotation.Primary;

import org.springframework.stereotype.Component;

@Component
@Qualifier("quickSort")
public class QuickSort implements SortAlgorithm {

    @Override
    public void sort() {
        System.out.println("Sort from Quick Sort");
    }

}
Run Code Online (Sandbox Code Playgroud)

排序算法.java

package com.prakash.Spring.Example;

public interface SortAlgorithm {
 void sort();
}
Run Code Online (Sandbox Code Playgroud)

BinarySearch.java 在这个类中,我想使用 fastSort bean 作为我的组件。

package com.prakash.Spring.Example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class BinarySearch {

    @Autowired …
Run Code Online (Sandbox Code Playgroud)

qualifiers autowired spring-boot

0
推荐指数
1
解决办法
2912
查看次数

@Autowired 在同一控制器的其他方法中无法在一个控制器中正常工作

当我将新的 @RequestMapping 方法添加到控制器时,@Autowired 突然停止工作。为什么我不明白。当我尝试使用这个新方法时,所有 @Autowired 接口都变为空。当我将其他 @RequestMapping 用于其他方法时,一切正常。

在此处输入图片说明

当我尝试使用新的@RequestMapping 时,第一张图片显示所有@Autowired 类为空。

在此处输入图片说明

这我使用其他@RequestMapping 都可以完美运行。这是 spring Rest Api 应用程序。我以前没有遇到过这些事情。什么可能是这样的应用程序。

java spring autowired spring-restcontroller

-1
推荐指数
1
解决办法
60
查看次数

在 Symfony 中注入“容器”

由于 Symfony 5.1 通过自动装配注入容器已Psr\Container\ContainerInterface被弃用,请参阅https://symfony.com/doc/current/service_container.html#public-versus-private-services

如果服务仍然需要访问容器,将来是否需要手动注入,或者是否有其他更方便的注入方式?

一种选择是使用按名称自动装配并注入它,只要有一个名为 的变量$container,但这感觉像是一种黑客攻击。

问题:如果在 Symfony 5.1+ 中服务仍然依赖于容器,那么授予服务访问容器的最佳实践是什么?

php dependency-injection autowired symfony

-1
推荐指数
1
解决办法
1541
查看次数

当从另一个类调用使用它的方法时,为什么自动装配对象为空?

我正在做一个 Spring Boot 2.1.6 项目。

ScheduledTasks当我有一个自动装配的对象时,我有一个类db,它允许我访问jdbcTemplate,以便我可以执行查询。start当我从另一个文件的 main调用时,该db对象为空。如果我将该start方法直接放在主类中db则不为空。

我不确定问题是什么。我放置@Component注释,ScheduledTasks以便 Spring 知道我的自动装配对象。我缺少什么?

这是我的ScheduledTasks

@Component
public class ScheduledTasks {
    private Logger log = VastLogger.getLogger(TrackingEventController.class);

    @Autowired
    private DBHandler db;

    public void start() {
        if (db == null) {
            log.info("db is null from parent");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的主要课程:

@SpringBootApplication
@EnableJms
public class ServerMain implements CommandLineRunner {
    private static final Logger log = LogManager.getLogger(ServerMain.class);

    @Autowired
    private DBHandler …
Run Code Online (Sandbox Code Playgroud)

java spring autowired spring-boot

-3
推荐指数
1
解决办法
347
查看次数