如何根据区域为变量赋值?
让我们说
system properties,
dev-url="dev-abc.com",
prod-url="prod-abc.com"和
qa-url="qa-abc.com"
@Value( #{systemProperties. ??? )
String url;
Run Code Online (Sandbox Code Playgroud) spring spring-annotations spring-data spring-boot spring-config
我正在使用 spring-boot-1.5.6。我里面有一个控制器,大约有 8 个请求映射,它们具有公共标头。下面提到的标头对于控制器中的大多数方法都是必需的,并且提及所有标头看起来很难看,有没有办法概括或自定义注释,以便我不想在所有方法中复制标头。
@RestController
public class RestController {
private static final Logger logger = LoggerFactory.getLogger(RestController.class);
/**
* Simply chooses a few headers, logs them and add them to model to
* showHeaders view to render to the user.
*/
@GetMapping(value="/showheaders")
public String getHeaders(
@RequestHeader(value="sample") String sample,
@RequestHeader(value="sample1") String sample1,
@RequestHeader(value="sample2") String sample2,
@RequestHeader(value="sample3") String sample3,
@RequestHeader(value="sample4") String sample4) {
logger.info("Inside getHeaders() method...");
logger.info("sample : " + sample);
logger.info("sample1 : " + sample1);
logger.info("sample2 : " + sample2);
logger.info("sample3 : …Run Code Online (Sandbox Code Playgroud) 正如标题所示,我想知道字段注入在 Spring 内部是如何工作的,我阅读了很多关于此的文章并了解了如下内容,但不明白其背后的确切原因:
-> 不应该使用它,因为当您进行单元测试时,您依赖于 spring 容器来实例化该类以防止字段注入。
-> 在字段注入的情况下不能使用“final”关键字,这意味着你不能使字段不可变。
-> 它内部使用反射
我想知道 @Autowired 内部到底是如何工作的,它如何使用反射,我试图了解上述所有点背后的确切原因,当我们编写以下代码时幕后会发生什么:
@Component
public class B {
@Autowired
private A a1;
}
Run Code Online (Sandbox Code Playgroud)
我已经阅读过有关此主题的堆栈溢出的类似问题,但我找不到我正在寻找的确切解释。
我目前正在开发一个小型项目,试图让Java spring验证在Web表单上运行.它确实有效,但是我有一个用于输入年龄的输入,然后我使用这个注释转换为数字格式,如果我输入字母,它会在提交表单时将其显示在输入框旁边:
"无法将类型为java.lang.String的属性值转换为属性所需的类型java.lang.Integer;嵌套异常为org.springframework.core.convert.ConversionFailedException:无法从类型java.lang转换值"dasdf" .string类型为java.lang.Integer;嵌套异常是java.lang.IllegalArgumentException:无法解析dasdf"
有没有办法改变这个消息,我确信它很简单,但已经搜索过,找不到它.
这是目前的验证码:
@NotNull
@NumberFormat(style = Style.NUMBER)
@Min(1)
@Max(110)
private Integer age;
Run Code Online (Sandbox Code Playgroud)
干杯,大卫
我试图将bean注入我的类ValiderBR
@Service
public class ValiderBR extends BusinessRule {
@Autowired
ILog logger;
...
}
Run Code Online (Sandbox Code Playgroud)
但我有一个由@Autowired注释引起的注入错误
22 mai 2013 14:44:02 org.apache.catalina.core.ApplicationContext log
GRAVE: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'validerBR': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: ma.co.services.log.ILog awb.businessrules.WorkflowIndividu.ValiderBR.logger; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ma.co.services.log.ILog] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1122)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
at …Run Code Online (Sandbox Code Playgroud) 我正在研究Spring框架,我对此示例的构造函数的@Autowired注释有以下疑问:
@Component
public class TransferServiceImpl implements TransferService {
@Autowired
public TransferServiceImpl(AccountRepository repo) {
this.accountRepository = repo;
}
}
Run Code Online (Sandbox Code Playgroud)
究竟是什么意思呢?该AccountRepository回购对象(definied作为组分某处)自动注射到 TransferServiceImpl()构造?
它是如何运作的?是按类型完成的吗?(因为AccountRepository是Spring默认的单例),还是什么?
TNX
为什么Spring不会抛出NoSuchBeanDefinitionException存在模糊依赖关系的地方,并且有多个bean候选者使用@Autowired注释进行自动装配?
我有这个简单的beans.xml,它有两个相同的bean,具有不同的id category,category1并且出于某种原因,Spring选择categorybean进行自动装配.我的印象是@Autowired注释byType在内部使用自动装配,因为这里有多个匹配,Spring会抛出NoSuchBeanDefinitionException异常.
我在3.2.13.RELEASE这里使用spring版本.
beans.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans
..... ">
<context:annotation-config />
<bean id="product" class="com.study.spring.Product">
<property name="id" value="101"/>
<property name="name" value="Apple iPhone"/>
<property name="active" value="true"/>
</bean>
<bean id="category1" class="com.study.spring.Category">
<property name="id" value="202"/>
<property name="name" value="Phone"/>
<property name="active" value="true"/>
</bean>
<bean id="category" class="com.study.spring.Category">
<property name="id" value="201"/>
<property name="name" value="Communications"/>
<property name="active" value="true"/>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
Product.java
package com.study.spring;
import org.springframework.beans.factory.annotation.Autowired;
public class Product …Run Code Online (Sandbox Code Playgroud) 我正在研究一个从Azure服务总线读取消息的应用程序。该应用程序是使用Spring Boot,Spring jms和Qpid jms客户端创建的。我能够从Queue正确读取消息,而没有任何问题。PFB我用来阅读消息的代码。
@Service
public class QueueReceiver {
@JmsListener(destination = "testing")
public void onMessage(String message) {
if (null != message) {
System.out.println("Received message from Queue: " + message);
}
}}
Run Code Online (Sandbox Code Playgroud)
问题是,对于不同的环境,我们有不同的目的地,例如testingfor dev,testing-qafor qa和testing-prodfor production,所有这些值都分别azure.queueName在不同的application-(ENV).properpties中提供。我想将这些目标动态传递给JmsListener注释中的目标。当我尝试使用
@Value("${azure.queueName}")
private String dest;
Run Code Online (Sandbox Code Playgroud)
并将dest传递给注释 @JmsListener(destination = dest)
我收到The value for annotation attribute JmsListener.destination must be a constant expression错误消息。仔细检查了此错误后,我发现我们无法将动态值传递给注释。请帮助我如何解决此问题或任何其他解决方案。
我在用
context.getBeanDefinitionNames()
Run Code Online (Sandbox Code Playgroud)
列出加载的所有bean,但是,如何过滤此列表以仅显示user/programmer定义的bean?我不想看到Spring Framework自动实例化的所有bean.
我正在关注Custom Spring AOP Annotation的给定文档
以下是我在项目中编写的源代码,具有与本文中提到的类似的依赖关系.
注释定义:
package com.sell.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CommitServiceAdvice {}
Run Code Online (Sandbox Code Playgroud)
方面定义:
package com.sell.business.service.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.sell.aggregator.MessageFlowAggregator;
@Aspect
@Component
public class CommitServiceAspect {
private Logger log = LoggerFactory.getLogger(this.getClass());
@Around(value="@annotation(CommitServiceAdvice)",argNames="mfa")
public MessageFlowAggregator advice(ProceedingJoinPoint joinPoint,MessageFlowAggregator mfa) throws Throwable {
log.debug(">>> matching advice on {}",joinPoint);
mfa= (MessageFlowAggregator) joinPoint.proceed();
log.debug("<<< returning advice on {}",joinPoint);
return mfa;
}
}
Run Code Online (Sandbox Code Playgroud)
联合点:
package …Run Code Online (Sandbox Code Playgroud) spring ×9
java ×7
spring-boot ×5
annotations ×3
spring-mvc ×3
aspectj ×1
frameworks ×1
spring-3 ×1
spring-aop ×1
spring-data ×1
spring-jms ×1