当前位置: 首页 > news >正文

web网站开发案例的好书正规优化公司哪家好

web网站开发案例的好书,正规优化公司哪家好,怎样查看网站是用什么cms 做的,网站流量监控1.环境搭建 1.1.创建工程 1.2.待改造的问题 我们发现&#xff0c;之所以我们现在离不开xml配置文件&#xff0c;是因为我们有一处很关键的配置&#xff0c;如果他要也能用注解配置&#xff0c;那么我们就可以脱离xml文件了&#xff1a; 1.2.1.jdbc配置 <context:propert…

1.环境搭建

1.1.创建工程

在这里插入图片描述

1.2.待改造的问题

我们发现,之所以我们现在离不开xml配置文件,是因为我们有一处很关键的配置,如果他要也能用注解配置,那么我们就可以脱离xml文件了:

1.2.1.jdbc配置

<context:property-placeholder location="classpath:db.properties"></context:property-placeholder><!--数据源--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><property name="driverClassName" value="${jdbc.driver}" /><property name="maxActive" value="10" /><property name="minIdle" value="5" /></bean>

1.2.2.mybatis配置

    <!--sqlSession工厂--><bean id="sessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="typeAliasesPackage" value="com.by.pojo"></property></bean><!--配置MapperScan扫描mapper接口,并把生成的代理类交给spring去管理--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.by.mapper"></property><property name="sqlSessionFactoryBeanName" value="sessionFactoryBean"></property></bean>

1.2.3.transactional配置

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 开启spring对注解事务的支持 --><tx:annotation-driven transaction-manager="transactionManager"/>

1.2.4.service配置

    <!--扫描service--><context:component-scan base-package="com.by.service"></context:component-scan>

1.2.5.springmvc配置

<!--配置springmvc要扫描的包--><context:component-scan base-package="com.by.controller,com.by.exception"></context:component-scan><!--开启注解驱动:配置handlerMapping和handlerAdapter--><mvc:annotation-driven conversion-service="cs"></mvc:annotation-driven><!--配置日期转换器--><bean id="cs" class="org.springframework.context.support.ConversionServiceFactoryBean"><property name="converters"><set><bean class="com.by.converter.MyDateConverter"></bean></set></property></bean><!--配置视图解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/pages/"></property><property name="suffix" value=".jsp"></property></bean><!--配置文件上传解析器--><bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize" value="5242880" /><property name="defaultEncoding" value="UTF-8" /></bean><!--资源映射器:直接放行无须dispatcherServlet去处理--><mvc:resources location="/head/" mapping="/head/**"/><!--配置拦截器--><mvc:interceptors><mvc:interceptor><mvc:mapping path="/account/**"/><mvc:exclude-mapping path="/account/login"></mvc:exclude-mapping><bean class="com.by.interceptor.LoginInterceptor"></bean></mvc:interceptor></mvc:interceptors>

1.2.6.servlet配置

<!--配置监听器:监听tomcat启动,加载spring配置文件-->
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener><!--前端控制器-->
<servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern>
</servlet-mapping>
<!--过滤器-->
<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>
</filter>
<filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern>
</filter-mapping>

2.新注解说明

2.1.@Configuration

  • 作用:

    用于指定当前类是一个spring配置类,可替换xml配置文件,作用和在spring的xml配置文件中的:<beans></beans>是一样的。

  • 示例代码

    /*** spring的配置类*/
    @Configuration//等价于<beans>,当从一个类上加载到该注解时会创建spring容器
    public class SpringConfiguration{
    }
    

2.2.@ComponentScan

  • 作用:

    用于指定spring在初始化容器时要扫描的包。作用和在spring的xml配置文件中的:

    <context:component-scan base-package="com.by"/>是一样的。

  • 属性:

    basePackages:用于指定要扫描的包。和该注解中的value属性作用一样。

  • 示例代码

    /*** spring的配置类*/
    @Configuration
    @ComponentScan(basePackages = "com.by")//等价于<context:component-scan>
    public class SpringConfiguration{
    }
    

2.3.@Bean

  • 作用:

    该注解只能写在方法上,表明使用此方法创建一个对象,并且放入spring容器。作用和在spring的xml配置文件中的:<bean/>是一样的。

  • 属性:

    name:给当前@Bean注解方法创建的对象指定一个名称(即bean的id)。

  • 示例代码

    public class Dog {private String nam;private Integer age;//set get......
    }    
    
    @Bean
    public Dog dog(){Dog dog = new Dog();dog.setNam("二狗");dog.setAge(18);return dog;
    }
    

2.4.@PropertySource

  • 作用:

    用于加载.properties文件中的配置。作用和在spring的xml配置文件中的:<context:property-placeholder location="">是一样的。

  • 属性:

    value[]:用于指定properties文件位置。如果是在类路径下,需要写上classpath:

  • 示例代码

    #config.properties
    nam=二狗
    age=18
    
    @Configuration
    @PropertySource("classpath:config.properties")
    public class SpringConfiguration {@Value("${nam}")private String nam;@Value("${age}")private Integer age;@Beanpublic Dog dog(){Dog dog = new Dog();dog.setNam(nam);dog.setAge(age);return dog;}
    }
    

2.5.@Import

  • 作用:

    @Import注解是用来导入配置类或者一些需要前置加载的类。作用和在spring的xml配置文件中的:<import resource=""></import>是一样.

  • 属性:

    value[]:用于指定其他配置类的字节码。

  • 示例代码

    @Configuration
    @ComponentScan(basePackages = "com.by")
    @Import({Configuration_Other.class})
    public class SpringConfiguration {}@PropertySource("classpath:config.properties")
    public class Configuration_Other {}
    

3.Spring的纯注解配置

3.1.JdbcConfig

package com.by.config;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"})//<context:property-placeholder/>
public class DataSourceConfig {@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 //<bean/>public DataSource getDataSource(){DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName(driver);dataSource.setUrl(url);dataSource.setUsername(userName);dataSource.setPassword(password);return dataSource;}
}

3.2.MybatisConfig

package com.by.config;import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;import javax.annotation.Resource;
import javax.sql.DataSource;public class MyBatisConfig {@Bean("sessionFactoryBean")public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource){SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();sessionFactoryBean.setDataSource(dataSource);return sessionFactoryBean;}@Beanpublic MapperScannerConfigurer getMapperScannerConfigurer(){MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();mapperScannerConfigurer.setBasePackage("com.by.mapper");mapperScannerConfigurer.setSqlSessionFactoryBeanName("sessionFactoryBean");return mapperScannerConfigurer;}
}

3.3.TxConfig

package com.by.config;import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;import javax.sql.DataSource;public class TxManagerConfig {@Beanpublic PlatformTransactionManager getPlatformTransactionManager(DataSource dataSource){DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();transactionManager.setDataSource(dataSource);return transactionManager;}
}

3.4.SpringConfig

package com.by.config;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({DataSourceConfig.class, MyBatisConfig.class, TxManagerConfig.class})
@ComponentScan("com.by.service")
@EnableTransactionManagement //等价于<tx:annotation-driven/>
public class ServiceConfig {
}

3.5.SpringMvcConfig

package com.by.config;import com.by.converter.DateConverter;
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.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 // <mvc:annotation-driven/》
public class SpringmvcConfig implements WebMvcConfigurer {@Beanpublic InternalResourceViewResolver getInternalResourceViewResolver(){InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();viewResolver.setPrefix("/WEB-INF/pages/");viewResolver.setSuffix(".jsp");return viewResolver;}@Bean("multipartResolver")public CommonsMultipartResolver getCommonsMultipartResolver(){CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();multipartResolver.setMaxUploadSize(5242880);multipartResolver.setDefaultEncoding("UTF-8");return multipartResolver;}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/account/**").excludePathPatterns("/user/**");}@Overridepublic void addFormatters(FormatterRegistry registry) {registry.addConverter(new DateConverter());}@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/head/**").addResourceLocations("/head/");}
}

3.6.WebConfig

package com.by.config;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[]{ServiceConfig.class};}//加载SpringMVC配置类protected Class<?>[] getServletConfigClasses() {return new Class[]{SpringmvcConfig.class};}//设置SpringMVC请求地址拦截规则protected String[] getServletMappings() {return new String[]{"/"};}//设置post请求中文乱码过滤器@Overrideprotected Filter[] getServletFilters() {CharacterEncodingFilter filter = new CharacterEncodingFilter();filter.setEncoding("utf-8");return new Filter[]{filter};}
}

3.7.删除xml配置文件

在这里插入图片描述

3.8.修改web.xml

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

3.9.测试

略…

总结

--------------------spring的注解--------------------
1、@PropertySource
作用:加载*.properties,等价于context:property-placeholder/
属性:
value:指定*.properties文件的位置
2、@Bean
作用:把实体bean放到ioc容器里,等于
属性:
value:指定IOC容器的key
3、@ComponentScan
作用:指定spring要扫描的包,等价于<context:component-scan base-package=“com.by.service”>
属性:
value:指定要扫描的包
4、@Import
作用:导入配置类,等价于
属性:
value:配置类的字节码
5、@Configuration
作用:等价于,spring读取到该注解会创建一个ioc容器

http://www.mmbaike.com/news/39987.html

相关文章:

  • wordpress性能甘肃seo网站
  • 做网站需要哪个专业买链接网
  • 网站的引导页怎么做的精品成品网站入口
  • 自己做网站要多少钱网站优化建设
  • 做网站版头蓝色图片推广关键词优化
  • 哪个网站有上门做指甲推广营销软件app
  • 网站制作例子seo培训中心
  • 龙岩建设局网站罗小波线上营销活动方案
  • 制作微信公众号的网站开发武汉seo公司出 名
  • 网站建设和维护价格网站制作河南
  • 域名过期做的网站怎么办360竞价推广技巧
  • 如何仿做网站百度seo软件优化
  • 网站图片切换代码深圳华强北
  • seo网站计划书抖音关键词优化排名靠前
  • ui设计周末培训机构网站推广和精准seo
  • 杭州动漫设计公司最新招聘百度关键词优化企业
  • 黑河北京网站建设数据分析师培训机构推荐
  • 仿58网站怎么做凡科网小程序
  • 做阿里巴巴类似的网站吗seo和sem的区别是什么
  • 深圳网站建设网站制作公司seo推广人员
  • 典型网站建设百度文章收录查询
  • 车票网站模板百度排行
  • 视频上传网站建设外贸b2b平台都有哪些网站
  • 做网站图片太多怎么办营销推广渠道有哪些
  • 全能网站建设蜘蛛seo超级外链工具
  • 石家庄个人谁做网站外贸海外推广
  • 外贸网站优势百度贴吧人工客服电话
  • 网站服务类型怎么选社交网络推广方法
  • 飞创网站建设免费找精准客户软件
  • wordpress虚拟资源主题重庆的seo服务公司