我知道可以配置一个 Spring应用程序 without the use of XML config files,并且已经提交了这个方法.但是,我不确定如何以这种方式申报 HTTP interceptors.我使用的是 this tutorial,它声明了以下XML. beans
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/welcome.htm">welcomeController</prop> </props> </property> <property name="interceptors"> <list> <ref bean="maintenanceInterceptor" /> <ref bean="executeTimeInterceptor" /> </list> </property> </bean> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"> <property name="interceptors"> <list> <ref bean="executeTimeInterceptor" /> </list> </property> </bean> <bean id="welcomeController" class="com.mkyong.common.controller.WelcomeController" /> <bean class="com.mkyong.common.controller.MaintenanceController" /> <bean id="executeTimeInterceptor" class="com.mkyong.common.interceptor.ExecuteTimeInterceptor" /> <bean id="maintenanceInterceptor" class="com.mkyong.common.interceptor.MaintenanceInterceptor"> <property name="maintenanceStartTime" value="23" /> <property name="maintenanceEndTime" value="24" /> <property name="maintenanceMapping" value="/SpringMVC/maintenance.htm" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/pages/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
如何用Java做到这一点?没有@Interceptor注释.
SpringApplication.java
@SuppressWarnings("WeakerAccess") @SpringBootApplication @PropertySources(value = {@PropertySource("classpath:/application.properties")}) public class SpringbackendApplication { @Autowired Environment env; public static void main(String[] args) { SpringApplication.run(SpringbackendApplication.class, args); initializeFirebase(); } @Bean public ViewResolver getViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } @Bean public UserController userController() { UserController userController = new UserController(getUserDAO(), getYodleeDAO()); userController.setCobrandSession(cobrandSession()); userController.setUserSessionManager(userSessionManager()); userController.setAccountsService(accountsService()); userController.setTransactionsService(transactionsService()); return userController; } @Bean public TestController testController() { TestController testController = new TestController(); testController.setCobrandSession(cobrandSession()); testController.setUserSessionManager(userSessionManager()); testController.setAccountsService(accountsService()); testController.setTransactionsService(transactionsService()); return testController; } @Bean public CobrandSession cobrandSession() { CobrandSession cobrandSession = new CobrandSession(); cobrandSession.setApiBase(this.env.getProperty("API_BASE")); cobrandSession.setLogin(this.env.getProperty("LOGIN")); cobrandSession.setPassword(this.env.getProperty("PASSWORD")); cobrandSession.setLocale(this.env.getProperty("LOCALE")); cobrandSession.setRestTemplate(restTemplate()); cobrandSession.setGson(gson()); return cobrandSession; } @Bean public AccountsService accountsService() { AccountsService accountsService = new AccountsService(); accountsService.setApiBase(this.env.getProperty("API_BASE")); accountsService.setRestTemplate(restTemplate()); accountsService.setGson(gson()); return accountsService; } @Bean public RestTemplate restTemplate() { SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setOutputStreaming(false); // If we don't turn this off, we may get HttpRetryException on 401's. return new RestTemplate(factory); } @Bean public Gson gson() { return new Gson(); } }要将XML文件中定义的Spring bean移动到Configuration类(用@Configuration标记),您需要这样的东西:
@Configuration public class MyConfig { @Bean(name="executeTimeInterceptor") public ExecuteTimeInterceptor getExecuteTimeInterceptor() { return new com.mkyong.common.interceptor.ExecuteTimeInterceptor(); } @Bean(name="maintenanceInterceptor") public MaintenanceInterceptor getMaintenanceInterceptor(@Value("${properties.maintenanceStartTime}") int maintenanceStartTime, @Value("${properties.maintenanceEndTime}") int maintenanceEndTime, @Value("${properties.maintenanceMapping}") String maintenanceMapping) { MaintenanceInterceptor myInt = new MaintenanceInterceptor(); myInt.setMaintenanceStartTime(maintenanceStartTime); myInt.setmMaintenanceEndTime(maintenanceEndTime); myInt.setMaintenanceMapping(maintenanceMapping); return myInt; } }
…然后在类路径的一些propertiesFile.properties中添加这些…
properties.maintenanceStartTime=23 properties.maintenanceEndTime=24 properties.maintenanceMapping=/SpringMVC/maintenance.htm
编辑
我看到你从环境中获取道具,所以不要使用@Value注入,而是立即使用你在代码中拥有它的方式.