本文主要是介绍基于spring3.0注解实现的spring MVC项目,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
链接:https://item.taobao.com/item.htm?spm=a230r.1.14.1.76bf523wIsO2k&id=547598359683&ns=1&abbucket=15#detail
我们采用sprng MVC开发项目时,通常都会采用注解的方式,这样可以大大提高我们的开发效率。实现零配置。下面我们从零开始重新做一个spring MVC的配置。这个项目完全采用注解的方式开发。
前面的建立项目导入jar包不再叙述。
1、 修改web.xml,文件内容如下:
2、springmvc-servlet.xml配置内容如下:
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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"> <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --> <context:component-scan base-package="com.sxt"/> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!--对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:suffix=".jsp"/> </beans>
3、 hib-config.xml(配置了spring集成hibernate)
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <context:component-scan base-package="com.sxt"/> <!-- 支持aop注解 --> <aop:aspectj-autoproxy /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/myhib"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <!-- key的名字前面都要加hibernate. --> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="packagesToScan"> <value>com.sxt.po</value> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" > <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!--配置一个JdbcTemplate实例--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事务管理 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" > <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="txManager" /> <aop:config> <aop:pointcut expression="execution(public * com.sxt.service.impl.*.*(..))" id="businessService"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager" > <tx:attributes> <tx:method name="find*" read-only="true" propagation="NOT_SUPPORTED" /> <!-- get开头的方法不需要在事务中运行 。 有些情况是没有必要使用事务的,比如获取数据。开启事务本身对性能是有一定的影响的--> <tx:method name="*"/> <!-- 其他方法在实务中运行 --> </tx:attributes> </tx:advice> </beans>
view plai co
4、javaBean类
5、dao类
6、service类
7、control类
8、WEB-INF下建立index.jsp。Index.jsp的内容如下:
<body> <h1>**********${params.uname}</h1> <h1>**********${requestScope.u}</h1> <h1>**********${requestScope.user}</h1>
</body>
好了,基于spring3.0注解实现的spring MVC项目也已经完成了。相比配置xml更少一点,不过对于没有经验的朋友来说结构不是很清晰,建立没有经验的朋友先从配置xml学起,然后再学习注解方法。
这篇关于基于spring3.0注解实现的spring MVC项目的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!