本文主要记录Spring零配置的方法,包括相关类以及注解的使用方法。java
传统的servlet都是在web.xml
中配置,从Servlet 3.0
开始提供了ServletContainerInitializer
接口,容许使用代码去配置servlets
、filters
、listeners
。mysql
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的配置主要就是配置各类Bean,主要是要了解几种注解的使用方法。sql
使用@Configuration
注解的类至关于传统配置文件中的Beans,该类中的方法能够经过@Bean
标注成为Bean。app
@Configuration public class SpringConfig { @Bean(name = "exampleBean") public ExampleBean getExampleBean() { return new ExampleBean(); } }
使用@ComponentScan
用来标明要扫描注解的包,至关于配置文件中的context:component-scan
,Spring会自动扫描注册指定包中使用注解指定的Bean。ide
@ComponentScan(basePackages = {"com.example.service","com.example.dao"})
使用@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=... ...