我已经测试了自动连接的行为,以防应用程序上下文 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) 我的代码有一个奇怪的问题。我已经实现了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) 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) 当我将新的 @RequestMapping 方法添加到控制器时,@Autowired 突然停止工作。为什么我不明白。当我尝试使用这个新方法时,所有 @Autowired 接口都变为空。当我将其他 @RequestMapping 用于其他方法时,一切正常。
当我尝试使用新的@RequestMapping 时,第一张图片显示所有@Autowired 类为空。
这我使用其他@RequestMapping 都可以完美运行。这是 spring Rest Api 应用程序。我以前没有遇到过这些事情。什么可能是这样的应用程序。
由于 Symfony 5.1 通过自动装配注入容器已Psr\Container\ContainerInterface被弃用,请参阅https://symfony.com/doc/current/service_container.html#public-versus-private-services
如果服务仍然需要访问容器,将来是否需要手动注入,或者是否有其他更方便的注入方式?
一种选择是使用按名称自动装配并注入它,只要有一个名为 的变量$container,但这感觉像是一种黑客攻击。
问题:如果在 Symfony 5.1+ 中服务仍然依赖于容器,那么授予服务访问容器的最佳实践是什么?
我正在做一个 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) autowired ×6
spring ×4
java ×3
spring-boot ×2
abstract ×1
annotations ×1
php ×1
qualifiers ×1
symfony ×1