本文主要記錄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=... ...