如何避免BeanCreationException:无法自动装配字段错误(How to avoid BeanCreationException: Could not autowire field error)
我有以下弹簧测试配置与不同的配置文件:
@Configuration @ComponentScan (value = {"uk.co.test.ws" }) public class SpringTestConfig { @Profile( "local") @PropertySource( "classpath:/config/local/settings.properties" ) public static class SpringTestConfigLocal { @Autowired private Environment environment ; @Bean(name= "baseUrl" ) public String setBaseUrl() { return environment .getRequiredProperty("baseurl.protocol" )+"://" +environment .getRequiredProperty( "baseurl.host"); } }
然后创建一个接收基本URL的基类
> @RunWith (SpringJUnit4ClassRunner. class) @ContextConfiguration > (classes = { SpringTestConfig. class }) public class BaseWsTest { > @Autowired > String baseUrl;
然后扩展到其他测试类,如下所示:
public class SampleFTest extends BaseWsTest { @Test public void hello() throws FileNotFoundException, Exception { System. out .println("base url: " + baseUrl );
当使用普通的maven clean install运行时,测试可以正常运行,但如果我通过右键单击该方法来运行它,则会得到一个
Error creating bean with name 'uk.co.test.ws.service.base.SampleFTest': Injection of autowired dependencies failed;
I have the following spring test configuration with different profiles:
@Configuration @ComponentScan (value = {"uk.co.test.ws" }) public class SpringTestConfig { @Profile( "local") @PropertySource( "classpath:/config/local/settings.properties" ) public static class SpringTestConfigLocal { @Autowired private Environment environment ; @Bean(name= "baseUrl" ) public String setBaseUrl() { return environment .getRequiredProperty("baseurl.protocol" )+"://" +environment .getRequiredProperty( "baseurl.host"); } }
and then created a base class that takes in the base url
> @RunWith (SpringJUnit4ClassRunner. class) @ContextConfiguration > (classes = { SpringTestConfig. class }) public class BaseWsTest { > @Autowired > String baseUrl;
which then gets extended to other test classes like below:
public class SampleFTest extends BaseWsTest { @Test public void hello() throws FileNotFoundException, Exception { System. out .println("base url: " + baseUrl );
When run using normal maven clean install the tests works but if I was to run it by right-clicking the method it gets a
Error creating bean with name 'uk.co.test.ws.service.base.SampleFTest': Injection of autowired dependencies failed;
原文:https://stackoverflow.com/questions/29297118
最满意答案
更正:由于@vacawama在他的回答中正确表示,括号在这里不匹配。\\([ABCD])\\)
部分是可以的,\\([ABCD]\\)
与括在圆括号中的字母AD中的一个匹配。另一个问题是空格和括号之间没有词边界 (
\b
模式)。所以你可以(根据你的需要),删除
\b
模式,或者用\s
替换为空格:let regex = NSRegularExpression(pattern: "\\s\\([ABCD]\\)\\s", ...
但由于匹配的字符串不应包含需要捕获组的空间:
let regex = NSRegularExpression(pattern: "\\s(\\([ABCD]\\))\\s", ... // ... let strRange = match?.rangeAtIndex(1)
TheCorrection: As @vacawama correctly said in his answer, the parentheses do not match here.\\([ABCD])\\)
part is OK,\\([ABCD]\\)
matches one of the letters A-D enclosed in parentheses.The other problem is that there is no word boundary (
\b
pattern) between a space and a parenthesis.So you could either (depending on your needs), just remove the
\b
patterns, or replace them by\s
for white space:let regex = NSRegularExpression(pattern: "\\s\\([ABCD]\\)\\s", ...
But since the matched string should not include the space you need a capture group:
let regex = NSRegularExpression(pattern: "\\s(\\([ABCD]\\))\\s", ... // ... let strRange = match?.rangeAtIndex(1)
相关问答
更多-
试试这个: 示例代码 public static void main(String[] args) { String test ="((6.28 + 3.14) + 7.56)"; String re = "\\(|\\)|\\d+\\.?\\d*|[+-/%^!*]"; List
components = new ArrayList (); Pattern p = Pattern.compile(re); Matcher m = p ... -
========= /[abcd]+|x/ 这肯定有效,这个表达式匹配[abcd]+ OR | 匹配x 。 [abcd]+匹配字母a , b , c和d一个或多个组合。 x匹配文字x 。 所以当你对abcdxabcd运行它时, [abcd]+首先匹配abcd ,然后x匹配文字x然后[abcd]+匹配第二个abcd 。 现在你完全正确,那些是单独的匹配,换句话说,如果你使用/^[abcd]+|x$/作为正则表达式(注意锚点^和$ )你会注意到这不会匹配abcdxabcd 。 ========= /(d|[ab ...
-
可以使用正则表达式来匹配嵌套模式吗?(Can regular expressions be used to match nested patterns? [duplicate])[2021-12-28]
不,这很简单。 有限自动机(它是正则表达式的基础的数据结构)除了它的状态之外没有内存,如果你有任意深度的嵌套,则需要一个任意大的自动机,这与自动机的概念相冲突。 您可以将嵌套/配对元素匹配到一个固定的深度,其深度仅受您的内存限制,因为自动机变得非常大。 但实际上,您应该使用下推自动机,即无上下文语法的解析器,例如LL(自上而下)或LR(自下而上))。 您必须考虑更差的运行时行为:O(n ^ 3)与O(n),n =长度(输入)。 有许多解析器生成器可以使用,例如Java的ANTLR 。 找到Java(或C)的 ... -
正则表达式是工作的错误工具,因为您正在处理嵌套结构,即递归。 但是有一个简单的算法来做到这一点,我在上面的问题的答案中描述了这一点 。 Regular expressions are the wrong tool for the job because you are dealing with nested structures, i.e. recursion. But there is a simple algorithm to do this, which I described in this ans ...
-
了解您正在使用哪种正则表达式会很有用,但您很有可能需要引用(和)字符,因为它们具有特殊含义。 所以用[(]替换(用[(] ,和) ,或者用反斜杠引用它们。 It would be usefull to know which flavour of regular expressions you are using, but there is a good chance that you need to quote ( and ) characters, as they have special meaning ...
-
\\([ABCD])\\)部分是可以的, 更正:由于@vacawama在他的回答中正确表示,括号在这里不匹配。 \\([ABCD]\\)与括在圆括号中的字母AD中的一个匹配。 另一个问题是空格和括号之间没有词边界 ( \b模式)。 所以你可以(根据你的需要),删除\b模式,或者用\s替换为空格: let regex = NSRegularExpression(pattern: "\\s\\([ABCD]\\)\\s", ... 但由于匹配的字符串不应包含需要捕获组的空间: let regex = NSRe ...
-
括号仅用于正则表达式匹配中的捕获组?(parentheses only used for capture group in regular expression matching?)[2022-01-08]
括号用于分组,这将使正则表达式引擎捕获括号内的子模式。 如果您不想使用此语法在非捕获组内捕获文本: (?:...) 这也将在处理长而复杂的正则表达式时节省一些内存。 查看有关非捕获组的更多信息 如果正则表达式是这样的话,应该如何谨慎放置ad where ()的基本和简单示例 ^\w+|\d+$ 我们在输入处选择开头的单词或结尾处的数字: foo -a123 bar baz 123 因此我们匹配foo和123 。 如果你像这样放置方括号() : ^(\w+|\d+)$ 然后你的匹配将失败,因为我们断言 ... -
正则表达式匹配JavaScript的括号和括号(Regular Expression to match parentheses and brackets for JavaScript)[2022-04-27]
这应该工作: /\([^)]*\)|\[[^\]]*\]/g; 试试下面: var str = "The (quick) brown [fox]"; var re = /\([^)]*\)|\[[^\]]*\]/g; str.match(re).forEach(function(m) { document.body.insertAdjacentHTML('beforeend', m + '
'); }); Regex101 This should work: /\([^)]*\) ... -
没有任何捕获组, >>> import re >>> str = """ ... gi|13195623|ref|NM_024197.1| Mus musculus NADH dehydrogenase (ubiquinone) 1 alp ... ha subcomplex 10 (Ndufa10), mRNAGCCGGCGCAGACGGCGAAGTCATGGCCTTGAGGTTGCTGAGACTCGTC ... CCGGCGTCGGCTCCCGCGCGCGGCCTCGCGGCCGGAGCCCAGCGCG ...
-
或多或少直接回复您的评论 尝试这个 import re s = '1 stores(s)' if re.match('store\(s\)$',s): print('match') 解决方案是使用re.search而不是re.match因为后者试图将整个字符串与regexp匹配,而前者只是试图在匹配表达式的字符串内部找到一个子字符串。 In more or less direct reply to your comment Try this import re s = '1 stores(s)' ...