目录
5.4、WebProjctionInitConfig.java
五、纯注解开发
核心: 配置类代替配置文件
步骤:
1.创建一个配置类SpringConfig.java,替代applicationContext.xml
2.创建一个配置类SpringMVCConfig.java,代替springmvc.xml
3.创建一个配置类MyBatisConfig.java,替代applicationContext_mybaits.xml
4.创建一个配置类WebProjectInitConfig来替代web.xml配置
5.在pom.xml中配置插件,表示war包不需要web.xml配置
不使用web.xml,就不能打war包,所以要配置插件,表示war包不需要web.xml配置
5.1、SpringConfig.java
@Configuration //声明当前类是一个配置类
//配置注解扫描,并且配置controller注解的排除
@ComponentScan(basePackages = "com.hhy",
excludeFilters = @ComponentScan.Filter(Controller.class))
@EnableAspectJAutoProxy //开启注解aop
@EnableTransactionManagement //开启注解事务
public class SpringConfig {
}
5.2、SpringMVCConfig.java
@Configuration
@ComponentScan("com.itheima.controller")
@EnableWebMvc //开启注解驱动
public class SpringMvcConfig extends WebMvcConfigurerAdapter {
@Bean
public InternalResourceViewResolver internalResourceViewResolver(){
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
//释放静态资源
//原理:在tomcat中,静态资源是由tomcat带的缺省的servlet来处理的
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
5.3、MyBatisConfig.java
@Configuration
@PropertySource("classpath:jdbc.properties") //导入外部配置
@MapperScan("com.itheima.dao") //包扫描方式创建mapper对象
public class MyBatisConfig {
@Value("${jdbc.username}") //读配置外部配置进行依赖注入
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.driverClass}")
private String driverClassName;
@Bean
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
//设置数据库连接池参数
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource){
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//设置数据源
sqlSessionFactoryBean.setDataSource(dataSource);
//设置别名映射
sqlSessionFactoryBean.setTypeAliasesPackage("com.hhy.pojo");
//设置<settings>相关配置
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
sqlSessionFactoryBean.setConfiguration(configuration);
SqlSessionFactory sqlSessionFactory = null;
try {
sqlSessionFactory = sqlSessionFactoryBean.getObject();
} catch (Exception e) {
e.printStackTrace();
}
return sqlSessionFactory;
}
}
5.4、WebProjctionInitConfig.java
public class ProjectInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class,MyBatisConfig.class}; //spring的配置类
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//添加过滤器
//servletContext.addFilter();
super.onStartup(servletContext);
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class}; //springMVC的配置类
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"}; // 配置dispatcherServlet的映射路径
}
}
5.5、pom.xml中声明不需要web.xml打war包
<!-- 声明打包时,不需要web.xml -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<!-- 声明不需要web.xml-->
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>