Spring零配置文件项目搭建

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

Servlet配置

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

Spring为我们提供了一个该接口的实现类SpringServletContainerInitializer,查看源代码可以知道该类通过@HandlesTypes()注解指定了onStartup()方法的第一个参数接收WebApplicationInitializer实现类的集合。所以如果我们要使用这种方式配置servlet,只需要实现WebApplicationInitializer接口即可。

具体实现代码:

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,主要是要了解几种注解的使用方法。

@Configuration

使用@Configuration注解的类相当于传统配置文件中的Beans,该类中的方法可以通过@Bean标注成为Bean。

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

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

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

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

@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文件内容:

ds.driver.classname=com.mysql.jdbc.Driver
ds.url=jdbc:mysql://...
ds.username=...
ds.password=...
...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值