Spring中用java config简化xml配置

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

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.Configuration;
import org.springframework.context.annotation.Bean;

//当通过手动注册配置类的时候,这个可以不写,如果想通过应用程序上下文获得这个bean,这个必须写
@Configuration
public class AppConf {

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

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

    // 定义了LogonService的Bean,名字是logonService1
    @Bean(name="logonService1")
    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,名字为logonService1的bean,如果没有指定名字,默认寻找匹配的类型.
        LogonService logonService = ac.getBean("logonService1",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进行自动装配。

bean.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:annotation-config/>
        <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() {
        //通过应用程序上下文获得bean
        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;
    }
}

引用的外部的bean2.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();
    }
}

一般我自己习惯使用应用程序上下文的方式去统一获得bean。

当然可以在@bean中指定initMethod和destoryMethod方法。替换AppConf的@bean替换为

@Bean(name="logonService1",initMethod="startLife",destroyMethod="die")

和在xml文件中指定是一样的效果。当然xml中定义的全部bean配置选项可以通过java Config对应的方式进行配置,全部等价。


下面我这段实现了几乎"零"xml配置。

比如说我要注入为一个属性注入一个Date类型的对象,通常的做法是在应用程序上下文中注册一个属性编辑器,用于将文本类型转化为Date对象类型,spring自带的CustomDateEditor类就是这个功能,首先我需要声明一个实例,然后使用CustomEditorConfigurer实例来注册这个属性编辑器。大概这样配置好需要20行代码左右。如果改用Configuration,只需要在xml中添加context:component-scan需要扫描的包就够了。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

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

@Configuration("configurationTest")
public class ConfigurationTest {
	
	@Autowired
	@Qualifier("fromDate")
	private Date fromDate;
	
	@Bean(name = "fromDate")
	public Date date() throws ParseException{
		return new SimpleDateFormat("yyyy-MM-dd").parse("2007-09-11");
	}
}

好了,就这样,我们已经为属性fromDate注入了一个Date类型的对象。

转载于:https://my.oschina.net/firebroo/blog/340020

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值