我想在spring配置中配置推土机.当使用xml配置时,它就像
<bean class="org.dozer.spring.DozerBeanMapperFactoryBean">
<property name="mappingFiles" value="classpath*:dozer/**/*.dzr.xml"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
如何在配置文件中定义资源.我尝试使用ctx.getResource()但我无法访问Configuration类中的ApplicationContext.
我尝试了ContextRefreshedEvent并从那里添加资源,但仍然没有运气.(已调用afterPropertiesSet并且添加的映射不起作用)
public class ContextRefreshedEventBuilder extends ContextRefreshedEvent {
public ContextRefreshedEventBuilder(ApplicationContext ctx) {
super(ctx);
DozerBeanMapperFactoryBean mapper = ctx.getBean(DozerBeanMapperFactoryBean.class);
try {
mapper.setMappingFiles(ctx.getResources("classpath*:dozer/**/*.dzr.xml"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
还尝试使用ClassPathResource但无法找到正确的方法
DozerBeanMapperFactoryBean mapper = new DozerBeanMapperFactoryBean();
mapper.setMappingFiles(new Resource[]{new ClassPathResource("classpath*:dozer/**/*.dzr.xml")});
return mapper;
Run Code Online (Sandbox Code Playgroud)
如何将ClassPathResource添加为映射位置?
- -回答 - -
@Bean
public DozerBeanMapperFactoryBean configDozer() throws IOException {
DozerBeanMapperFactoryBean mapper = new DozerBeanMapperFactoryBean();
Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:dozer/**/*.dzr.xml");
mapper.setMappingFiles(resources);
return mapper;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用 spring boot (1.3.4.RELEASE) 并且有一个关于新的 @AliasFor 注释在 4.2 中引入 spring 框架的问题
考虑以下注释:
看法
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component
public @interface View {
String name() default "view";
}
Run Code Online (Sandbox Code Playgroud)
合成的
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@View
public @interface Composite {
@AliasFor(annotation = View.class, attribute = "name")
String value() default "composite";
}
Run Code Online (Sandbox Code Playgroud)
然后我们注释一个简单的类如下
@Composite(value = "model")
public class Model {
}
Run Code Online (Sandbox Code Playgroud)
运行以下代码时
ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
String[] beanNames = context.getBeanNamesForAnnotation(View.class);
for (String beanName : beanNames) {
View annotationOnBean = context.findAnnotationOnBean(beanName, View.class);
System.out.println(annotationOnBean.name());
}
Run Code Online (Sandbox Code Playgroud)
我期待输出是model,但它的观点 …
尝试使用自动装配两个bean时,我收到以下错误
没有定义[javax.jms.ConnectionFactory]类型的限定bean:期望的单个匹配bean但找到2:aConnectionFactory,bConnectionFactory
Description:
Parameter 1 of method jmsListenerContainerFactory in org.springframework.boot.autoconfigure.jms.JmsAnnotationDrivenConfiguration required a single bean, but 2 were found:
- aConnectionFactory: defined by method 'aConnectionFactory' in package.Application
- bConnectionFactory: defined by method 'bConnectionFactory' in package.Application
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
Run Code Online (Sandbox Code Playgroud)
我有这个注释驱动配置:
@SpringBootApplication
@EnableIntegration
@IntegrationComponentScan
public class Application extends SpringBootServletInitializer implements
WebApplicationInitializer {
@Resource(name = "aConnectionFactory")
private ConnectionFactory aConnectionFactory;
@Resource(name …Run Code Online (Sandbox Code Playgroud) 在我们的 Spring Web 应用程序中,我们正在从基于 XML 的配置转向基于注释的配置。
我被这个 XML 定义的计划任务所困扰
<task:scheduled-tasks scheduler="cacheScheduler">
<task:scheduled ref="currencyExchangeRateTask" method="cacheCurrencyExchangeRates" cron="0 0 8,20 * * *" />
</task:scheduled-tasks>
Run Code Online (Sandbox Code Playgroud)
我们的网络应用程序中有多个调度程序。而这个任务需要在 id 的调度器上执行cacheScheduler。
我现在有以下注释
@Scheduled(cron = "0 0 8,20 * * *")
public void cacheCurrencyExchangeRates() {
...
}
Run Code Online (Sandbox Code Playgroud)
这是在默认调度程序上执行的。
如果没有 XML 配置,如何解决这个问题?
关于 spring 注释 @Value 的小问题。
该注释功能强大、众所周知,并且提供了开箱即用的可能性,可以通过配置绑定布尔值、字符串等类型,甚至更复杂的数据结构(例如列表、Map<String, Integer>)。
@Value("${my.string}")
private String myString;
@Value("${my.flag}")
private Boolean someFlag;
@Value("#{'${my.list.of.strings}'.split(',')}")
private List<String> myListOfStrings;
@Value("#{${my.hashmap}}")
private Map<String, Integer> myHashMap;
Run Code Online (Sandbox Code Playgroud)
application.properties
my.string=something
my.flag=true
my.list.of.strings=hello,world
my.hashmap={'hello':'world','aaa':'bbb','ccc':'ddd'}
Run Code Online (Sandbox Code Playgroud)
问题:是否可以在我定义的某些 POJO 上使用 @Value? 就像是:
@Value("${my.pojo}")
private MyPojo myPojo;
public class MyPojo {
private String firstName;
private String lastName;
private int age;
private boolean isMarried;
}
application.properties
my.pojo={ "firstname": "john", "lastname": "doe", "isMarried": false, "age": 20 }
Run Code Online (Sandbox Code Playgroud)
并让@Value(或者其他东西)拾取它。我在上面尝试过,但没有得到 myPojo。请问有什么解决方案可以在 @Value 下配置此功能?
谢谢
我熟悉复合注释。然而,即使经过一些研究,它们似乎还不足以满足我的特定需求。
我想创建一个用于测试的注释,将其与一些属性放在一个类上,可以自定义测试上下文。
public @interface MyTestingAnnotation {
String myTestingAttribute()
}
Run Code Online (Sandbox Code Playgroud)
的值应该由我的自定义 (TBD )myTestingAttribute之一读取@TestConfiguration
在我的应用程序中,我必须模拟时钟,以便测试可以模拟特定时间点运行。例如,测试结果不依赖于硬件时钟。我java.time.Clock为此目的定义了一个 bean。
目前,我只有一个注释来启用模拟时钟,但指定其时间取决于@TestPropertySource
public @interface MyTestingAnnotation {
String myTestingAttribute()
}
Run Code Online (Sandbox Code Playgroud)
相反,我想注释一个测试@MockClock(at = "2024-02-15T12:35:00+04:00"),而不一定使用属性源语法。
我知道如何@AliasFor在自定义注释中使用,但目前我只能@Import(MockClockConfiguration.class)在元注释中使用。
我怎样才能在 Spring 中实现这样的目标?
以下是阅读Spring Reference产生的问题,请帮忙.
(1)我是否需要手动创建ApplicationContext?我是否需要第二个AplicationContext实例?
(2)我们有以下配置说明:
<context:annotation-config/>
<context:component-scan base-package=".."/>
<mvc:annotation-driven/>
Run Code Online (Sandbox Code Playgroud)
这些说明是否与自己复制?在哪些情况下是的,其中没有?
(3)我对Spring引入的从字符串转换为对象的所有方式都有点困惑:PropertyEditor,Conversions,Formatting ..这是一个简单的用例:我有一个处理一些POST请求的Spring MVC控制器.该请求是填写某种形式的结果.表单是某个实体的Web表示.因此,给定用户提交新的项目表单.在该表单中,存在一个日期字段和一个管理者名称字段,可从现有管理者列表中选择.输入的日期应转换为Project对象的Date属性,经理的名称 - 转换为Manager属性,由此名称创建或定位(即我想将Manager注入他的Project).在这种情况下我应该使用什么?属性编辑器,格式化程序,还有别的什么?
(4)通常,我可以说在类路径中找到的所有@interface类都可以被Spring用作注释吗?换句话说,我怎么知道我的项目中可以使用哪些注释?所有这些都可以在我的类路径中找到,或者我需要以某种方式注册它们?
(5)我试图在没有aspectj.jar的情况下使用spring aop:刚刚为这个方面创建了一个Aspect和addred XML定义(没有任何注释).结果它抛出"class not found Exception:org/aspectj/weaver/BCException".所以看起来我不能在没有aspectJ库的情况下使用Spring AOP?
我搜索了很多,但我无法解决任何帖子或评论或任何完整的代码集成spring和quartz并将石英配置存储在数据库中使用java配置(XML-LESS)任何人都可以帮助我并向我展示一些代码或参考?非常感谢
我正在使用Spring Batch设置项目而不使用Spring Boot。创建Spring应用程序上下文后,所有作业都将执行。
我尝试添加spring.batch.job.enbled=false到application.properties以防止这种情况,但仍然无法正常工作。
还有其他方法可以阻止Spring在启动时执行作业吗?
主类:
package com.project.batch;
import ...
@Configuration
@EnableBatchProcessing
@PropertySource("classpath:application.properties")
public class App {
public static void main(String [] args) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
System.out.println("starting main");
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.project.batch");
context.refresh();
//JobParameters jobParameters = new JobParametersBuilder().toJobParameters();
//JobLauncher jobLauncher = context.getBean(JobLauncher.class);
//JobExecution execution = jobLauncher.run(context.getBean("loaderJob",Job.class),jobParameters);
System.out.println("finished!!");
}
}
Run Code Online (Sandbox Code Playgroud)
职位类别:
package com.project.batch;
import ...
@Configuration
public class LoaderJobConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired …Run Code Online (Sandbox Code Playgroud) spring spring-annotations spring-batch spring-boot spring-config
我正在尝试在Spring MVC中发布自定义事件,但在加载上下文时未触发,下面是代码段,
连接到服务器后将调用onConnectionOpened,该服务器是在使用@PostConstruct进行bean初始化之后触发的
@Autowired
private ApplicationEventPublisher publisher;
public void onConnectionOpened(EventObject event) {
publisher.publishEvent(new StateEvent("ConnectionOpened", event));
}
Run Code Online (Sandbox Code Playgroud)
我在侦听器部分中使用注释,如下所示
@EventListener
public void handleConnectionState(StateEvent event) {
System.out.println(event);
}
Run Code Online (Sandbox Code Playgroud)
我能够看到在加载或刷新上下文后触发的事件,这是否可以在加载或刷新上下文后发布自定义应用程序事件?
我正在使用Spring 4.3.10
提前致谢
spring ×8
java ×5
spring-mvc ×2
annotations ×1
classpath ×1
resources ×1
spring-aop ×1
spring-batch ×1
spring-boot ×1
spring-test ×1