在Spring中,@Autowired注释仅引用一个对象吗?
@Autowired
A object_a;
@Autowired
B object_b;
@Autowired
C object_c;
和
@Autowired
A object_a;
B object_b;
C object_c;
它们是一样的吗?谢谢
对于基于Spring的单元测试,我有以下Beans:
abstract class A {
// some injections using @Value
}
class B extends A {
// some injections using @Autowired, @Qualifier, @PersistenceUnit and @Value
}
class C extends A {
// some injections using @Autowired, @Qualifier, @PersistenceUnit and @Value
}
class F
在这门课上@Autowired就是工作
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Autowired
private UserDAO userDAO; // is NOT null
@Autowired
private IArticleService articleService; // is NOT null
public void registerWebSocketHandlers(WebSocke
有时Spring组件可能如下所示:
@Service
public final class SomeService {
@Autowired
private SomeService2 someService2;
@Autowired
private SomeService3 someService3;
@Autowired
private SomeService4 someService4;
// … and many other services
@Autowired
private SomeDao someDao;
我需要使用Spring上下文中的@Autowired字段来填充我的ScalaTest测试,但是大多数Scalatest测试(例如FeatureSpecs不能由SpringJUnit4ClassRunner.class运行-
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="myPackage.UnitTestSpringConfiguration", loader=AnnotationConfigContextLoader.class)
public class AdminLogin
我在一些对象中使用进行bean注入。问题是,beans的注入在jUnit测试中不起作用。用于测试的是SpringJUnit4ClassRunner。
public class DossierReportItemXlsImporterImpl implements DossierRerportItemXlsImporer {
private final Logger logger = Logger.getLogger(getClass());
// are not autowired.
@Autowired
private DossierReportServic
@Component
public class RepositoryContainer {
@Autowired
public CommentRepository commentRepository;
@Autowired
public ItemRepository itemRepository;
@Autowired
public UserRepository userRepository;
@Autowired
public PostRepository postRepository;
}
这样,我只需要注入一个单一的Repo
我在项目中遇到了一个循环依赖的小问题,发现在字段/设置器上使用@Autowired可以解决这个问题,而不是在构造函数上使用@Autowired。这种行为的结论是,Spring在字段/setter注入的情况下注入代理,而在构造函数注入的情况下注入实际的bean。 问题是:这背后的原因是什么?为什么注入不同的实体? 考虑一下简单的代码片段: @Component
public static class A{
@Autowired B b;
@Autowired C c;
}
@Component
public static
我在我的项目中使用spring to mockito Beans,我还使用AutoWired进行测试。
@Service
public class A {
someMethod() { }
}
@Service
public class B {
@Autowired
A a;
someMethod() {
a.someMethod();
}
}
@Service
public class C {
@Autowired
B b;
someMethod() {
b.someMethod();
我有一门课要考试,但我不知道如何测试它。有人能帮忙做测试吗?
@Component
public class KvkkCorrBankingExecuter {
private final Map<String, IKvkkCorrBankingExecuter> operationMap = new HashMap<>();
IKvkkCorrBankingExecuter executer;
@Autowired
DemandBoeExecuter demandBoeExecuter;
@Autowired
D
我有一个名为MainService的服务,它有几个用@Autowired初始化的管理器,它使用了一些也是@Autowired的外部服务。
我的目的是创建单元测试用例,以便我可以使用管理器访问内存中的DB,并希望模拟外部服务。
现在我面临的问题是,如果我在单元测试中使用@Autowired,并将@Mock用于外部服务,那么它就不会使用模拟方法,而是使用实际的实现。如果我使用@InjectMocks,它就不会从存储库中提取数据,因为它找不到管理器各自的依赖项,而且如果我同时使用@Autowired和@InjectMocks,它仍然不能使用Mock。
像这样的东西
@Service
public c
在MyController中,在没有getter/setter的情况下,@Autowired可以很好地导入myService:
@Controller
public class MyController
{
@Autowired
private MyService myService;
但是,当我试图将@Autowired注释应用到MyService的myOtherService字段时,我会发现一个错误,它不能为myOtherService找到必要的setter方法--但是如果我为该字段填充getter和setter方法,它就能工作:
这工作:
@Service
public
在我的项目中,我有三个类,它们与多级继承相关,如下所示
@Component
@Scope("prototype")
class A{
}
@Component
@Scope("prototype")
class B extends A{
}
@Component
@Scope("prototype")
class C extends B{
}
现在,通过使用@Autowired注释,我希望C类对象在管理器类中
@Component(value = "manager")
@Scope("prototype
如果我将一个Lombok连接到项目中,如何使用@autowired注解注入beans? 这些链接上的答案似乎不稳定(支持?): Spring + Lombok: Can I have @Autowired @Setter Spring support in IDEA with Lombok: Is "Navigate to autowired dependencies" supported?
我在下面的测试中嘲笑AuthService,并将AuthService设置为"true“。它实际上是被设置的,但是在实际的method(authService.checkAuthority(UserAuthority.COMMENT_ACCESS))中,它得到的值为false。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(ApplicationRunner.class)
@WebAppConfiguration
@Transactional
public class TagsTest e
我有一个能做出一些动作的系统。此系统使用以下命令为每个对象创建单独的线程: stp.scheduleWithFixedDelay((EditSite) ctx.getBean("EditSite", site), threadTimeParams); 我想创建一个带有"Site“参数的构造函数,但我应该以某种方式保持我的自动连接构造函数工作。 public class EditSite{
private final A a;
private final B b;
private final C c;
private Site site
我正在学习Spring,而我喜欢使用@Component和@Autowired来让Spring管理依赖bean的想法。例如,我可以使用一个Service类和Builder类
// SomeService.java
@Service
public class SomeService {
// SomeBuilder is a @Component class
@Autowired
SomeBuilder someBuilder;
}
// SomeController.java
@Component
public class SomeController {
@
我正在将C类的对象标记为“@Autowired(必需=假)”,仍然spring试图找到它的实例,我收到下面的错误
由: org.springframework.beans.factory.NoSuchBeanDefinitionException:不限定类C类的bean引起的
@Component
@Slf4j
public class Foo {
private A aa;
private B bb;
private C cc;
public Foo(@Autowired final A aa,
@Autowired fina
我有下面的设置
@Service
public class TestDispatcherImpl implements Dispatcher <MobilePushNotification> {
@Autowired
private A a;
@Autowired
private B b;
@Autowired
private C c;
@PostConstruct
public void initialize() {} {
b.someMethod(); //GETTING A NULL POIN
我有一个A类,它使用了3个不同的带有自动装配的类
public class A () {
@Autowired
private B b;
@Autowired
private C c;
@Autowired
private D d;
}
当测试它们时,我想只有2个类(B & C)作为模拟,并有D类被自动连接为正常运行,这段代码对我不起作用:
@RunWith(MockitoJUnitRunner.class)
public class aTest () {
@InjectMocks
private A a;