天道酬勤,学无止境

spring-java-config

如何根据应用程序参数选择要运行的 spring 批处理作业 - spring boot java 配置(how to select which spring batch job to run based on application argument - spring boot java config)

问题 我在同一个项目中有两个独立的 spring 批处理作业,因为我想使用相同的基础设施相关 bean。 一切都在 Java 中配置。 我想知道是否有一种正确的方法可以根据例如 main 方法中的第一个 java app 参数来独立启动作业。 如果我运行SpringApplication.run只有第二个作业被魔术执行。 主要方法如下所示: @ComponentScan @EnableAutoConfiguration public class Application { public static void main(String[] args) { SpringApplication app = new SpringApplication(Application.class); app.setWebEnvironment(false); ApplicationContext ctx= app.run(args); } } 并且这两个作业的配置如 Spring.io 上的 Spring Batch 入门教程中所述。 这是第一个作业的配置文件,第二个以相同的方式配置。 @Configuration @EnableBatchProcessing @Import({StandaloneInfrastructureConfiguration.class

2021-11-28 13:23:44    分类:技术分享    spring   spring-batch   spring-boot   spring-java-config

如何使用 Spring Security Java 配置将 HTTP 请求重定向到 HTTPS?(How to redirect HTTP requests to HTTPS using Spring Security Java configuration?)

问题 我有一个 Spring Security 3.2.3 版应用程序,它可以同时侦听 HTTP 和 HTTPS。 我希望对 HTTP 端口的任何请求都重定向到 HTTPS。 如何仅使用 Java 进行配置? Spring Security javadoc for HttpSecurity 提出了以下解决方案(修剪到本质): public class WebSecurityConfig extends WebSecurityConfigurerAdapter { protected void configure(HttpSecurity http) { http.channelSecurity().anyRequest().requiresSecure(); } } 但是这不起作用,因为HttpSecurity没有方法channelSecurity() 。 回答1 在问题的代码中用requiresChannel()替换channelSecurity()似乎给出了所需的行为。 工作代码如下所示: public class WebSecurityConfig extends WebSecurityConfigurerAdapter { protected void configure(HttpSecurity http) { http.requiresChannel()

2021-11-28 12:27:01    分类:技术分享    java   spring   security   spring-security   spring-java-config

如何延迟 Spring @Conditional 配置注释的评估?(How do I delay evaluation of a Spring @Conditional configuration annotation?)

问题 我有一个 Spring @Configuration类,它应该在环境中设置特定属性值时注册一个 bean。 我编写了一个自定义Condition实现来检查该值是否存在,当我在 Spring Boot 中启动应用程序时它可以工作,但是在运行 JUnit 测试时从未注册过 bean。 我调试了应用程序并确定在实例化PropertySourcesPlaceholderConfigurer之前正在评估Condition 。 我修改了我的Condition以实现ConfigurationCondition并在REGISTER_BEAN阶段指定评估。 该方法仍会在配置器实例化之前被调用,但注册的 bean 现在随着我从属性文件中添加或删除属性而出现和消失。 这是重新排序评估的最佳方法吗? 这是ConfigurationCondition接口的用途,还是我现在不小心让它工作了? @Conditional(PropertyCondition.class) @Configuration public class PostbackUrlConfiguration { @Value("${serviceName.postbackUrl}") String postbackUrl; @Bean public PostbackUrlProvider provider() { return new

2021-11-28 06:51:10    分类:技术分享    spring   spring-java-config

Right way to use @ComponentScan in multi module Java-Config Spring MVC app

I've just started a new spring project, and this time I want to do things "right". In the last project I had issues with multiple registering of certain classes because of multiple @ComponentScan annotations. (i.e. all service classes got registered twice) Basically I'm using the following layout: WebAppInitializer: public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[] { RootConfig.class }; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[] {

2021-11-28 02:51:06    分类:问答    spring   maven   spring-mvc   multi-module   spring-java-config

在 Spring 单元测试期间无法加载 ApplicationContext(Failed to Load ApplicationContext during Spring unit test)

问题 我正在尝试使用 Springs java-config 为我的应用程序上下文运行 Junit 功能测试。 我不确定这是 Spring 还是 Junit 的问题......只是不确定......该应用程序在我的本地服务器上运行良好(命中数据库),但是当我尝试运行我的测试时它会爆炸。 我刚刚从 xml 转移到 java 配置,所以我总是可以导入我的 xml 上下文文件(我知道它在我的测试中有效),但我宁愿只使用我的 java 配置。 我的测试班 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = { AppConfig.class, ServletConfig.class }) @EnvironmentConfiguration(db2Enabled = true) public class EventDAOTest { 堆栈跟踪 Feb 4, 2014 11:51:45 AM com.aoins.config.CachingPropertyFileReader <clinit> SEVERE: ERROR - Configuration Framework not detected! Feb 4

2021-11-28 01:00:50    分类:技术分享    spring   junit4   spring-java-config

Could not obtain transaction-synchronized Session for current thread

I am getting the following Exception with a Spring4/Hibernate4 project I converted from xml- to Java-Config. org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread The project starts up property and errorfree in Eclipse, but on the first request the Exception appears. In my ConfigRoot-class I have @Bean configured for DataSource, SessionFactory, HibernateTransactionManager, ImprovedNamingStrategy. All my @Service services are annotated with @Transactional. Any idea where this could come from ? Edit 1 As requested, here the stacktrace: org

2021-11-24 23:09:54    分类:问答    java   spring   hibernate   spring-java-config

Spring Security OAuth2 with custom TokenGranter in version 2.0.+

In previous versions of OAuth2 it was possible to add a custom token granter by adding it to the xml configuration in the <authorization-server> element. I wonder how I could extend the authorization server with Java Config using a AuthorizationServerConfigurerAdapter, without losing the default configuration, which contains the implicit, client credentials, refresh token and authorization code grant types. First attempt was using creating the TokenGranter with @Component: @Component("customTokenGranter") public class CustomTokenGranter { //implementation } This leads to a dependency

2021-11-24 18:05:57    分类:问答    java   spring   spring-java-config   spring-security-oauth2

How to redirect HTTP requests to HTTPS using Spring Security Java configuration?

I have a Spring Security version 3.2.3 application that listens to both HTTP and HTTPS. I want any request to the HTTP port to be redirected to HTTPS. How do I configure that using Java only? Spring Security javadoc for HttpSecurity proposes the following solution (trimmed to the essential): public class WebSecurityConfig extends WebSecurityConfigurerAdapter { protected void configure(HttpSecurity http) { http.channelSecurity().anyRequest().requiresSecure(); } } However that doesn't work because HttpSecurity doesn't have method channelSecurity().

2021-11-22 00:12:52    分类:问答    java   spring   security   spring-security   spring-java-config

How do I delay evaluation of a Spring @Conditional configuration annotation?

I have a Spring @Configuration class that should register a bean when a specific property value is set in the environment. I wrote a custom Condition implementation that checked whether the value was present, and it works when I fire up the application in Spring Boot, but the bean was never registered when running JUnit tests. I debugged the application and determined that the Condition was being evaluated before the PropertySourcesPlaceholderConfigurer was being instantiated. I modified my Condition to implement ConfigurationCondition and specify evaluation during the REGISTER_BEAN phase. The

2021-11-21 16:16:01    分类:问答    spring   spring-java-config

Spring security oauth2 和表单登录配置(Spring security oauth2 and form login configuration)

问题 我的项目包含两个不同的部分,一个 JSF 管理面板和一个 RESTfull 服务。 我正在尝试设置 spring security 以根据用户导航的 URL 使用不同的身份验证方法。 要求是 导航到 JSF 页面的用户会获得一个登录屏幕,在那里他们使用表单身份验证进行身份验证。 导航到 REST 服务的用户使用 OAuth2 隐式身份验证和基本身份验证来授予令牌。 单独的配置自己工作,问题是当我尝试将它们组合在一个配置中时,在这种情况下,即使请求转到管理 url(这记录在 spring 安全订购中)。 我的示例配置如下所示: 对于表单登录 (JSF) @Override @Order(1) protected void configure(HttpSecurity http) throws Exception { http .csrf().disable() .authorizeRequests() .antMatchers("/resources/**").permitAll() .antMatchers("/templates/**").permitAll() .antMatchers("/401.html").permitAll() .antMatchers("/404.html").permitAll() .antMatchers("/500.html")

2021-11-20 02:07:02    分类:技术分享    spring   spring-security   oauth-2.0   spring-java-config