Spring零配置文件项目搭建

本文主要记录Spring零配置的方法,包括相关类以及注解的使用方法。java

Servlet配置

传统的servlet都是在web.xml中配置,从Servlet 3.0开始提供了ServletContainerInitializer接口,容许使用代码去配置servletsfilterslistenersmysql

Spring为咱们提供了一个该接口的实现类SpringServletContainerInitializer,查看源代码能够知道该类经过@HandlesTypes()注解指定了onStartup()方法的第一个参数接收WebApplicationInitializer实现类的集合。因此若是咱们要使用这种方式配置servlet,只须要实现WebApplicationInitializer接口便可。web

具体实现代码:spring

public class WebInitializer implements WebApplicationInitializer {

    private static final Logger logger = LoggerFactory.getLogger(WebInitializer.class);

    @Override
    public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException {
        logger.info("begin init web application.");

        //配置Spring
        AnnotationConfigWebApplicationContext springContext = new AnnotationConfigWebApplicationContext();
        springContext.register(SpringConfig.class);
        
        //添加linstener
        servletContext.addListener(new ContextLoaderListener(springContext));

        //添加servlet
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
                "dispatcher", new DispatcherServlet(springContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

        //添加filter
        LoggerFilter loggerFilter = new LoggerFilter();
        FilterRegistration.Dynamic logFilterRegistration=container.addFilter("requestResponseLogFilter", loggerFilter);
        logFilterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC), false, "/*");

        logger.info("init web application success.");
    }
}

Spring配置

Spring的配置主要就是配置各类Bean,主要是要了解几种注解的使用方法。sql

@Configuration

使用@Configuration注解的类至关于传统配置文件中的Beans,该类中的方法能够经过@Bean标注成为Bean。app

@Configuration
public class SpringConfig {
    @Bean(name = "exampleBean")
    public ExampleBean getExampleBean() {
        return new ExampleBean();
    }
}
@ComponentScan

使用@ComponentScan用来标明要扫描注解的包,至关于配置文件中的context:component-scan,Spring会自动扫描注册指定包中使用注解指定的Bean。ide

@ComponentScan(basePackages = {"com.example.service","com.example.dao"})
@PropertySource

使用@PropertySource注解能够引入properties配置文件,经过注入Environment对象能够很方便的拿到配置文件中的内容。ui

@Configuration
@PropertySource("classpath:config.properties")
@ComponentScan(basePackages = {"com.example.service","com.example.dao"})
public class SpringConfig {

    @Autowired
    private Environment env;

    @Bean(name = "mysqlDataSource")
    public DataSource mysqlDataSource() {
        ProxoolDataSource dataSource = new ProxoolDataSource();
        dataSource.setDriver(env.getProperty("ds.driver.classname"));
        dataSource.setDriverUrl(env.getProperty("ds.url"));
        dataSource.setUser(env.getProperty("ds.username"));
        dataSource.setPassword(env.getProperty("ds.password"));
        dataSource.setPrototypeCount(env.getProperty("proxool.prototype", Integer.class));
        dataSource.setMinimumConnectionCount(env.getProperty("proxool.minimum", Integer.class));
        dataSource.setMaximumConnectionCount(env.getProperty("proxool.maximum", Integer.class));
        dataSource.setSimultaneousBuildThrottle(env.getProperty("proxool.simultaneous", Integer.class));
        dataSource.setTestBeforeUse(true);
        dataSource.setHouseKeepingTestSql(env.getProperty("proxool.testSql"));
        return dataSource;
    }

}

config.properties文件内容:url

ds.driver.classname=com.mysql.jdbc.Driver
ds.url=jdbc:mysql://...
ds.username=...
ds.password=...
...
相关文章
相关标签/搜索