首页 \ 问答 \ 如何避免BeanCreationException:无法自动装配字段错误(How to avoid BeanCreationException: Could not autowire field error)

如何避免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
更新时间:2023-07-10 14:07

最满意答案

\\([ABCD])\\)部分是可以的, 更正:由于@vacawama在他的回答中正确表示,括号在这里不匹配。 \\([ABCD]\\)与括在圆括号中的字母AD中的一个匹配。

另一个问题是空格和括号之间没有词边界\b模式)。

所以你可以(根据你的需要),删除\b模式,或者用\s替换为空格:

let regex = NSRegularExpression(pattern: "\\s\\([ABCD]\\)\\s", ...

但由于匹配的字符串不应包含需要捕获组的空间:

let regex = NSRegularExpression(pattern: "\\s(\\([ABCD]\\))\\s", ...
// ...
let strRange = match?.rangeAtIndex(1)

The \\([ABCD])\\) part is OK, Correction: As @vacawama correctly said in his answer, the parentheses do not match here. \\([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 ...
  • 不,这很简单。 有限自动机(它是正则表达式的基础的数据结构)除了它的状态之外没有内存,如果你有任意深度的嵌套,则需要一个任意大的自动机,这与自动机的概念相冲突。 您可以将嵌套/配对元素匹配到一个固定的深度,其深度仅受您的内存限制,因为自动机变得非常大。 但实际上,您应该使用下推自动机,即无上下文语法的解析器,例如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 ...
  • 括号用于分组,这将使正则表达式引擎捕获括号内的子模式。 如果您不想使用此语法在非捕获组内捕获文本: (?:...) 这也将在处理长而复杂的正则表达式时节省一些内存。 查看有关非捕获组的更多信息 如果正则表达式是这样的话,应该如何谨慎放置ad where ()的基本和简单示例 ^\w+|\d+$ 我们在输入处选择开头的单词或结尾处的数字: foo -a123 bar baz 123 因此我们匹配foo和123 。 如果你像这样放置方括号() : ^(\w+|\d+)$ 然后你的匹配将失败,因为我们断言 ...
  • 这应该工作: /\([^)]*\)|\[[^\]]*\]/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)' ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)