最近因爲項目要求,要求用SpringMVC進行項目的開發,因此就到網上找資料,進行SpringMVC的練習,爲了之後的項目作準備。首先咱們先來看一下SpringMVC的配置吧。java
項目截圖mysql
(1)web.xml的配置web
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<!-- application的加載路徑 -->
<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>
<!-- SpringMVC處理的核心 -->
<servlet>
<servlet-name>yygl</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 找到servlet,注意必然還有一個yygl-servlet.xml與之對應 -->
<servlet-mapping>
<servlet-name>yygl</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>spring
</web-app>sql
(2)applicationContext.xmlsession
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName"> <!-- setup spring annotation scan --> <context:component-scan base-package="."> </context:component-scan> <!-- dataSource --> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="jdbc:mysql://localhost:3306/ylgl" /> <property name="username" value="root" /> <property name="password" value="xuzhang" /> <!-- 初始化鏈接大小 --> <property name="initialSize" value="0" /> <!-- 鏈接池最大使用鏈接數量 --> <property name="maxActive" value="20" /> <!-- 鏈接池最大空閒 --> <property name="maxIdle" value="20" /> <!-- 鏈接池最小空閒 --> <property name="minIdle" value="0" /> <!-- 獲取鏈接最大等待時間 --> <property name="maxWait" value="60000" /> <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="testWhileIdle" value="true" /> <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打開removeAbandoned功能 --> <property name="removeAbandoned" value="true" /> <!-- 1800秒,也就是30分鐘 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 關閉abanded鏈接時輸出錯誤日誌 --> <property name="logAbandoned" value="true" /> <!-- 開啓Druid的監控統計功能 --> <property name="filters" value="stat" /> <!--<property name="filters" value="mergeStat" /> --></bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> </props> </property> <property name="mappingDirectoryLocations"> <list><!-- 實體 --> <value>classpath:com/xuzhang/cn/entity/</value> </list> </property> </bean> <!-- HibernateTemplate bean--> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- Configuration Affairs --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- Cut point cut all service --> <aop:config> <aop:advisor pointcut="execution(* org.commonframe.hibernate..*Service*.*(..) )" advice-ref="txAdvice" /> <aop:advisor pointcut="execution(* com.xuzhang.cn.jcxx..*Service*.*(..) )" advice-ref="txAdvice" /> </aop:config> <!-- All at the beginning of the find, query, search method does not apply transactions --> <tx:advice id="txAdvice"> <tx:attributes> <tx:method name="get*" read-only="true" propagation="SUPPORTS" /> <tx:method name="find*" read-only="true" propagation="SUPPORTS" /> <tx:method name="search*" read-only="true" propagation="SUPPORTS" /> <tx:method name="check*" read-only="true" propagation="SUPPORTS" /> <tx:method name="load*" read-only="true" propagation="SUPPORTS" /> <tx:method name="createTask" propagation="NEVER" /> <tx:method name="create*" /> <tx:method name="save*" /> <tx:method name="update*" /> <tx:method name="delete*" /> <tx:method name="remove*" /> <tx:method name="batch*" /> <tx:method name="execute*" /> <tx:method name="upload*" /> <tx:method name="add*" /> <tx:method name="del*" /> </tx:attributes> </tx:advice> </beans>
(3)yygl-servlet.xmlmvc
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName"> <context:annotation-config /> <!-- 把標記了@Controller註解的類轉換爲bean --> <context:component-scan base-package="com.xuzhang.cn.controller" /> <!-- ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- 處理器 --> </beans>
(4)Controllerapp
package com.xuzhang.cn.controller; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.commonframe.hibernate.util.CommonService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.xuzhang.cn.entity.User; import com.xuzhang.cn.services.UserService; @Controller @RequestMapping("/login.do") public class Login { private UserService userService; public Login() { super(); } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } protected CommonService<User> getCommonService(){ return (CommonService<User>)this.userService; } @RequestMapping(params = "method=add") public String add(HttpServletRequest request,HttpServletResponse response){ String id = ""; List<User> data = this.getCommonService().findListByHQL("from User", null); if("".equals(id)){ } return "student_add"; } }
(5)service層框架
servicejsp
package com.xuzhang.cn.services; import org.commonframe.hibernate.util.CommonService; import com.xuzhang.cn.entity.User; public interface UserService extends CommonService<User>{ }
serviceImpl
package com.xuzhang.cn.services.impl; import org.commonframe.hibernate.util.CommonServiceImpl; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; import com.xuzhang.cn.dao.UserDao; import com.xuzhang.cn.entity.User; import com.xuzhang.cn.services.UserService; @Scope("singleton") @Service("userService") public class UserServiceImpl extends CommonServiceImpl<User> implements UserService{ private UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
(6)dao層
userDao
package com.xuzhang.cn.dao; import org.commonframe.hibernate.util.CommonHibernateDao; import com.xuzhang.cn.entity.User; public interface UserDao extends CommonHibernateDao<User>{ }
userDaoImpl
package com.xuzhang.cn.daoimpl; import org.commonframe.hibernate.util.CommonHibernateDaoImpl; import org.springframework.stereotype.Component; import com.xuzhang.cn.dao.UserDao; import com.xuzhang.cn.entity.User; @Component("userDao") public class UserDaoImpl extends CommonHibernateDaoImpl<User> implements UserDao{ }
因爲原來用的是SSH框架,剛開始用SpringMVC的時候,Service死活都注入不進來,最後在網上查找了大量的資料,最後經過一我的的回答解決了個人困惑,具體那我的我忘記了,- -!!,原來的struts2的action經過了插件struts2-spring-plugin-2.2.1.jar來將struts2的action被Spring進行管理。而我剛開始的時候用的是直接實現了spring的Controller接口,因此致使了該action沒法注入service,按照個人理解就是沒有被spring進行管理。
[org.springframework.beans.factory.support.DefaultListableBeanFactory]Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1644028: defining beans [login,userDao,userService,commonHibernateDao,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,dataSource,sessio]
這是咱們控制檯所打印的數據,裏面看到了已經掃描的beans裏面已經包含了login(@Controller Spring所掃描到的)。