当前位置: 首页 > 知识库问答 >
问题:

Spring Autowires来自xml定义的bean

金理
2023-03-14
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>myapp</display-name>

<servlet>
    <servlet-name>mycompany</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mycompany</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:mypackage/simplebean.xml</param-value>
</context-param>

</web-app>

MyCompany-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"    
   xmlns:mvc="http://www.springframework.org/schema/mvc"    
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:util="http://www.springframework.org/schema/util"    
   xsi:schemaLocation="http://www.springframework.org/schema/beans    
   http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
   http://www.springframework.org/schema/mvc    
   http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   
   http://www.springframework.org/schema/context  
   http://www.springframework.org/schema/context/spring-context-3.2.xsd
   http://www.springframework.org/schema/util 
   http://www.springframework.org/schema/util/spring-util-3.2.xsd">  

<context:component-scan base-package="com.mycompany" />     

</beans>

simpleBean.xml(在src/main/resources/mypackage中)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                    http://www.springframework.org/schema/util
                    http://www.springframework.org/schema/util/spring-util-3.2.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
                    http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-2.5.xsd">

<context:component-scan base-package="com.mycompany" /> 

<bean id="myString" class="java.lang.String">
    <constructor-arg value="A_String"/>
</bean>

</beans>
package com.mycompany;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("services/test")
public class TestRestController {

@Autowired
private String myString;

@RequestMapping(value = "/testService", method = RequestMethod.GET)
@ResponseBody
public Integer testSendMessage(HttpServletRequest request) throws Exception {

    System.out.println("myString: " + myString);
    return 0;
}
}
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.mycompany.TestRestController.myString; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] 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)}

共有1个答案

微生阳平
2023-03-14
<context:annotation-config /> 
 类似资料:
  • 禁止DisableNastyThing1或DisableNastyThing2的工作方式与禁止模块完全相同。换句话说,myCheck XML元素名将表示这样一个事实,即该检查没有映射到实现AbstractCheck的实际Java类,而是映射到执行嵌套在其中的检查的一些通用检查。 是不是Checkstyle中已经有这样的概念,而我却没能找到它,还是怎么的?

  • 我目前正在尝试将一些现有的XML解组到我手工创建的几个类中。问题是,我总是得到一个错误,告诉我,JaxB需要一个天气元素,但却找到了一个天气元素。(?) UnMarshalException:意外元素(URI:“http://www.aws.com/aws”,本地:“weather”)。需要的元素为<{}API>、<{}Location>、<{}Weather> 这就是我试图解析的XML: 我不太

  • 并设置Claas

  • 我使用Wordpress Rest API将内容从WordPres斯网站导入到PHP应用程序中。这并不复杂,只是一个包含帖子列表和个人帖子页面的主页。 我在API响应中添加了一些字段,特别是获取帖子中插入的第一张图片的url的字段。 这是该部分的代码: 当我在主页中列出帖子时,它可以正常工作,但在单个帖子页面中,字段为空。对此,我唯一能给出的解释是,我有一个用于单个帖子的自定义endpoint:

  • 我正在从Ajax JSON创建一个DataTable。 将创建DataTables,但它显示一个错误: DataTables警告:表ID=CHANGETABLE-为行0,列0请求未知参数“0”。有关此错误的详细信息,请参阅http://datatables.net/TN/4 我的JSON看起来如下所示 在我的Java控制器中创建了这样的JSON对象: 我不知道如何将此信息与 属性一起使用,以使其适