使用Spring 3.XX我有2个使用@Primary注释的服务,我创建了另一个配置类,我想使用其中一个服务的"自定义"版本.
出于某种原因,我在调试配置类时忽略了我看到它获得了正确的依赖关系,但是当我想使用它时,它设置了错误的依赖关系.
我发现使用代码更容易解释,这是一个例子:
界面Foo:
public interface Foo {
String getMessage();
}
Run Code Online (Sandbox Code Playgroud)
使用硬编码的默认消息的主要实现:
@Primary
@Service("FooImpl")
public class FooImpl implements Foo {
private String message = "fooDefaultMessage";
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Run Code Online (Sandbox Code Playgroud)
栏界面:
public interface Bar {
void doBar();
}
Run Code Online (Sandbox Code Playgroud)
栏默认实施:
@Primary
@Service("BarImpl")
public class BarImpl implements Bar {
private Foo foo;
@Override
public void doBar() {
System.out.println(foo.getMessage());
}
public Foo getFoo() {
return foo;
}
@Autowired
public …
Run Code Online (Sandbox Code Playgroud) 是否可以在运行时重新初始化Spring Bean?
我的Bean使用静态设置,在某些情况下会更改,然后我必须重新初始化Bean。
我正在学习带有 Java 配置(无 xml)的 Spring MVC,我有一个简单的问题。我看到有两种配置 Spring bean 的方法:
方法一:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.demo.springmvc")
public class DemoAppConfig {
// define a bean for ViewResolver
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
Run Code Online (Sandbox Code Playgroud)
方法2:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example")
public class SpringConfig implements WebMvcConfigurer{
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
Run Code Online (Sandbox Code Playgroud)
因此,一种方法是实现 WebMvcConfigurer 接口,另一种方法是不实现 WebMvcConfigurer 接口。我想问你有什么区别?当我实现这个接口时会发生什么,当我不实现它时会发生什么。任何反馈将不胜感激。
基于此答案,我尝试使用java.util.Function
接口配置请求范围Bean 。
我的配置如下所示:
@Configuration
public class RequestConfig {
@Bean
public Function<? extends BaseRequest, RequestWrapper<? extends BaseRequest, ? extends BaseResponse>> requestWrapperFactory() {
return request -> requestWrapper(request);
}
@Bean
@RequestScope
public RequestWrapper<? extends BaseRequest, ? extends BaseResponse> requestWrapper(
BaseRequest request) {
RequestWrapper<?, ?> requestWrapper = new RequestWrapper<BaseRequest, BaseResponse>(request);
return requestWrapper;
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试像这样使用bean:
@RestController
public class CheckRequestController {
private final RequestService<CheckRequest, CheckResponse> checkRequestServiceImpl;
@Autowired
private Function<CheckRequest, RequestWrapper<CheckRequest, CheckResponse>> requestWrapperFactory;
public CheckRequestController(
RequestService<CheckRequest, CheckResponse> checkRequestServiceImpl) {
super();
this.checkRequestServiceImpl = …
Run Code Online (Sandbox Code Playgroud) 我已经声明了两个相同类类型的bean.初始化它们@Lazy
.@Autowiring
它们中的一个bean也自动初始化了另一个bean.我很惊讶地看到这种行为.只是好奇了解更多有关机制的信息.
码
//bean
public class HelloWorld {
public HelloWorld(String msg){
System.out.println( msg + ", " + this);
}
}
@Configuration
@Lazy
public class SpringAppContext {
@Bean(name="helloworld1")
public HelloWorld helloworld1(){
return new HelloWorld("helloworld1");
}
@Bean(name="helloworld2")
public HelloWorld helloworld2(){
return new HelloWorld("helloworld2");
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={SpringAppContext.class})
public class SpringBeanLazyLoadTest {
@Autowired
private HelloWorld helloworld2;
@Test // this test is lame but just trying out.
public void print(){
System.out.println("Autowired: " + helloworld2);
}
}
Run Code Online (Sandbox Code Playgroud)
产量
helloworld2, my.entp.spring.HelloWorld@3a9bba
helloworld1, my.entp.spring.HelloWorld@163f7a1 …
Run Code Online (Sandbox Code Playgroud) 我有一个带@Service
注释的类,它提供了我可以在所有项目中使用的核心功能:
@Service
public class MyService {}
Run Code Online (Sandbox Code Playgroud)
和另一个扩展它以实现项目特定的东西:
@Service
public class ExtendedMyService extends MyService {}
Run Code Online (Sandbox Code Playgroud)
现在我想配置一个bean别名,以便@Qualifier("MyServiceAlias")
在使用属性自动装配它时使用:
# MyService qualifier (default: myService)
myService.qualifier=extendedMyService
Run Code Online (Sandbox Code Playgroud)
在XML中它看起来像:
<alias name="${myService.qualifier}" alias="MyServiceAlias" />
Run Code Online (Sandbox Code Playgroud)
这里也讨论了它,但我只需要使用XML,JavaConfig.是否可能以及如何实现?
我们的应用程序中有几个DB连接,因此JPA有几个配置.配置只有架构名称,数据库主机名等不同.其余如休眠设置等(通常)相同.这导致多个HibernateJpaVendorAdapter,数据源等bean.它们都需要不同的名称,以免它们发生碰撞.我们目前手动设置如下:
@Configuration
@Bean
public class FooDbConfig {
public DataSource fooDataSource() {
return ...;
}
// ... more beans like HibernateJpaVendorAdapter etc.
}
@Configuration
@Bean
public class BarDbConfig {
public DataSource barDataSource() {
return ...;
}
// ... more beans like HibernateJpaVendorAdapter etc.
}
Run Code Online (Sandbox Code Playgroud)
这当然是非常脆弱的维护.
我们想要一些java配置"Configurer",通过传递bean名称前缀来创建必要bean的设置.然后它应该创建具有不同名称的所有必需的bean(数据源等),前缀为给定的前缀(例如"fooDataSource"和"barDataSource").
这样做的好方法是什么?
我怎么能用java配置以编程方式生成bean别名?
我定义了一个带注释配置的类
@Configuration
@AutoConfigureAfter(EndpointAutoConfiguration.class)
public class EndpointConfiguration {
@Resource
private MetricsEndpoint metricsEndpoint;
@Bean
public MetricsFormatEndpoint metricsFormatEndpoint() {
return new MetricsFormatEndpoint(metricsEndpoint);
}
}
Run Code Online (Sandbox Code Playgroud)
MetricsFormatEndpoint运行良好.
但我使用注释conditionalOnBean,它根本不起作用.
@Bean
@ConditionalOnBean(MetricsEndpoint.class)
public MetricsFormatEndpoint metricsFormatEndpoint() {
return new MetricsFormatEndpoint(metricsEndpoint);
}
Run Code Online (Sandbox Code Playgroud)
看到localhost:8080/beans,spring applicationContext有bean'metricsEndpoint',
{"bean":"metricsEndpoint","scope":"singleton",
"type":"org.springframework.boot.actuate.endpoint.MetricsEndpoint",
"resource":"class path resource
[org/springframework/boot/actuate/autoconfigure/EndpointAutoConfiguration.class]",
"dependencies":[]}
Run Code Online (Sandbox Code Playgroud)
我阅读了注释@ConditionalOnBean的文档,它说的是应该检查的bean的类类型.当指定的任何类包含在{@link ApplicationContext}中时,条件匹配.
谁能告诉我为什么
我在考虑Spring中的bean的延迟初始化.对我来说,这里的"懒惰"意味着在引用bean时会创建一个bean,这一点并不十分清楚.
我期望Spring中的延迟初始化支持是不同的.我认为这是一个基于"方法调用"的懒惰创作.我的意思是,无论何时在方法上调用任何方法,都会创建它.
我认为这可以通过创建特定bean的代理实例并在任何方法调用上进行初始化来轻松解决.
我错过了为什么没有实现的东西?这个概念有什么问题吗?
任何反馈/想法/答案将不胜感激.
多谢你们!
我没有使用xml配置来定义bean.而是使用组件扫描和autowire来定义和注入依赖项.RestTemplate是springframework的一部分.我怎么能注入这个课程?
spring dependency-injection autowired resttemplate spring-bean
spring-bean ×10
java ×8
spring ×8
spring-boot ×3
autowired ×1
resttemplate ×1
spring-aop ×1
spring-mvc ×1