作用:用于导入其他的配置类。
属性:
- value:用于指定其他配置类的字节码。
当我们使用@Import之后,有@Import注解的类就是父配置类,而导入的都是子配置类。
示例(在Spring中@Configuration、@ComponentScan和@Bean注解基础上):
SpringConfiguration类:
package config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.*;
import javax.sql.DataSource;
//该类是一个配置类,它的作用和bean.xml是一样的
//@Configuration
@ComponentScan(basePackages = {"com.qublog"})
@Import(value = {JdbcConfig.class})
public class SpringConfiguration {
}
JdbcConfig类:
package config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import javax.sql.DataSource;
//和spring连接数据库相关的配置类
//@Configuration
public class JdbcConfig {
//用于创建一个QueryRunner对象
@Bean(name = "runner")
@Scope(value = "prototype")
public QueryRunner createQueryRunner(DataSource dataSource) {
return new QueryRunner(dataSource);
}
//创建数据源对象
@Bean(name = "dataSource")
public DataSource createDataSource() {
try {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass("com.mysql.cj.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/springtest?serverTimezone=UTC");
ds.setUser("root");
ds.setPassword("1234");
return ds;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
以下方法配置类是并列关系:
SpringConfiguration类:
package config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.*;
import javax.sql.DataSource;
//该类是一个配置类,它的作用和bean.xml是一样的
//@Configuration
@ComponentScan(basePackages = {"com.qublog"})
//@Import(value = {JdbcConfig.class})
public class SpringConfiguration {
}
JdbcConfig类:
package config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import javax.sql.DataSource;
//和spring连接数据库相关的配置类
//@Configuration
public class JdbcConfig {
//用于创建一个QueryRunner对象
@Bean(name = "runner")
@Scope(value = "prototype")
public QueryRunner createQueryRunner(DataSource dataSource) {
return new QueryRunner(dataSource);
}
//创建数据源对象
@Bean(name = "dataSource")
public DataSource createDataSource() {
try {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass("com.mysql.cj.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/springtest?serverTimezone=UTC");
ds.setUser("root");
ds.setPassword("1234");
return ds;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
AccountServiceTest类:
@Test
public void testFindAll() {
//获取容器
// ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class, JdbcConfig.class);
//得到业务层对象
AccountService as = (AccountService) ac.getBean("accountService");
//执行方法
List<Account> accounts = as.findAllAccount();
for (Account account:accounts) {
System.out.println(account);
}
}
当我们直接在AnnotationConfigApplicationContext类中导入字节码时,该类不需要配置@Configuration就可以被扫描。
也可以@ComponentScan中写入要扫描的包,如下:
SpringConfiguration类:
package config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.*;
import javax.sql.DataSource;
//该类是一个配置类,它的作用和bean.xml是一样的
//@Configuration
@ComponentScan(basePackages = {"com.qublog","config"})
//@Import(value = {JdbcConfig.class})
public class SpringConfiguration {
}
JdbcConfig类:
package config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import javax.sql.DataSource;
//和spring连接数据库相关的配置类
@Configuration
public class JdbcConfig {
//用于创建一个QueryRunner对象
@Bean(name = "runner")
@Scope(value = "prototype")
public QueryRunner createQueryRunner(DataSource dataSource) {
return new QueryRunner(dataSource);
}
//创建数据源对象
@Bean(name = "dataSource")
public DataSource createDataSource() {
try {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass("com.mysql.cj.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/springtest?serverTimezone=UTC");
ds.setUser("root");
ds.setPassword("1234");
return ds;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
AccountServiceTest类:
@Test
public void testFindAll() {
//获取容器
// ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
//得到业务层对象
AccountService as = (AccountService) ac.getBean("accountService");
//执行方法
List<Account> accounts = as.findAllAccount();
for (Account account:accounts) {
System.out.println(account);
}
}