jingzi123456789的专栏

每天一小步、三天一大步

Spring基于Java类配置Bean(四):通过@Configuration配置类引用xml配置信息

总结:

在@Configuration配置类中,通过@ImportResource就可以引入xml配置文件,在配置类中可直接通过@AutoWired引用xml文件中定义的bean。

(1)XML配置文件

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

    <!-- <context:component-scan base-package="com.study.spring.bean.javaClassBean"/> -->
    <bean id = "userDao"  class = "com.study.spring.bean.javaClassBean.UserDao"/>

</beans>

(2)@Configuration配置类

package com.study.spring.bean.javaClassBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource("classpath:conf/spring/spring-conf.xml") //(1)通过@ImportResource引入xml配置文件
public class SpringBeansConfig {

    @Bean
    @Autowired //(2)自动注入xml配置文件定义的Bean
    public UserService userService(UserDao userDao) {
        UserService userService = new UserService();
        userService.setUserDao(userDao);
        return userService;
    }
}

(3)UserService类

package com.study.spring.bean.javaClassBean;

import com.study.spring.interfaces.IUserService;
import java.util.List;

/**
 * 业务层
 */
public class UserService implements IUserService {

    private UserDao userDao;
    //要有set方法
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public List<User> queryUserList() {
        if (null != userDao) {
            return userDao.queryUserList();
        }
        return null;
    }
}

(4)测试类

package com.study.spring.bean.javaClassBean;

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
//import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestJavaClassBean {

    public static void main(String[] args) {

        //方法一:AnnotationConfigApplicationContext类通过标注@Configuration的Java类启动Spring容器。
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringBeansConfig.class);
        UserService userService = context.getBean(UserService.class);

        //方法二:AnnotationConfigApplicationContext支持通过编码的方式加载多个@Configuration配置类,然后通过刷新容器因应用这些配置类。
        //AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        //注册一个活或多个@Configuration配置类
        //context.register(SpringBeansConfig.class);
        //继续注册@Configuration配置类
        //context.register(SpringConfig.class);
        //刷新容器以应用这些注册的配置类
        //context.refresh();

        //ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("conf/spring/spring-conf.xml");
        //UserDao userDao = (UserDao)context.getBean("userDaoTemp");
        //List<User> userList = userDao.queryUserList();
        //UserService userService = (UserService)context.getBean("userService");

        List<User> userList = userService.queryUserList();

        if (null != userList) {
            for (User user : userList) {
                System.out.println(user);
            }
        } else {
            System.out.println("userList is null...");
        }

        //context.destroy();
    }
}

(5)测试结果

信息: Loading XML bean definitions from class path resource [conf/spring/spring-conf.xml]
User [userName=userName1, password=password1, updateTime=1526433618543]
User [userName=userName2, password=password2, updateTime=1526433618543]
User [userName=userName3, password=password3, updateTime=1526433618543]
User [userName=userName4, password=password4, updateTime=1526433618543]
User [userName=userName5, password=password5, updateTime=1526433618543]

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jingzi123456789/article/details/80332649
个人分类: Spring4.X
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Spring基于Java类配置Bean(四):通过@Configuration配置类引用xml配置信息

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭