1.springWeb之java配置css
@Configuration @ComponentScan(basePackages = "com.wise.tiger",excludeFilters = @ComponentScan.Filter(Controller.class)) @PropertySource(value = "classpath:dbcp-config.properties", encoding = "UTF-8")//導入配置文件 @EnableAspectJAutoProxy//打開自動代理 @EnableTransactionManagement//提供事務支持 public class SpringIoCConfig { /** * 配置數據源 */ @Bean public DataSource dataSource( @Value("${driverClassName}") String driverClass, @Value("${url}") String url, @Value("${jdbc.username}") String username, @Value("${password}") String password, @Value("${defaultAutoCommit}") boolean defaultAutoCommit, @Value("${connectionProperties}") String connectionProperties) { var dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClass);dataSource.setUrl(url);dataSource.setUsername(username); dataSource.setPassword(password);dataSource.setDefaultAutoCommit(defaultAutoCommit); dataSource.setConnectionProperties(connectionProperties); return dataSource; } /** * 配置SqlSessionFactory * SqlSessionFactory是產生SqlSession的基礎,在MyBatis-Spring項目中提供了SqlSessionFactoryBean去支持SqlSessionFactory的配置 */ @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { var factoryBean = new SqlSessionFactoryBean(); //設置類型別名 factoryBean.setTypeAliasesPackage("com.wise.tiger.domain"); //設置數據源 factoryBean.setDataSource(dataSource); //ResourcePatternResolver定義了getResources來查找資源,PathMatchingResourcePatternResolver提供了以classpath開頭的通配符方式查詢 var resourcePatternResolver = new PathMatchingResourcePatternResolver(); factoryBean.setMapperLocations(resourcePatternResolver.getResources("classpath*:com/wise/tiger/mapper/**/*.xml")); return factoryBean.getObject(); } /** * 事務管理器(Mybaits採用DataSourceTransactionManger) */ @Bean public DataSourceTransactionManager transactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } }
@Configuration @EnableWebMvc @ComponentScan(basePackages = "com.wise.tiger.web") public class SpringMVCConfig implements WebMvcConfigurer { /** * 配置視圖解析器 * @return */ @Bean public InternalResourceViewResolver viewResolver(){ var viewResolver =new InternalResourceViewResolver(); viewResolver.setViewClass(org.springframework.web.servlet.view.JstlView.class); viewResolver.setPrefix("/WEB-INF/book/"); viewResolver.setSuffix(".jsp"); return viewResolver; } /** * 添加視圖映射靜態過濾器 * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations("/static/"); registry.addResourceHandler("/js/**").addResourceLocations("/js/"); registry.addResourceHandler("/css/**").addResourceLocations("/css/"); registry.addResourceHandler("/images/**").addResourceLocations("/images/"); } /** * 添加類型轉換器 * @param registry */ @Override public void addFormatters(FormatterRegistry registry) { registry.addConverter(new StringArray2SetRoleConverter()); } /** * 添加表單校驗器 */ @Override public Validator getValidator() { var validator = new LocalValidatorFactoryBean(); validator.setProviderClass(HibernateValidator.class); validator.setValidationMessageSource(messageResource()); return validator; } /** * 添加攔截器 * @param registry */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/user*").addPathPatterns("/content*"); } /** * 配置消息處理器 * @return */ @Bean public ReloadableResourceBundleMessageSource messageResource(){ var messageResource = new ReloadableResourceBundleMessageSource(); // messageResource.setBasename(""); return messageResource; } /** * 配置文件上傳解析器 * @return */ @Bean(name = "multipartResolver") public MultipartResolver multipartResolver() { //若return new CommonsMultipartResolver(); 則須要apache的commons-upload.jar組件支持,能夠定義文件上傳屬性,且無需再在ApplicationConfig中配置 return new StandardServletMultipartResolver(); } }
@Configuration public class ApplicationConfig extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[]{RootConfig.class}; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{ServletConfig.class}; } @Override protected String[] getServletMappings(){ return new String[]{"/"}; } @Override protected Filter[] getServletFilters() { return new Filter[]{new CharacterEncodingFilter("utf-8", true)}; } @Override protected void customizeRegistration(ServletRegistration.Dynamic registration) { registration.setMultipartConfig( new MultipartConfigElement(null,30*1024*1024,100*1024*1024,0)); } }
2.springWeb之xml配置java
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 開啓註解掃描--> <context:component-scan base-package="com.wise.tiger"> <!--過濾掉@Controller註解--> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <bean class="com.wise.tiger.aop.BookAspect" name="bookAspect2"/> <!-- 啓動aop註解支持 --> <aop:config> <!--定義切入點 --> <aop:pointcut expression="execution(* com.wise.tiger..*(..))" id="pointcut"/> <aop:aspect ref="xmlAspect"> <!-- 前置通知 --> <aop:before method="before" pointcut-ref="pointcut" /> <aop:after-returning method="afterReturn" pointcut-ref="pointcut"/> <aop:after method="after" pointcut-ref="pointcut"/> <aop:after-throwing method="throwing" pointcut-ref="pointcut" throwing="ex"/> <aop:around method="around" pointcut="execution(* com.wise.tiger..*(..)) and @annotation(authority)" /> </aop:aspect> </aop:config> <context:property-placeholder location="classpath:dbcp-config.properties"/> <!-- step1:配置數據源:DruidDataSource事務默認自動,須要關閉自動提交 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" > <property name="driverClassName" value="${driverClassName}" /> <property name="url" value="${url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${password}" /> <property name="defaultAutoCommit" value="${defaultAutoCommit}"/> <property name="connectionProperties" value="${connectionProperties}"/> </bean> <!-- step2:配置jdbc模板類 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <!--配置數據源事務管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--開啓註解管理事務--> <tx:annotation-driven transaction-manager="txManager"/> </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--開啓mvc註解驅動--> <mvc:annotation-driven/> <!--mvc掃描的包--> <context:component-scan base-package="com.wise.tiger.controller"/> <!--配置視圖解析器--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/> </bean> <!--配置攔截器--> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/controll/**"/> <bean class="com.wise.bbs.interceptor.RoleInterceptor"/> </mvc:interceptor> </mvc:interceptors> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--配置監聽器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--配置springIoC--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <!--配置編碼過濾器--> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--配置處理器分發器--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>