Spring中用java config簡化xml配置

我的感受仍是這種配置方式最靈活了。java

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個類。而後咱們來測試。spring

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進行自動裝配。this

bean.xml配置文件以下:spa

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

測試類。code

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


經過configuration配置類引入xml配置文件orm

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

<?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類型的對象。

相關文章
相關標籤/搜索