@Import annotation in Spring

Role: Used to import other configuration classes.
Attributes:

  • value: Used to specify the bytecode of other configuration classes.

When we use @Import, the class annotated with @Import is the parent configuration class, and the imported ones are all child configuration classes.

Example ( based on @Configuration, @ComponentScan and @Bean annotations in Spring ):
SpringConfiguration class:

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 class:

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);
        }
    }
}

The following method configuration classes are side by side:
SpringConfiguration class:

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 class:

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 class:

    @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);
        }
    }

When we directly import bytecode in the AnnotationConfigApplicationContext class, this class can be scanned without configuring @Configuration.


You can also write the package to be scanned in @ComponentScan as follows:
SpringConfiguration class:

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 class:

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 class:

    @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);
        }
    }
Published 56 original articles · praised 0 · visits 521

Guess you like

Origin blog.csdn.net/qq_41242680/article/details/105659062