package com.softeem;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
/**
* 创作作者:ctl
* 创作时间:2022/7/11 17:06
* 项目名称
*/
@Configuration //把一个类作为一个loC容 器 ,它的某个方法头上如果注册7@Bean , 就会作为这个Spring容器中的Bean
@ComponentScan //默认会扫描该类所在的包下所有的配置类,相当于之前的 <context:component-scan>
@EnableAspectJAutoProxy //启用aop支持
@MapperScan("com.softeem.mapper")//指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类
@EnableTransactionManagement //启用事务支持
@EnableWebMvc //启用mvc
public class AppConfig {
public static void main(String[] args) throws LifecycleException {
/* AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);*/
Tomcat tomcat = new Tomcat();
// 设置端口
tomcat.setPort(Integer.getInteger("port",8080));
tomcat.getConnector();
// 创建容器,设置资源
Context ctx = tomcat.addWebapp("",
new File("src/main/webapp").getAbsolutePath());
WebResourceRoot resources = new StandardRoot(ctx);
// 定义class源文件的资源目录位置
resources.addPreResources(
new DirResourceSet(
resources,"/WEB-INF/classes",
new File("target/classes").getAbsolutePath(),
"/"
)
);
ctx.setResources(resources);
tomcat.start();
tomcat.getServer().await();
}
//在IOC容器中定义DataSource对象
@Bean
DataSource createDataSource(){
HikariDataSource dataSource = new HikariDataSource();
dataSource.setUsername("root");
dataSource.setPassword("19990822chen");
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test?serverTimezone=UTC");
return dataSource;
}
// 将该方法返回的对象装载到容器中
@Bean
SqlSessionFactoryBean creteFactoryBean(@Autowired DataSource dataSource) throws IOException {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
//设置数据源
sessionFactory.setDataSource(dataSource);
//设置映射文件位置(可选)
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
getResources("classpath:mapper/*.xml"));
return sessionFactory;
}
//启用事物
@Bean
PlatformTransactionManager createTx(@Autowired DataSource dataSource){
DataSourceTransactionManager transactionManagement
= new DataSourceTransactionManager(dataSource);
return transactionManagement;
}
//视图解析器
@Bean
ViewResolver viewResolver(){
InternalResourceViewResolver viewResolver
= new InternalResourceViewResolver();
viewResolver.setSuffix(".jsp");
return viewResolver;
}
//静态资源解析器
@Bean
WebMvcConfigurer webMvcConfigurer(){
return new WebMvcConfigurer() {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**")
.addResourceLocations("/css/");
}
};
}
}
spring5的相关配置
最新推荐文章于 2024-02-24 14:26:53 发布