我试图将特定配置文件的所有 bean 定义放在一起,并且不想将它们全部推入一个巨大的 AppConfig.java 类中。我想知道是否有一种方法可以使用 package-info.java 在包级别进行注释,并使该包中的所有配置文件继承该配置文件。
我在 package-info.java 中尝试了以下操作:
@Profile("test")
package com.system.configuration.test;
import org.springframework.context.annotation.Profile;
Run Code Online (Sandbox Code Playgroud)
但是@Configuration无论是否是“测试”配置文件,包中的类似乎都会被使用。
单独注释每个类是唯一的选择吗?
我正在尝试将 BouncyCastle 添加到我的 Spring 应用程序中,但我不确定如何java.security.Security使用 JavaConfig 将提供程序添加到提供程序列表中。
使用 XML 配置,我可以使用MethodInvokingFactoryBean类似以下内容:
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="java.security.Security.addProvider"/>
<property name="arguments">
<list>
<bean class="org.bouncycastle.jce.provider.BouncyCastleProvider"/>
</list>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
但是,我不确定使用 JavaConfig 执行此操作的正确方法。我还应该使用吗MethodInvokingFactoryBean?我想因为它是纯java,所以会有更直接的方法。目前,我已将该指令添加到@PostConstructJavaConfig 对象中的一个方法中,但对此并不太兴奋 - 对我来说这似乎有点“hacky”:
@Configuration
public class AppConfig {
// other @Bean definitions
@PostConstruct
public void init(){
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下组件类,我想根据属性实例化它;
@Component("componentA")
@PropertySource("classpath:components.properties")
@ConditionalOnExpression("'${components.enabled}'.contains('componentA')")
public class ComponentA implements ComponentIF {
...
Run Code Online (Sandbox Code Playgroud)
components.properties 文件具有以下属性;
components.enabled=componentA,componentB,componentD
Run Code Online (Sandbox Code Playgroud)
问题是@PropertySource("classpath:components.properties")在评估@ConditionalOnExpression("'${components.enabled}'.contains('componentA')").
另一方面,如果我将components.enabled=componentA,componentB,componentD属性放在 spring-boot 的application.properties文件中,则该属性在 ConditionalOnExpression 评估期间可用,并且按预期工作。
但是,我想使用components.properties以将所有组件特定的属性保持在同一位置。
任何想法是否 PropertySource 在 ConditionalOnExpression 期间无效?
您能帮我将以下基于 spring xml 的配置转换为基于 Java 的 bean 配置吗?
<jaxws:client id="helloClient"
serviceClass="demo.spring.HelloWorld"
address="http://localhost:9002/HelloWorld" />
Run Code Online (Sandbox Code Playgroud) 我的代码中有以下注释:
@ComponentScan(basePackageClasses={MyClass.class},
excludeFilters={@Filter(Component.class)}, //@Component
includeFilters={@Filter(type=ASSIGNABLE_TYPE, classes=MyClass.class)}
)
Run Code Online (Sandbox Code Playgroud)
MyClass带有注释@Component但仍希望在组件扫描期间包含它。但是,组件扫描过滤器似乎使用和逻辑而不是或。我如何实现我想做的事?
我一直在使用@EnableJpaRepositories并且我对定义特定类而不是包的机会感兴趣。这背后的原因是我正在使用多模块项目,并且目前有一个核心模块,其中包含单独包中的所有存储库定义:
core/repository/ - Here all repository definitions are stored
Run Code Online (Sandbox Code Playgroud)
在依赖于核心模块的其他模块中,我使用以下定义来获取存储库:
@EnableJpaRepositories(basePackages ="core.repository")
Run Code Online (Sandbox Code Playgroud)
显然,使用这意味着获取core/repository包下所有存储库的定义。但是,在某些包中,我只需要一些存储库,而不是全部。现在,我已将每个存储库定义移动到一个单独的包中,例如:
core/repository/user
Run Code Online (Sandbox Code Playgroud)
不过我很感兴趣 - 是否真的可以定义具体的存储库类,而不是包,类似这样:
@EnableJpaRepositories(baseClasses ="core.repository.UserRepository")
Run Code Online (Sandbox Code Playgroud) 在 java config 中使用 OAuth2 配置 Spring Security,并且client_credentails流工作正常,但密码流抛出Handler dispatch failed;嵌套异常是 java.lang.StackOverflowError下面是日志信息
2016-10-10 23:19:08 DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [public org.springframework.http.ResponseEntity<org.springframework.security.oauth2.common.OAuth2AccessToken> org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken(java.security.Principal,java.util.Map<java.lang.String, java.lang.String>) throws org.springframework.web.HttpRequestMethodNotSupportedException]: org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.StackOverflowError
这是我的配置文件:
授权服务器配置.java
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Autowired
DataSource dataSource;
@Autowired
private UserDetailsService userDetailsService;
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Override
public void configure(
AuthorizationServerSecurityConfigurer oauthServer)
throws Exception {
oauthServer
.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) …Run Code Online (Sandbox Code Playgroud) spring spring-mvc spring-security spring-security-oauth2 spring-java-config
我有一个项目,其中包含许多使用 @Configuration 类(Spring Integration 和 Spring Batch 的东西)定义 bean 的小库,我经常遇到问题,因为这两个库都有一个同名的 bean。
可以:
Spring中是否有一种机制可以提供通用bean构建自动化的方法?
例如,如果我有一个类定义,例如:
class Foo<T> {
private final T type;
...
}
Run Code Online (Sandbox Code Playgroud)
和依赖如:
@Autowired
private Foo<String> foo;
Run Code Online (Sandbox Code Playgroud)
我想在 Spring 中使用某种机制,以某种形式从依赖定义中提供 T(在上面的示例中为 String),并提供自动实例创建的方法?
我想在我的@Configuration类 bean 中进行配置,该 bean 已经由其他库的自动配置创建。我只需要在初始化后更改该类中的一些字段。
但是我找不到如何在@Configuration类中提供代码块而不使用@Bean注释的正确方法。有没有一种在春天这样做的理想方法?