SpringBoot环境搭建

一、

1.Spring的纯注解配置

1.1.JdbcConfig

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
 
@PropertySource("classpath:db.properties")
public class JdbcConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String userName;
    @Value("${jdbc.password}")
    private String password;

    @Bean("dataSource")
    public DataSource getDatasource() {
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(userName);
        ds.setPassword(password);
        return ds;
    }
}

1.2.MybatisConfig

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
 
import javax.sql.DataSource;
 
public class MyBatisConfig {
    @Bean
    public SqlSessionFactoryBean getSqlSessionFactoryBean(
             DataSource dataSource){
        SqlSessionFactoryBean ssfb=new SqlSessionFactoryBean();
        ssfb.setDataSource(dataSource);
        ssfb.setTypeAliasesPackage("com.by.pojo");
        return ssfb;
    }
    @Bean
    public MapperScannerConfigurer getMapperScannerConfigurer(){
        MapperScannerConfigurer msc=new MapperScannerConfigurer();
        msc.setBasePackage("com.by.mapper");
        return msc;
    }
}

1.3.TxConfig

import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;

public class TxConfig {

    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource){
        DataSourceTransactionManager ds = new DataSourceTransactionManager();
        ds.setDataSource(dataSource);
        return ds;
    }
}

1.4.SpringConfig

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@Import({MyBatisConfig.class, JdbcConfig.class, TxConfig.class})
//等同`
@ComponentScan(value = "com.by.service")
//等同于,bean的名称默认取transactionManager
@EnableTransactionManagement
public class SpringConfig {
 
}

1.5.SpringMvcConfig

import com.by.converter.MyDateConverter;
import com.by.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
//等同于@ComponentScan("com.by.controller")
//等同于
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/pages/"); // 设置视图文件所在目录
        viewResolver.setSuffix(".jsp"); // 设置视图文件后缀名
        return viewResolver;
    }

    @Bean
    public CommonsMultipartResolver multipartResolver(){
        CommonsMultipartResolver commonsMultipartResolver=new CommonsMultipartResolver();
        return commonsMultipartResolver;
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new MyDateConverter());
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/emp/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/head/**").addResourceLocations("/head/");
    }
}

1.6.WebConfig

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * 当类扩展了AbstractAnnotationConfigDispatcherServletlnitializer并将其部署到 Servlet容器时,
 * 容器会自动发现它,并用它来配置Servlet环境
 */
public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    
    //加载Spring配置类
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }
    
    //加载SpringMVC配置类
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }
    
    //设置SpringMVC请求地址拦截规则
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
    
    //设置post请求中文乱码过滤器
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("utf-8");
        return new Filter[]{filter};
    }
}


1.7 修改web.xml


<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">
    <welcome-file-list>
        <welcome-file>login.jspwelcome-file>
    welcome-file-list>
web-app>

2.注解说明

2.1 @Configuration

作用:
用于指定当前类是一个spring配置类,可替代xml配置文件,作用和在spring的xml配置文件是一样的

2.2 @ComponentScan

作用:
用于指定spring在初始化容器时要扫描的包,作用和在spring的xml配置文件中的是一样的
属性:
basePackages:用于指定要扫描的包。和该注解中的value属性作用一样。

2.3 @Bean

作用:
该注解只能写在方法上,表明使用此方法创建一个对象,并且放入spring容器。作用和在spring的xml配置文件中的是一样的。
属性:
name:给当前@Bean注解方法创建的对象指定一个名称。

2.4 @PropertySourdce

作用:
用于加载*.properties文件中的配置。作用和在spring的xml配置文件中的:是一样的。
属性:
value[]:用于指定properties文件位置。如果是在类路径下,需要写上classpath;

2.5 @Import

作用:@Import注解是用来导入配置类或者一些需要前置加载的类。作用和在spring的xml配置文件中的是一样的。
属性:
value[]:用于指定其他配置类的字节码。

你可能感兴趣的:(spring,boot,java,spring)