论坛首页 入门技术论坛

SSH全注解-annotation详细配置.

浏览 22202 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (5) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-11-19   最后修改:2010-11-19

 如果有点SSH框架的经验,这个很好理解.配置不难.

配置annotation目的:省去庞大的.XML配置简化配置.其实这个annotation最近才流行的.我配置了几天,愁坏了...

这里写这篇博客就是方便下大家,给大家启示:

 

首先annotation是spring2.5的新功能.所以项目用Myeclipse里添加spring支持选择spring2.5.添加Hibernate的时候也要添加annotation需要的jar包,按步骤走就可以.需要特殊加的jar包有:struts2-convention-plugin-2.1.6.jar,hibernate-commons-annotations.jar.

 

web.xml的配置:

 

<!--Spring的装载器  -->
	<listener> 
      <listener-class> 
         org.springframework.web.context.ContextLoaderListener          
      </listener-class> 
    </listener>
    <!--spring的应用上下文  -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
 
  <!-- struts2文件拦截器 -->	
    	
	<filter>  
      <filter-name>struts2</filter-name>  
      <filter-class>xx.servlet.web.MyStrutsFilterDispatcher</filter-class>  
	 <init-param>
  	 	<param-name>actionPackages</param-name>
   		<param-value>xx.bean,xx.dao,xx.quanxin.action</param-value>
       </init-param>
	</filter>  
	<filter-mapping>
	  <filter-name>struts2</filter-name>
	  <url-pattern>/*</url-pattern>
	</filter-mapping>
	

 

 

struts.xml基本没用了,所以就省掉了...先鼓掌.

applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
  http://www.springframework.org/schema/aop  
  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  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/tx  
  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byType">
		
	<context:annotation-config/>
	<context:component-scan base-package="*" />
	
	<aop:aspectj-autoproxy /> 

        <tx:annotation-driven transaction-manager="transactionManager" /> 
    
	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean>
	
	
	<bean id="propertyConfigurer"
	      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	      <property name="location">
	         <value>/WEB-INF/systeminfo.properties</value>
	      </property>
	</bean>
	
    <!-- DBCP数据库连接数据源的配置 -->
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close"> <!-- 把连接重新放到连接池里 -->
        <!-- 添加连接池属性 -->
        <property name="driverClassName" value="${DB.driverClassName}"/>
        <property name="url"  value="${DB.server}"/>
        <property name="username" value="${DB.username}"/>
        <property name="password" value="${DB.password}"/>
        <property name="initialSize" value="2" /> <!-- 初始连接数 -->
        <property name="maxActive" value="50"/> <!-- 连接池最大连接数 -->
        <property name="maxIdle" value="20"/>   <!-- 最大的可空闲的连接数 -->
        <property name="minIdle" value="10"/>    <!-- 最小的可空闲的连接数 -->
        <property name="logAbandoned" value="true" />  <!-- 超时后打印超时连接错误 -->
        <property name="removeAbandoned" value="true" />   <!-- 超时移除连接 -->
        <property name="removeAbandonedTimeout" value="300"/> <!-- 超时时间 -->
        <property name="maxWait" value="1000"/>   <!-- 最大可以等待时间 -->
        <property name="defaultAutoCommit" value="true"/>  <!-- 自动提交, -->
    </bean>
    
    <!-- 将dataSource注入到下面的sessionFactory类里 -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingResources">
            <list>
           		<value>xx/bean/UserInfo.hbm.xml</value>
           		<value>xx/bean/Role.hbm.xml</value>
        	</list>
        </property>
		<!--
			通配符加载方式,暂未启用 <property name="mappingLocations"> <list>
			<value>classpath:/jy/bean/*.hbm.xml</value> </list> </property>
		-->
		<property name="hibernateProperties"> <!-- 这里是Properties列表 -->
			<props>
				<prop key="hibernate.dialect"> org.hibernate.dialect.SQLServerDialect  </prop>
				<prop key="hibernate.show_sql">true</prop> <!-- 显示sql -->
			</props>

		</property>
	</bean>
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
</beans>

 

 注:基本和原来的一样,红色字体是主要变化的...还有头文件也增加了点..到时候复杂就可以了.

 

 

再下面就看源码了...简单的东西马上就来了:

 

DAO层的接口类,也就是Interface没有变化,省掉了.

DAO层的impl文件有变化,看源码:

 

 

 

@Repository("dao")
public class PublicDaoImpl extends HibernateDaoSupport implements PublicDao {
	 
	@Resource(name = "sessionFactory")
    public void setSuperSessionFactory(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }
	
     public void delete(Object obj) {
	this.getHibernateTemplate().delete(obj);

     }
 

Service层接口么变化,看impl的源码:

 

 

@Service("base")
@Transactional
public class BaseServiceImple implements BaseService {
	@Resource(name="dao")
	private PublicDao publicDao;
	
	
	public PublicDao getPublicDao() {
		return publicDao;
	}


 

最后看Action里面的配置.最重要的了,看源码:

 

 


import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import xx.bean.UserInfo;
import xx.service.*;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Namespace("")
@ParentPackage("default-package")
public class UserinfoAction extends ActionSupport {
       
      @Resource(name="base")
	private BaseService baseService;
 @Action(value="/new",results={@Result(name="success",location="/index.jsp")})
	
	public String newUser() {
		UserInfo xx=new UserInfo();
		xx.setUserid("111");
		xx.setPassword("111");
		xx.setSfz("12121");
		this.baseService.save(xx);
		return SUCCESS;
	}
 

自己看吧,红字是重点..不要有set和get方法....

 

然后就输入new.action.....成功...

 

没耐心讲了,写的不够详细..,呵呵...有问题mail我...

 

 

 

 

   发表时间:2010-11-20  
啊  哦  这样讲还不如录一个视频  。  操作性的东西  光文字是说不清的。。。
0 请登录后投票
   发表时间:2010-11-21  
struts用Convention插件,可以连注释都不需要。。。
0 请登录后投票
   发表时间:2010-11-21  
注解越来越流行了呀!
0 请登录后投票
   发表时间:2010-11-22  
这样还不如用配置文件。
0 请登录后投票
   发表时间:2010-11-22  
这种需要明确被注入对象的还不如用配置文件
你要知道当初配置文件出来就是为了解决配置硬编码和分散在各个类里面的问题
而这种使用注解的方式其实是重蹈覆辙罢了
0 请登录后投票
   发表时间:2010-11-22  
貌似听我老大说,注释性能很差?
0 请登录后投票
   发表时间:2010-11-22   最后修改:2010-11-22
还是用配置文件方便,少写比起简单的维护我还是选择后者啊
0 请登录后投票
   发表时间:2010-11-22  
感觉现在大部分地方滥用注解 感觉用在适合的地方就可以了
0 请登录后投票
   发表时间:2010-11-22  
我也是觉的配置好点能够从配置里面查看依赖关系
和在配置里面组装信息

不过2个东西做好同时兼备,能够做到做大的简化和做大的灵活性就好了
不必要追求什么极致 没什么意义
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics