springmvc
<!doctype html>
springmvc
配置文件
xxxxxxxxxx
30
1
2
<beans xmlns="http://www.springframework.org/schema/beans"
3
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
xmlns:context="http://www.springframework.org/schema/context"
5
xmlns:mvc="http://www.springframework.org/schema/mvc"
6
xsi:schemaLocation="http://www.springframework.org/schema/beans
7
http://www.springframework.org/schema/beans/spring-beans.xsd
8
http://www.springframework.org/schema/context
9
http://www.springframework.org/schema/context/spring-context.xsd
10
http://www.springframework.org/schema/mvc
11
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
12
<!-- 扫描指定包下的注解 -->
13
<context:component-scan base-package="huluxia.fun.controller"/>
14
<!-- 配置注解驱动,它的主要的作用是:注册映射器HandlerMapping和适配器HandlerAdapter 两个类型的Bean -->
15
<!--HandlerMapping的实现为实现类RequestMappingHandlerMapping,它会处理 @RequestMapping 注解,并将其注册到请求映射表中-->
16
<!--HandlerAdapter的实现为实现类RequestMappingHandlerAdapter,它是处理请求的适配器,确定调用哪个类的哪个方法,并且构造方法参数,返回值 --><!--在使用SpringMVC是一般都会加上该配置 -->
17
<mvc:resources mapping="/bootstrap/**" location="/bootstrap/"/>
18
<mvc:resources mapping="/bootstrap-datetimepicker-master/**" location="/bootstrap-datetimepicker-master/"/>
19
<mvc:resources mapping="/images/**" location="/images/"/>
20
<!-- 配置注解驱动-->
21
<mvc:annotation-driven/>
22
<context:annotation-config />
23
<!-- 注册视图解析器 -->
24
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
25
<!-- 配置前缀 -->
26
<property name="prefix" value="/WEB-INF/pages/"/>
27
<!-- 配置后缀 -->
28
<property name="suffix" value=".jsp"/>
29
</bean>
30
</beans>
NoSuchBeanDefinitionException报错解决
xxxxxxxxxx
5
1
06-Jul-2021 13:40:43.404 信息 [http-nio-8080-exec-6] org.springframework.web.servlet.FrameworkServlet.initServletBean Initializing Servlet 'springmvc'
2
06-Jul-2021 13:40:43.499 警告 [http-nio-8080-exec-6] org.springframework.context.support.AbstractApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController': Unsatisfied dependency expressed through field 'loginService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'huluxia.fun.service.LoginService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
3
06-Jul-2021 13:40:43.499 严重 [http-nio-8080-exec-6] org.springframework.web.servlet.FrameworkServlet.initServletBean Context initialization failed
4
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController': Unsatisfied dependency expressed through field 'loginService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'huluxia.fun.service.LoginService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
5
at
解决办法
web.xml中加
xxxxxxxxxx
7
1
<context-param>
2
<param-name>contextConfigLocation</param-name>
3
<param-value>classpath:applicationContext.xml</param-value>
4
</context-param>
5
<listener>
6
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
7
</listener>
ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext.xml的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。
springmvc中web.xml配置
xxxxxxxxxx
69
1
2
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
5
version="4.0">
6
<!-- 配置加载spring文件的监听器-->
7
<context-param>
8
<param-name>contextConfigLocation</param-name>
9
<param-value>classpath:applicationContext.xml</param-value>
10
</context-param>
11
<listener>
12
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
13
</listener>
14
<!-- 编码过滤-->
15
<filter>
16
<filter-name>encodingFilter</filter-name>
17
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
18
<!--支持异步处理-->
19
<async-supported>true</async-supported>
20
<!-- 配置encoding,告诉指定的编码格式,这里设置为UTF-8 -->
21
<init-param>
22
<param-name>encoding</param-name>
23
<param-value>UTF-8</param-value>
24
</init-param>
25
<!-- 解决请求乱码 -->
26
<init-param>
27
<param-name>forceRequestEncoding</param-name>
28
<param-value>true</param-value>
29
</init-param>
30
<!-- 解决响应乱码 -->
31
<init-param>
32
<param-name>forceResponseEncoding</param-name>
33
<param-value>true</param-value>
34
</init-param>
35
</filter>
36
<filter-mapping>
37
<filter-name>encodingFilter</filter-name>
38
<url-pattern>/*</url-pattern>
39
</filter-mapping>
40
<!-- 配置SpringMVC核心控制器 -->
41
<servlet>
42
<!-- 配置前端过滤器-->
43
<servlet-name>springmvc</servlet-name>
44
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
45
<!-- 配置DispatcherServlet的初始化参数:读取springmvc.xml -->
46
<!-- 初始化时加载配置文件-->
47
<init-param>
48
<param-name>contextConfigLocation</param-name>
49
<param-value>classpath:spring-mvc.xml</param-value>
50
</init-param>
51
<!-- 表示容器在启动时立即加载Servlet -->
52
<load-on-startup>1</load-on-startup>
53
<!--支持异步处理-->
54
<async-supported>true</async-supported>
55
</servlet>
56
<servlet-mapping>
57
<servlet-name>springmvc</servlet-name>
58
<!--
59
/* 匹配所有资源(永远不要这样写)
60
/ 匹配所有请求(推荐)
61
*.扩展名 匹配所有请求(推荐),例如*.do
62
-->
63
<url-pattern>/</url-pattern>
64
</servlet-mapping>
65
<!-- 系统默认页面-->
66
<welcome-file-list>
67
<welcome-file>index.jsp</welcome-file>
68
</welcome-file-list>
69
</web-app>
springmvc-config.xml
xxxxxxxxxx
31
1
2
<beans xmlns="http://www.springframework.org/schema/beans"
3
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
xmlns:context="http://www.springframework.org/schema/context"
5
xmlns:mvc="http://www.springframework.org/schema/mvc"
6
xsi:schemaLocation="http://www.springframework.org/schema/beans
7
http://www.springframework.org/schema/beans/spring-beans.xsd
8
http://www.springframework.org/schema/context
9
http://www.springframework.org/schema/context/spring-context.xsd
10
http://www.springframework.org/schema/mvc
11
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
12
<!-- 扫描指定包下的包 -->
13
<context:component-scan base-package="huluxia.fun.controller"/>
14
<!-- 配置注解驱动:配置处理器映射器和适配器-->
15
<mvc:annotation-driven/>
16
<!-- 配置注解驱动,它的主要的作用是:注册映射器HandlerMapping和适配器HandlerAdapter 两个类型的Bean -->
17
<!--HandlerMapping的实现为实现类RequestMappingHandlerMapping,它会处理 @RequestMapping 注解,并将其注册到请求映射表中-->
18
<!--HandlerAdapter的实现为实现类RequestMappingHandlerAdapter,它是处理请求的适配器,确定调用哪个类的哪个方法,并且构造方法参数,返回值 -->
19
<!--在使用SpringMVC是一般都会加上该配置 -->
20
<!-- 配置静态资源的访问映射,此配置中的文件,将不被前端控制器拦截-->
21
<mvc:resources mapping="/bootstrap/**" location="/bootstrap/"/>
22
<mvc:resources mapping="/bootstrap-datetimepicker-master/**" location="/bootstrap-datetimepicker-master/"/>
23
<mvc:resources mapping="/images/**" location="/images/"/>
24
<!-- 注册视图解析器 -->
25
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
26
<!-- 配置前缀 -->
27
<property name="prefix" value="/WEB-INF/pages/"/>
28
<!-- 配置后缀 -->
29
<property name="suffix" value=".jsp"/>
30
</bean>
31
</beans>
springmvc中ApplicationContext.xml的配置
xxxxxxxxxx
87
1
2
<beans xmlns="http://www.springframework.org/schema/beans"
3
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
xmlns:tx="http://www.springframework.org/schema/tx"
5
xmlns:aop="http://www.springframework.org/schema/aop"
6
xmlns:context="http://www.springframework.org/schema/context"
7
xsi:schemaLocation="http://www.springframework.org/schema/beans
8
http://www.springframework.org/schema/beans/spring-beans.xsd
9
http://www.springframework.org/schema/tx
10
http://www.springframework.org/schema/tx/spring-tx.xsd
11
http://www.springframework.org/schema/aop
12
http://www.springframework.org/schema/aop/spring-aop.xsd
13
http://www.springframework.org/schema/context
14
http://www.springframework.org/schema/context/spring-context.xsd">
15
<!-- 读取db.properties-->
16
<context:property-placeholder location="classpath:huluxia/fun/resources/db.properties" ignore-unresolvable="true" />
17
<!-- 配置数据源-->
18
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
19
<!-- 基本属性 url、user、password -->
20
<property name="driverClassName" value="${jdbc.driver}" />
21
<property name="url" value="${jdbc.url}" />
22
<property name="username" value="${jdbc.username}" />
23
<property name="password" value="${jdbc.password}" />
24
<!-- 配置监控统计拦截的filters -->
25
<property name="filters" value="stat" />
26
<!-- 配置初始化大小、最小、最大 -->
27
<property name="maxActive" value="20" />
28
<property name="initialSize" value="1" />
29
<property name="minIdle" value="1" />
30
<!-- 配置获取连接等待超时的时间 -->
31
<property name="maxWait" value="60000" />
32
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
33
<property name="timeBetweenEvictionRunsMillis" value="60000" />
34
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
35
<property name="minEvictableIdleTimeMillis" value="300000" />
36
<property name="testWhileIdle" value="true" />
37
<property name="testOnBorrow" value="false" />
38
<property name="testOnReturn" value="false" />
39
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
40
<property name="poolPreparedStatements" value="true" />
41
<property name="maxOpenPreparedStatements" value="20" />
42
</bean>
43
<!-- 事务管理器,依赖于数据源-->
44
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
45
<property name="dataSource" ref="dataSource"></property>
46
</bean>
47
<!-- 开启事务注解-->
48
<tx:annotation-driven transaction-manager="transactionManager"/>
49
<!-- 配置Mybatis中的sqlSessionFactory,SqlSessionFactoryBean是用来产生sqlSessionFactory的 -->
50
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
51
<property name="basePackage" value="huluxia.fun.dao" />
52
<!-- 可选,如果不写,Spring启动时候。容器中自动会按照类型去把SqlSessionFactory对象注入进来 -->
53
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
54
</bean>
55
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
56
<!-- 加载上面配置好数据源 -->
57
<property name="dataSource" ref="dataSource" />
58
<!-- 加载mybatis的映射配置文件;XxxMapper.xml -->
59
<property name="mapperLocations" value="classpath:huluxia/fun/po/*.xml" />
60
<!-- 加载mybatis的全局配置文件;mybatis-config.xml-->
61
<property name="configLocation" value="classpath:mybatis-config.xml"/>
62
<!-- 配置别名,使用包扫描,扫描com.thr.pojo包下的所有实体 -->
63
<property name="typeAliasesPackage" value="huluxia.fun.pojo"/>
64
</bean>
65
<!-- 配置AOP -->
66
<!--通知-->
67
<tx:advice id="txadvice" transaction-manager="transactionManager">
68
<tx:attributes>
69
<tx:method name="select*" propagation="REQUIRED"/>
70
<tx:method name="insert*" propagation="REQUIRED"/>
71
<tx:method name="add*" propagation="REQUIRED"/>
72
<tx:method name="create*" propagation="REQUIRED"/>
73
<tx:method name="delete*" propagation="REQUIRED"/>
74
<tx:method name="update*" propagation="REQUIRED"/>
75
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
76
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
77
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
78
</tx:attributes>
79
</tx:advice>
80
<!-- 切面-->
81
<aop:config>
82
<aop:advisor advice-ref="txadvice"
83
pointcut="execution(* huluxia.fun.service.*.*(..))"/>
84
</aop:config>
85
<!-- 扫描service-->
86
<context:component-scan base-package="huluxia.fun.service"/>
87
</beans>

浙公网安备 33010602011771号