
Spring原型原型中的bean
我试图将一个prototypebean 注入一个bean,singleton以便对单例bean方法的每个新调用都具有原型bean的新实例。考虑如下的单例豆: @Component public class SingletonBean { @Autowired private PrototypeBean prototypeBean; public void doSomething() { prototypeBean.setX(1); ...
2024-01-10
Spring bean范围
有人可以解释一下我一直只使用“原型”的Spring bean的作用域吗,但是还有其他参数可以代替吗?我在说什么的例子<bean id="customerInfoController" class="com.action.Controller" scope="prototype"> <property name="accountDao" ref="accountDao"/> <property name="utilityDao" ref="utilityDao"/> <property na...
2024-01-10
在Spring的运行时动态声明bean
我想知道以下情况是否可能。出于测试目的,我希望在应用程序上下文中为不同的测试声明不同的模拟类。这些是使用Jersey REST客户端的验收测试。有没有办法在运行时动态声明bean?Spring是否有API允许在加载上下文后更改应用程序上下文?回答:关于第一个问题,你可以在运行时通过BeanDefinitionRegistry....
2024-01-10
在spring测试中要求范围内的bean
我想在我的应用程序中使用请求范围的bean。我使用JUnit4进行测试。如果我尝试在这样的测试中创建一个:@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath:spring/TestScopedBeans-context.xml" })public class TestScopedBeans { protected final static Logger logger = Logger...
2024-01-10
在spring测试中请求范围内的bean
我想在我的应用程序中使用请求范围的bean。我使用JUnit4进行测试。如果我尝试在这样的测试中创建一个:@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath:spring/TestScopedBeans-context.xml" })public class TestScopedBeans { protected final static Logger logger = Logger...
2024-01-10
在Spring的运行时注册bean(原型)
只需要一些由社区评估的东西。以下是一段代码,这是一个创建特定类型实例的简单工厂。该方法将在上下文中将bean注册为原型并返回实例。这是我第一次在运行时配置bean。你能否评价并提供反馈?先感谢你。package au.com.flexcontacts.flexoperations;import org.springframework.beans.BeansException;import org.springframework....
2024-01-10
在Spring中需要多个相同类型的bean
将请求标记为重复之前的请求。我浏览了论坛,在任何地方都找不到解决问题的方法。我正在使用Spring 3.2编写代码,所有内容都完全基于注释。该代码接收从不同XSD文件派生的XML文件。可以这么说,有五个不同的XSD(A1,A2,A3,A4,A5),我的代码可以接收任何类型的XML,并且我有逻辑可以在到达时识...
2024-01-10
具有动态构造函数值的Spring bean
我需要创建一个没有构造函数参数的不完整对象。像这样Class A { private final int timeOut public A(int timeout) { this.timeOut = timeout; } //...}我希望这个Bean是Spring管理的,以便以后可以使用Spring AOP。<bean id="myBean" class="A" singleton="false"></bean>但是我的bean需要将超时作为动态值传递-是否有一种方法可...
2024-01-10
如何从Spring获取实例化的bean列表?
我在Spring上下文中有几个具有状态的bean,所以我想在单元测试之前/之后重置该状态。我的想法是向助手类添加一个方法,该方法仅遍历Spring上下文中的所有bean,检查用@Before或注释的方法@After并调用它们。如何从中获取实例化的 bean 列表ApplicationContext?注意:仅迭代所有定义的bean的解决方案是没有...
2024-01-10
是否可以取消对Spring bean的代理?
我有一个Spring bean,可以这样说:@TransactionAttribute(TransactionAttributeType.REQUIRED) public class AImpl implements A { public void setSomeDependency(D dependency) { // This setter DOES NOT BELONG to interface A }}<bean id="aImpl" class="AImpl"/>现...
2024-01-10
如何禁用特定bean的Spring自动装配?
jar(外部库)中有一些类在内部使用Spring。因此,库类具有如下结构:@Componentpublic class TestBean { @Autowired private TestDependency dependency; ...}库提供用于构造对象的API:public class Library { public static TestBean createBean() { ApplicationContext context = new Annotati...
2024-01-10
如何在Spring获得相同bean的多个实例?
默认情况下,spring bean是单例。我想知道是否有一种方法可以获取同一bean的多个实例进行处理。这是我目前正在做的 @Configuration public class ApplicationMain { @Value("${service.num: not configured}") private int num; //more code@PostConstructpublic void run(){ for (int i = 0; i < num...
2024-01-10
将bean注入Spring托管上下文之外的类中
我是公司产品之一的最终用户。它不太适合集成到Spring中,但是我能够获取上下文的句柄并按名称检索所需的bean。但是,我仍然想知道是否可以将bean注入到此类中,即使该类不是由Spring本身管理的。说明:管理某个类MyClass的生命周期的同一应用程序也正在管理Spring上下文的生命周期。Spring没有MyClass...
2024-01-10
带有运行时构造函数参数的Spring bean
我想在 创建一个Spring bean,并在运行时传递一些构造函数参数。我创建了以下Java配置,其中有一个bean ,它在构造函数中需要一些参数。@Configurationpublic class AppConfig { @Autowrire Dao dao; @Bean @Scope(value = "prototype") **//SourceSystem can change at runtime** public FixedLengthReport fixe...
2024-01-10
Spring-排除包中的Bean被扫描
如果我的com.xyz.abc软件包中有大约50个春豆,并且想将其中的2个豆排除为豆之外,是否可以这样做?我正在使用Spring Boot。@ComponentScan({'com.xyz.abc'})有一个类Automobile.class,我不想将其视为Spring Bean。但是我有Car.class,它扩展了Automobile以被视为springbean。回答:您可以excludeFilters使用@ComponentScan注释的参...
2024-01-10
打印所有已加载的Spring Bean
有没有办法打印所有在启动时加载的spring bean?我正在使用Spring 2.0。回答:是,获取ApplicationContext并调用.getBeanDefinitionNames()你可以通过以下方式获取上下文:implementing ApplicationContextAwareinjecting it with @Inject / @Autowired (after 2.5)use WebApplicationContextUtils.getRequiredWebApplicationCo...
2024-01-10
在不同环境下定义Spring bean的通用策略
定义一堆bean的常见策略是什么,在开发和生产环境中使用的策略不同?假设我有2个bean,每个bean实现相同的接口。一个bean充当本地文件系统的抽象,另一个连接到分布式文件系统。为了保持开发的稳定,开发环境应使用本地文件系统实现,生产发行版应使用分布式文件系统bean。目前,我正在做的是...
2024-01-10
用非主beanSpring覆盖主bean
我正在尝试使用@Primary在测试配置中声明的测试中覆盖Spring bean。一个声明位于src / main /java路径中,另一个声明主要位于src / test / java路径中。但是,Spring故意将非主要bean替换为主要bean,我不想在测试中使用该bean。如果仅将生产(src / main /java)配置bean注释掉,它会根据需要在测试配置中使用主测试...
2024-01-10
Spring-以编程方式生成一组bean
我有一个Dropwizard应用程序,需要为配置列表中的每个配置生成十几个bean。像这样:@Componentclass MyModule { @Inject private MyConfiguration configuration; @Bean @Lazy public QuartzModule quartzModule() { return new QuartzModule(quartzConfiguration()); } @Bean ...
2024-01-10
具有私有构造函数的Java Spring bean
bean的类可能没有公共构造函数,而只有私有构造函数吗?创建bean时会调用此私有构造函数吗?谢谢。回答:Spring可以调用私有构造函数。如果找到具有正确参数的构造函数,无论可见性如何,它将使用反射将其构造函数设置为可访问的。...
2024-01-10
SPring:确保首先初始化特定的bean
我有一个库在运行时设置和配置log4j(没有log4j.properties或log4j.xml)。我已经定义了一个名为MyLoggerFactory的类,并且我希望它是第一个使用spring初始化的bean。我已经看到spring已经提出了一个支持初始化顺序的问题,但是我想知道是否有一种方法可以将bean标记为spring容器初始化的第一个bean?回答:你的...
2024-01-10
Spring抛出错误:Bean定义是抽象的
我需要有关下一个主题的帮助。我有一个豆的层次结构,就像这样:<bean id="father" class="myPackage.Father" abstract="true"/><bean id="childA" class="myPackage.ChildA" parent="father"> <property name="atrib1" value="hello"></property> </bean><bean id="childB" class="myPackage.Ch...
2024-01-10
Hadoop Pig加入来自Hbase的数据
我试图从文本文件中加入来自hbase的数据,但失败。Hadoop Pig加入来自Hbase的数据这是我使用的数据。C:(XXX1,06820,7) (XXX2,07442,9)G1:(XXX1,53) (XXX2,54)G2:(XXX1,53) (XXX1,53)G1从文本文件加载,G2从HBase加载。G1:{SN:字节组,国家:chararray}G2:{SN:字节组,国家:chararray}J1 =由SNJ2加入由SN...
2024-01-10
注册为Spring bean时过滤器调用两次
我想@Autowire搭配使用Filter。所以我在SecurityConfig下面定义我的过滤器: @Override protected void configure(HttpSecurity http) throws Exception { http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); http.addFilterBefore(getA(), Ba...
2024-01-10
Spring是否要求所有bean具有默认构造函数?
我不想为我的auditRecord班级创建默认的构造函数。但是Spring似乎坚持:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'auditRecord' defined in ServletContext resource[/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested excepti...
2024-01-10
