Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在XML配置中注入自动连线bean?_Java_Xml_Spring_Annotations - Fatal编程技术网

Java 如何在XML配置中注入自动连线bean?

Java 如何在XML配置中注入自动连线bean?,java,xml,spring,annotations,Java,Xml,Spring,Annotations,我的Dao和DaoImpl类如下: public interface MyDao{ } 我需要将MyDaoImpl注入到类Driver.java中的变量myDao中 public class Driver{ MyDao myDao; public MyDao getMyDao() { return myDao; } public void setMyDao(MyDao myDao) { this.myDao = myDao; } }

我的Dao和DaoImpl类如下:

public interface MyDao{
}
我需要将MyDaoImpl注入到类Driver.java中的变量myDao中

public class Driver{
   MyDao myDao;
   public MyDao getMyDao() {
        return myDao;
   }
   public void setMyDao(MyDao myDao) {
        this.myDao = myDao;
   }
}
现在的问题是,我正在使用XML创建bean,如下所示:

<bean id="driver123" class="com.Driver">
</bean>

如何使用XML将MyDaoImpl(通过注释创建)的对象注入这个bean中? 如果MyDaoImpl是通过XML创建的,我就可以使用属性和ref-config


但是我在这里该怎么做呢?

首先要为这样的配置创建Config类

package com.example.amer.config;

import com.example.amer.dao.MyDaoImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {

    @Bean(name = "myDaoImpl")
    public MyDaoImpl myDaoImpl()
    {
        MyDaoImpl myDao = new MyDaoImpl();
        return myDao;
    }
}
<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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Scan the JavaConfig -->
    <context:component-scan base-package="com.example.amer.config" />

    <bean id="driver" class="com.example.amer.Driver">
        <property name="myDao" ref="myDaoImpl"></property>
    </bean>

</beans>
这里我们创建MyDaoImpl Bean

和MyDao接口

package com.example.amer.dao;

public interface MyDao {

    public String getUserByName();
}
驾驶舱呢

package com.example.amer;

import com.example.amer.dao.MyDao;

public class Driver {
    MyDao myDao;

    public MyDao getMyDao() {
        return myDao;
    }

    public void setMyDao(MyDao myDao) {
        this.myDao = myDao;
    }

}
接下来,我将创建如下xml bean

package com.example.amer.config;

import com.example.amer.dao.MyDaoImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {

    @Bean(name = "myDaoImpl")
    public MyDaoImpl myDaoImpl()
    {
        MyDaoImpl myDao = new MyDaoImpl();
        return myDao;
    }
}
<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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Scan the JavaConfig -->
    <context:component-scan base-package="com.example.amer.config" />

    <bean id="driver" class="com.example.amer.Driver">
        <property name="myDao" ref="myDaoImpl"></property>
    </bean>

</beans>

在这里,我们用xml扫描包含MyDao Impl的Java配置 我创建驱动程序bean并定义myDao属性,然后我引用mydaoimplbean,其名称在@bean中定义(name=“MyDaoImpl”)


所以它会起作用

我可以使用属性和ref配置,您仍然可以这样做。该bean的名称是
apininboundmanager
@SotiriosDelimanolis您可以提供一些代码吗?只要您的XML或注释配置以某种方式导入,
您就可以尝试这种方法<代码>尝试了相同的方法。我在尝试获取cfg.getApplicationContext().getBean(beanId)行中的bean时遇到异常,原因是:org.springframework.beans.factory.nosuchBean定义异常:没有名为“MyDao”的bean可用