码迷,mamicode.com
首页 > 编程语言 > 详细

Spring中用java config简化xml配置

时间:2014-11-02 22:40:26      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:http   io   ar   使用   java   sp   文件   on   log   

个人感觉还是这种方式配置方式最灵活了。


package com.baobaotao.conf;

public class LogDao {
    public void print(){
        System.out.println("helloworld");
    }
}
package com.baobaotao.conf;

public class UserDao {
    public void print(){
        System.out.println("Helloworld");
    }
}
package com.baobaotao.conf;

public class LogonService {
    public UserDao userDao;

    public LogDao logDao;

    public void setLogDao(LogDao logDao) {
        this.logDao = logDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void print() {
        System.out.println("helloworld");
    }
}

先定义上面3个类。然后我们来测试。

package com.baobaotao.conf;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;

@Configurable
public class AppConf {

    // 以下两个方法定义了两个Bean,并提供了Bean的实例化逻辑
    @Bean
    public UserDao userDao() {
        return new UserDao();
    }

    @Bean
    public LogDao logDao() {
        return new LogDao();
    }

    // 定义了logonService的Bean
    @Bean
    public LogonService logonService() {
        LogonService logonService = new LogonService();
        // 将上面定义的Bean注入到logonService Bean中
        logonService.setLogDao(logDao());
        logonService.setUserDao(userDao());
        return logonService;
    }
    
}

测试类。

package com.baobaotao.conf;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class ConfigTest {
    @Test
    public void test(){
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConf.class);
        LogonService logonService = ac.getBean(LogonService.class);
        logonService.print();
    }
}

如果bean在多个@Configuration中定义。

package com.baobaotao.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DaoConfig {
    @Bean
    public UserDao userDao(){
        return new UserDao();
    }
    
    @Bean
    public LogDao logDao(){
        return new LogDao();
    }
}
package com.baobaotao.conf;

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

@Configuration
public class ServiceConfig {
    //想普通Bean一样注入DaoConfig
    @Autowired
    private   DaoConfig daoConfig;
    
    @Bean
    public LogonService logonService(){
        LogonService logonService = new LogonService();
        //像普通Bean一样,调用Bean相关的方法
        logonService.setLogDao(daoConfig.logDao());
        logonService.setUserDao(daoConfig.userDao());
        return logonService;
    }
}

因为@Configuration是通过@Component进行元注解的,所以意味着通过@Configuration注解的类,可以被Spring的<context:component-scan>扫描到。故可以使用@Autowired进行自动装配。

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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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.baobaotao.conf" />
</beans>

测试类。

package com.baobaotao.conf;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ConfigTest {
    @Test
    public void test() {
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(
                "com/baobaotao/conf/bean.xml");
        LogonService logonService = ac.getBean(LogonService.class);
        logonService.print();
    }
}


通过component 扫描之后,使用@Configuration注解的类已经被组装到了xml文件中,所以可以使用应用程序上下文去得到这个bean。


通过configuration配置类引入xml配置文件

package com.baobaotao.conf;

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;

//通过@ImportResourcce引入XML配置文件
@Configuration
@ImportResource("classpath:com/baobaotao/conf/bean2.xml")
public class LogonAppConfig {
    
    //自动注入XML文件中定义的Bean
    @Bean
    @Autowired
    public LogonService logonService(UserDao userDao, LogDao logDao){
        LogonService logonService = new LogonService();
        logonService.setUserDao(userDao);
        logonService.setLogDao(logDao);
        return logonService;
    }
}

引用的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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <bean id="userDao" class="com.baobaotao.conf.UserDao"/>
    <bean id="logDao" class="com.baobaotao.conf.LogDao"/>
</beans>

测试类。

package com.baobaotao.conf;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ConfigTest {
    @Test
    public void test() {
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(
                "com/baobaotao/conf/bean.xml");
        LogonService logonService = ac.getBean(LogonService.class);
        logonService.print();
    }
}

Spring中用java config简化xml配置

标签:http   io   ar   使用   java   sp   文件   on   log   

原文地址:http://my.oschina.net/firebroo/blog/340020

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!