简体   繁体   中英

How to inject Autowired beans inside XML configuration?

I have my Dao and DaoImpl classes as below:

public interface MyDao{
}
@Service(value = "MyDao")
public class MyDaoImpl implements MyDao{
}

I need MyDaoImpl inject to the variable myDao in the class Driver.java

public class Driver{
   MyDao myDao;
   public MyDao getMyDao() {
        return myDao;
   }
   public void setMyDao(MyDao myDao) {
        this.myDao = myDao;
   }
}

Now the problem is I am creating the bean using XML something like this:

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

How do I inject an object of MyDaoImpl(created through Annotation) inside this bean using XML? Had MyDaoImpl been created though XML, I could have used the property and ref config.

But how do I do here?

First You create Config Class for Configuration like this

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;
    }
}

here we create MyDaoImpl Bean

and MyDao interface

package com.example.amer.dao;

public interface MyDao {

    public String getUserByName();
}

and the Driver class

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;
    }

}

next i will create the xml beans like this

<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>

here in xml we scan about Java Configuration that contains MyDao Impl the i create the Driver bean and define to myDao Property then i refer to MyDaoImpl bean with it's name defined in @Bean(name="myDaoImpl")

so it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM