我们在使用Spring框架中,特别是框架级的功能,经常看到有@Import导入功能,
我就介绍下它能导入什么,首先声明下@Import是注解,导入类型可分为三类:
1. 导入配置 @Configuration,类似于spring早期版本2.5的import 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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
">
<import resource="cms-validator-service.xml"/>
<import resource="cms-validator-dao.xml"/>
</beans>
只是现在注解抢了风头,但目的一样,用于使用所有标有@configuration注解的配置。
下面我就写个小例子,怎么建java项目就略了
先建java主包com.spring, 然后分别建子包
com.spring.service, com.spring.service.impl, com.spring.config, com.spring.test
1.1 建立服务接口
package com.spring.service;
/**
*
* @author dgm
* @describe "日志服务接口"
*/
public interface LogService {
void print(String message);
}
1.2 建立服务实现类,分三种情况,控制台、文件和数据库mysql
package com.spring.service.impl;
import org.springframework.stereotype.Component;
import com.spring.service.LogService;
/**
* @author dgm
* @describe "日志到控制台"
*/
@Component
public class StdOutLogServiceImpl implements LogService {
@Override
public void print(String message) {
System.out.println(message);
System.out.println("写日志到控制台!");
}
}
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.springframework.stereotype.Component;
import com.spring.service.LogService;
/**
*
* @author dgm
* @describe "日志到文件"
*/
@Component
public class FileLogServiceImpl implements LogService {
private static final String FILE_NAME="d://LogService.txt";
@Override
public void print(String message) {
try {
File file = new File(FILE_NAME);
FileWriter fw = null;
// true:表示是追加的标志
fw = new FileWriter(file, true);
fw.write(message+"\n");
fw.close();
System.out.println(message);
System.out.println("写日志入文件!");
} catch (IOException e) {
}
}
}
/**
* @author dgm
* @describe "写日志入mysql数据库"
*/
@Component
public class MysqlLogServiceImpl implements LogService {
@Override
public void print(String message) {
System.out.println(message);
System.out.println("写日志入数据库");
}
}
1.3 写配置类,三个服务实现类对应三个@Configuration
package com.spring.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.spring.service.LogService;
import com.spring.service.impl.StdOutLogServiceImpl;
@Configuration
public class StdOutConfig {
@Bean(name="stdOutLogServiceImpl")
public LogService stdOutLogServiceImpl(){
return new StdOutLogServiceImpl();
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.spring.service.LogService;
import com.spring.service.impl.FileLogServiceImpl;
@Configuration
public class FileLogConfig {
@Bean(name="fileLogServiceImpl")
public LogService fileLogServiceImpl(){
return new FileLogServiceImpl();
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.spring.service.LogService;
import com.spring.service.impl.MysqlLogServiceImpl;
@Configuration
public class MysqlLogConfig {
@Bean(name="mysqlLogServiceImpl")
public LogService mysqlLogServiceImpl(){
return new MysqlLogServiceImpl();
}
}
然后@Import注解登场了
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotatio