91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Sprigmvc項目轉為springboot的方法

發布時間:2020-09-13 01:41:36 來源:腳本之家 閱讀:217 作者:geeksblog 欄目:編程語言

是否有老掉牙的springmvc項目,想轉成springboot項目,看這個文章就對了。

說明

如果你的項目連maven項目都不是,請自行轉為maven項目,在按照本教程進行。

本教程適用于spring+springmvc+mybatis+shiro的maven項目。

1.修改pom文件依賴

刪除之前的spring依賴,添加springboot依賴

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>
<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>

      <!-- 這個是剔除掉自帶的 tomcat部署的-->
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
      </exclusions>

    </dependency>
    <!-- tomcat容器部署 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <!--<scope>compile</scope>-->
    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <optional>true</optional>
    </dependency>
    <!-- 支持 @ConfigurationProperties 注解 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <optional>true</optional>
    </dependency>
 <dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
 </dependency>
</dependencies>

添加springboot構建插件

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <source>1.7</source>
      <target>1.7</target>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>1.5.9.RELEASE</version>
    <executions>
      <execution>
        <goals>
          <goal>repackage</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>

2.添加application啟動文件

注意,如果Application在controller,service,dao的上一層包里,無需配置 @ComponentScan ,

否則,需要指明要掃描的包。

@SpringBootApplication
//@ComponentScan({"com.cms.controller","com.cms.service","com.cms.dao"})
public class Applicationextends SpringBootServletInitializer{

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
    return application.sources(Application.class);
  }

  public static void main(String[] args)throws Exception {
    SpringApplication.run(Application.class, args);
  }
}

3.添加springboot配置文件

在resources下面添加application.properties文件

添加基本配置
#默認前綴
server.contextPath=/
# 指定環境
spring.profiles.active=local
# jsp配置
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
#log配置文件
logging.config=classpath:logback-cms.xml
#log路徑
logging.path=/Users/mac/work-tommy/cms-springboot/logs/
#數據源
spring.datasource.name=adminDataSource
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/mycms?useUnicode=true&autoReconnect=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = 123456

4.使用@Configuration注入配置

注入mybatis配置,分頁插件請自主選擇

@Configuration
@MapperScan(basePackages = "com.kuwo.dao",sqlSessionTemplateRef = "adminSqlSessionTemplate")
public class AdminDataSourceConfig{

  @Bean(name = "adminDataSource")
  @ConfigurationProperties(prefix = "spring.datasource")
  @Primary
  public DataSource adminDataSource(){
    return DataSourceBuilder.create().build();
  }

  @Bean(name = "adminSqlSessionFactory")
  @Primary
  public SqlSessionFactory adminSqlSessionFactory(@Qualifier("adminDataSource")DataSource dataSource)throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    //分頁插件
// PageHelper pageHelper = new PageHelper();
    PagePlugin pagePlugin = new PagePlugin();
// Properties props = new Properties();
// props.setProperty("reasonable", "true");
// props.setProperty("supportMethodsArguments", "true");
// props.setProperty("returnPageInfo", "check");
// props.setProperty("params", "count=countSql");
// pageHelper.setProperties(props);
    //添加插件
    bean.setPlugins(new Interceptor[]{pagePlugin});
    // 添加mybatis配置文件
    bean.setConfigLocation(new DefaultResourceLoader().getResource("classpath:mybatis/mybatis-config.xml"));
    // 添加mybatis映射文件
    bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/system/*.xml"));
    return bean.getObject();
  }

  @Bean(name = "adminTransactionManager")
  @Primary
  public DataSourceTransactionManager adminTransactionManager(@Qualifier("adminDataSource")DataSource dataSource){
    return new DataSourceTransactionManager(dataSource);
  }

  @Bean(name = "adminSqlSessionTemplate")
  @Primary
  public SqlSessionTemplate adminSqlSessionTemplate(@Qualifier("adminSqlSessionFactory")SqlSessionFactory sqlSessionFactory)throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
  }
}

添加Interceptor配置,注意addInterceptor的順序,不要搞亂了

@Configuration
public class InterceptorConfigurationextends WebMvcConfigurerAdapter{
  @Override
  public void addInterceptors(InterceptorRegistry registry){
    registry.addInterceptor(new LoginHandlerInterceptor());
  }
}

添加shiro配置文件

注意:本來使用redis做session緩存,但是和shiro集成發現一個問題,user對象存儲以后,從shiro中獲取后,無法進行類型轉換,所以暫時放棄了redis做session緩存。

@Configuration
public class ShiroConfiguration{
  @Value("${spring.redis.host}")
  private String host;

  @Value("${spring.redis.port}")
  private int port;

  @Value("${spring.redis.timeout}")
  private int timeout;
  @Bean
  public static LifecycleBeanPostProcessor getLifecycleBeanPostProcessor(){
    return new LifecycleBeanPostProcessor();
  }

  /**
   * ShiroFilterFactoryBean 處理攔截資源文件問題。
   * 注意:單獨一個ShiroFilterFactoryBean配置是或報錯的,因為在
   * 初始化ShiroFilterFactoryBean的時候需要注入:SecurityManager
   *
   Filter Chain定義說明
   1、一個URL可以配置多個Filter,使用逗號分隔
   2、當設置多個過濾器時,全部驗證通過,才視為通過
   3、部分過濾器可指定參數,如perms,roles
   *
   */
  @Bean
  public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager){
    System.out.println("ShiroConfiguration.shirFilter()");
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

    // 必須設置 SecurityManager
    shiroFilterFactoryBean.setSecurityManager(securityManager);
    // 如果不設置默認會自動尋找Web工程根目錄下的"/login.jsp"頁面
    shiroFilterFactoryBean.setLoginUrl("/login_toLogin");
    // 登錄成功后要跳轉的鏈接
    shiroFilterFactoryBean.setSuccessUrl("/usersPage");
    //未授權界面;
    shiroFilterFactoryBean.setUnauthorizedUrl("/403");
    //攔截器.
    Map<String,String> filterChainDefinitionMap = new LinkedHashMap<>();

    //配置退出 過濾器,其中的具體的退出代碼Shiro已經替我們實現了
    filterChainDefinitionMap.put("/logout", "logout");
    filterChainDefinitionMap.put("/login_toLogin", "anon");
    filterChainDefinitionMap.put("/login_login", "anon");
    filterChainDefinitionMap.put("/static/login/**","anon");
    filterChainDefinitionMap.put("/static/js/**","anon");
    filterChainDefinitionMap.put("/uploadFiles/uploadImgs/**","anon");
    filterChainDefinitionMap.put("/code.do","anon");
    filterChainDefinitionMap.put("/font-awesome/**","anon");
    //<!-- 過濾鏈定義,從上向下順序執行,一般將 /**放在最為下邊 -->:這是一個坑呢,一不小心代碼就不好使了;
    //<!-- authc:所有url都必須認證通過才可以訪問; anon:所有url都都可以匿名訪問-->

    filterChainDefinitionMap.put("/**", "authc");


    shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
    return shiroFilterFactoryBean;
  }


  @Bean
  public SecurityManager securityManager(){
    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
    //設置realm.
    securityManager.setRealm(myShiroRealm());
    // 自定義緩存實現 使用redis
    //securityManager.setCacheManager(cacheManager());
    // 自定義session管理 使用redis
    securityManager.setSessionManager(sessionManager());
    return securityManager;
  }

  @Bean
  public ShiroRealm myShiroRealm(){
    ShiroRealm myShiroRealm = new ShiroRealm();
// myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher());
    return myShiroRealm;
  }

}
  /**
   * 開啟shiro aop注解支持.
   * 使用代理方式;所以需要開啟代碼支持;
   * @param securityManager
   * @return
   */
  @Bean
  public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
    AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
    authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
    return authorizationAttributeSourceAdvisor;
  }

  /**
   * 配置shiro redisManager
   * 使用的是shiro-redis開源插件
   * @return
   */
  public RedisManager redisManager(){
    RedisManager redisManager = new RedisManager();
    redisManager.setHost(host);
    redisManager.setPort(port);
    redisManager.setExpire(1800);
    redisManager.setTimeout(timeout);
    // redisManager.setPassword(password);
    return redisManager;
  }

  /**
   * cacheManager 緩存 redis實現
   * 使用的是shiro-redis開源插件
   * @return
   */
  public RedisCacheManager cacheManager(){
    RedisCacheManager redisCacheManager = new RedisCacheManager();
    redisCacheManager.setRedisManager(redisManager());
    return redisCacheManager;
  }

  /**
   * RedisSessionDAO shiro sessionDao層的實現 通過redis
   * 使用的是shiro-redis開源插件
   */
  @Bean
  public RedisSessionDAO redisSessionDAO(){
    RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
    redisSessionDAO.setRedisManager(redisManager());
    return redisSessionDAO;
  }

  @Bean
  public DefaultWebSessionManager sessionManager(){
    DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
// sessionManager.setSessionDAO(redisSessionDAO());
    return sessionManager;
  }

}

總結

搞了一天時間把項目轉成springboot,查閱各種資料,希望這篇文章能夠為你帶來幫助。也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

武强县| 泰兴市| 莱阳市| 长汀县| 九龙城区| 灵宝市| 汾西县| 靖西县| 黎川县| 三原县| 扎赉特旗| 临邑县| 屏东市| 青州市| 蕉岭县| 江安县| 南宫市| 泰来县| 阿巴嘎旗| 安平县| 华亭县| 金昌市| 望奎县| 巴林左旗| 长海县| 佛坪县| 稻城县| 饶河县| 临洮县| 莲花县| 育儿| 泰顺县| 凤翔县| 长垣县| 两当县| 怀宁县| 黄平县| 江华| 湖南省| 宣汉县| 都匀市|