目录java
最近才体会到Spring注解配置的遍历,总结一下。web
@Configuration @EnableWebMvc @ComponentScan("cn.fjhdtp.maventest.controller") public class SpringMvcConfig { @Bean public InternalResourceViewResolver internalResourceViewResolver() { InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver(); internalResourceViewResolver.setViewClass(JstlView.class); internalResourceViewResolver.setPrefix("/WEB-INF/views/"); internalResourceViewResolver.setSuffix(".jsp"); return internalResourceViewResolver; } }
@Configuration
代表这是一个配置类;@EnableWebMvc
启用SpringMVC。redis
public class WebInitializer implements WebApplicationInitializer{ public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(SpringMvcConfig.class); ctx.setServletContext(servletContext); ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); servlet.addMapping("/"); servlet.setLoadOnStartup(1); } }
实现__WebApplicationInitializer__的类的__onStartup__方法会在Spring启动以后被执行,而且这个优先级在listener以前。能够用@Order(100)
注解来配置执行的顺序,没有这个注解则表明是最早执行。spring
相似<context:component-scan />
。mvc
@ComponentScan(value = "cn.fjhdtp.maventest", excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Configuration.class, Controller.class}), })
与@Configuration
结合使用,读取properties文件app
@PropertySources({ @PropertySource(value = "classpath:jedis.properties") })
与@PropertySource
或者<context:property-placeholder/>
结合使用,注入属性值。jsp
@Value("${redis.hostName:127.0.0.1}") private String hostName; // 主机名 @Value("${redis.port:6379}") private int port; // 监听端口 @Value("${redis.auth:}") private String password; // 密码 @Value("${redis.timeout:2000}") private int timeout; // 客户端链接时的超时时间(单位为秒)
@Controller
代表这个类是一个controller,和@RequestMapping
结合来配置映射的路径。maven
@Component
代表这是一个Bean,可是若是知道属于那一层最好仍是用@Service
或者@Repository
。spa
用在Service层。code
用在Dao层。