Tag: spring java config

使用Java Conf而不是xml配置Spring Wicket

我使用Spring很短的时间,但对于检票口我是新手。 但我想尝试一下这个框架。 我想配置这两个框架togheter。 但我想避免使用xml配置文件。 我知道wicket可以这样配置,根据这个: http://wicket.apache.org/guide/guide/single.html#helloWorld_2和此: https://github.com/bitstorm/Wicket-tutorial-examples/tree/master/SpringInjectionExample 但是现在我只得到错误: lis 15, 2014 4:31:15 PM org.apache.catalina.core.StandardContext filterStart SEVERE: Exception starting filter com.derp.wicket.ProjectFilter java.lang.NoSuchMethodError: org.apache.wicket.protocol.http.WebApplication.setMetaData(Lorg/apache/wicket/MetaDataKey;Ljava/lang/Object;)Lorg/apache/wicket/Application; at org.apache.wicket.spring.injection.annot.SpringComponentInjector.(SpringComponentInjector.java:115) at org.apache.wicket.spring.injection.annot.SpringComponentInjector.(SpringComponentInjector.java:92) at com.derp.wicket.WicketApplication.init(WicketApplication.java:52) at org.apache.wicket.Application.initApplication(Application.java:823) at org.apache.wicket.protocol.http.WicketFilter.init(WicketFilter.java:424) at org.apache.wicket.protocol.http.WicketFilter.init(WicketFilter.java:351) at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279) at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260) at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:105) at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4603) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5210) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714) at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:581) at […]

SpringBatch – javaconfig vs xml

我一直在为Spring Batch使用Xml配置,感觉它更简单和简洁。 但是,现在人们建议使用javaconfig而不是xml。 我用Google搜索了这个话题。 这个网站告诉我们为什么javaconfig更好https://blog.codecentric.de/en/2013/06/spring-batch-2-2-javaconfig-part-1-a-comparison-to-xml/ 选择javaconfig而不是xml的主要原因 : 我们想在框架中做一些基本配置。 人们为我们的框架库添加依赖项,并根据需要导入这些配置。 如果这些配置是用XML编写的,那么他们很难打开它们来查看它们正在做什么。 在Java中没问题。 XML中没有可导航性。 只要您没有太多XML文件并且所有这些文件都在您的工作区中,这可能没问题,因为这样您就可以利用Spring IDE支持。 但是框架库通常不应该作为项目添加到工作区。 使用基于Java的配置时,您可以完美地跳转到框架配置类。 我将在以下博客文章中详细讨论这个主题。 在一个框架中,您经常需要库的用户必须满足的要求才能使一切工作,例如需要DataSource,PlatformTransactionManager和线程池。 从框架的角度来看,实现无关紧要,只需要在那里。 在XML中,你必须为框架的用户编写一些文档,告诉他们需要将这个以及这个以及此名称的Spring bean添加到ApplicationContext中。 在Java中,您只需编写一个描述该契约的接口,使用该库的人实现该接口并将其作为配置类添加到ApplicationContext。 这就是我对界面所做的。 这个网站告诉我们为什么xml更好https://dzone.com/articles/consider-replacing-spring-xml 选择xml而不是javaconfig的主要原因 配置是集中的,它不会分散在所有不同的组件中,因此您可以在一个地方对bean及其布线进行很好的概述。 如果您需要拆分文件,没问题,Spring会让您这样做。 然后,它在运行时通过内部标记或外部上下文文件聚合重新组合它们。 只有XML配置允许显式连接 – 而不是自动assembly。 有时,后者对我自己的品味来说有点太神奇了。 它显而易见的简单性隐藏了真正的复杂性:我们不仅需要在按类型和按名称自动assembly之间切换,更重要的是,在所有符合条件的版本中选择相关bean的策略可以逃脱,但是经验丰富的Spring开发人员。 配置文件似乎使这更容易,但相对较新,并为少数人所知。 最后但并非最不重要的是,XML与Java文件完全正交:2之间没有耦合,因此该类可以在具有不同配置的多个上下文中使用。 我总结说,如果您正在创建独立的批处理作业,并且您没有通过与Spring Batch集成来创建任何新框架,那么仍然可以使用xmls。 我错过了xmls的任何缺点吗?

StackOverflowError试图在Spring WebSecurityConfigurerAdapter中公开AuthenticationManager

我试图通过扩展WebSecurityConfigurerAdapter来创建一个Spring Security配置,基本上是这样的: @EnableWebSecurity @Configuration public class StackOverflowSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(myUsernamePasswordProvider()); auth.authenticationProvider(mySecurityTokenProvider()); super.configure(auth); } @Override @Bean public AuthenticationManager authenticationManager() throws Exception { return super.authenticationManagerBean(); } @Bean public MyPreAuthenticatedProcessingFilter myAuthenticationFilter() throws Exception { MyPreAuthenticatedProcessingFilter myAuthenticationFilter = new MyPreAuthenticatedProcessingFilter(); myAuthenticationFilter.setAuthenticationManager(authenticationManager()); return myAuthenticationFilter; } } 我看到了这个: SEVERE: Servlet.service() for servlet […]

使用Java Config时的Spring批处理表前缀

我的Spring Batch存储库(部署在Oracle数据库上)位于不同的模式中,因此我需要预先添加模式名称。 使用XML配置时,这很容易做到: 但是,当我使用Java Config时,事实certificate这更棘手。 我找到的最佳解决方案是让我的Java Config类extend DefaultBatchConfigurer并覆盖createJobRepository()方法: @Configuration @EnableBatchProcessing public class BatchConfiguration extends DefaultBatchConfigurer{ @Autowired private DataSource dataSource; @Autowired private PlatformTransactionManager transactionManager; @Override protected JobRepository createJobRepository() throws Exception { JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); factory.setDataSource(dataSource); factory.setTransactionManager(transactionManager); factory.setTablePrefix(“GFA.BATCH_”); factory.afterPropertiesSet(); return factory.getObject(); } … } 与XML解决方案相比,这几乎是代码! 而且它也不合逻辑 – 我的第一个猜测是提供一个@Bean方法,如下所示: @Bean public JobRepository jobRepository() throws Exception { […]

使用JavaConfig示例的Spring Security Digest Auth

如何使用javaconfig(无XML)专门为摘要式身份validation配置Spring 4.0和Spring Security(3.2.0)? 我使用下面的配置类,但所有请求都被HTTP 401拒绝,“Nonce应该产生两个令牌,但是(…消息就在那里停止)”。 @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class SecurityConfigurationDigest extends WebSecurityConfigurerAdapter { @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().withUser(“user”).password(“password”).roles(“USER”); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers(“/**”).authenticated().and().addFilter(digestAuthenticationFilter(digestEntryPoint())); } @Override @Bean public UserDetailsService userDetailsServiceBean() throws Exception { return super.userDetailsServiceBean(); } public DigestAuthenticationFilter digestAuthenticationFilter(DigestAuthenticationEntryPoint digestAuthenticationEntryPoint) throws Exception { DigestAuthenticationFilter […]

如何获得我不“拥有”的自动assembly器

要点是Spring Batch(v2)测试框架具有带@Autowired注释的JobLauncherTestUtils.setJob 。 我们的测试套件有多个Job类提供程序。 由于这个类不是我可以修改的,所以我不确定如何确定它自动assembly哪个作业,每个测试可能会有所不同。 STDOUT [WARN ] [2015.04.15 11:14:42] support.GenericApplicationContext – Exception encountered during context initialization – cancelling refresh attempt org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘jobLauncherTestUtilsForSnapshot’: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.batch.test.JobLauncherTestUtils.setJob(org.springframework.batch.core.Job); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.batch.core.Job] is […]

如何在spring-security-javaconfig中添加Access Denied Handler

我正在使用spring-security-javaconfig库来保证弹簧安全。 如果我使用xml配置文件,我会使用类似这样的内容来定义自定义访问被拒绝页面: 到目前为止,这是我的安全配置类: @Configuration @EnableWebSecurity public class SecurityConfigurator extends WebSecurityConfigurerAdapter { @Override protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().withUser(“user”).password(“password”).roles(“USER”); auth.inMemoryAuthentication().withUser(“admin”).password(“password”).roles(“ADMIN”); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeUrls().antMatchers( “/admin”).hasRole(“ADMIN”); } }

在jobExeuctionDecider之后执行Spring Batch(java-config)步骤

我正在尝试使用java-config在spring批处理中配置Flow,这个流程基本上必须这样做: 执行init步骤(在数据库中添加记录), 然后执行一个决策程序来检查文件是否存在, 2.1。 如果文件存在,它将执行加载作业(这是一个并行的一堆步骤的另一个流程) 执行完成步骤(在数据库中添加记录),即使2.1未执行,也应该始终运行。 我尝试进行此配置,但完成步骤永远不会运行: Flow flow = new FlowBuilder(“commonFlow”) .start(stepBuilderFactory.get(“initStep”).tasklet(initTasklet).build()) .next(decider) .on(FlowExecutionStatus.COMPLETED.getName()) .to(splitFlow) .from(decider).on(“*”) .end() .next(stepBuilderFactory.get(“finishStep”).tasklet(finishTasklet).build()) .end(); 我能够按照下面的方式工作,但它根本不优雅: Step finishStep = stepBuilderFactory.get(“finishStep”).tasklet(finishTasklet).build(); Flow flow = new FlowBuilder(“commonFlow”) .start(stepBuilderFactory.get(“initStep”).tasklet(initTasklet).build()) .next(decider) .on(FlowExecutionStatus.COMPLETED.getName()) .to(splitFlow) .next(finishStep) .from(decider).on(“*”) .to(finishStep) .end(); 有人知道在使用java-config做出决定后执行步骤的正确方法是什么?

如何将Spring配置为部分和可选地覆盖属性?

我想要一个属性设置,在某些环境中,它可以覆盖特定的属性。 例如,dev的默认JDBC属性是: db.driverClassName = com.mysql.jdbc.Driver = db.url配置参数JDBC:MySQL的://本地主机:3306 / ourdb db.username =根 db.password = 问题是我们的一些开发人员希望在数据库上拥有不同的用户名/密码,甚至可能是非本地托管的数据库。 我们的rabbitMQ配置也是如此,它目前使用类似的localhost,guest / guest设置。 能够在每个开发人员的基础上覆盖此配置的某些元素的属性将允许我们移动大部分基础架构/安装要求,以便从本地计算机和专用服务器构建软件。 我已经设置了一个简单的项目来围绕实现我想要的配置,这是我第一次进入spring属性配置的世界,因为到目前为止,属性加载和管理是通过一些自定义完成的码。 这是我的设置: class Main_PropertyTest { public static void main(String[] args) { String environment = System.getenv(“APPLICATION_ENVIRONMENT”); // Environment, for example: “dev” String subEnvironment = System.getenv(“APPLICATION_SUB_ENVIRONMENT”); // Developer name, for example: “joe.bloggs” System.setProperty(“spring.profiles.active”, environment); System.setProperty(“spring.profiles.sub”, subEnvironment); try(AnnotationConfigApplicationContext context = […]

如何将jndi查找从xml转换为java配置

目前我正在将xml转换为java配置。 但我坚持了几天,我已经研究了几天。 这里的问题是: Xml配置: 到目前为止,我设法转换此代码: 对此: @Bean(name = “dbDataSource”) public JndiObjectFactoryBean dataSource() { JndiObjectFactoryBean bean = new JndiObjectFactoryBean(); bean.setJndiName(“${db.jndi}”); bean.setResourceRef(true); return bean; } 还有这个 : 对此: @Bean(name = “jdbcTemplate”) public JdbcTemplate jdbcTemplate() { JdbcTemplate jt = new JdbcTemplate(); jt.setDataSource(dataSource); return jt; } 问题是方法setDataSource()需要DataSource对象,但我不确定如何关联两个bean。如何将JndiObjectFactoryBean传递给DataSource? 或者我需要使用其他方法吗? 额外问题: bean.setJndiName(“${db.jndi}”) , $ {db.jndi}是指属性文件,但我总是得到NameNotFoundException,如何让它工作? 谢谢!!