【原】spring 2.5--注解

spring 2.5 权威学习资料:http://www.ibm.com/developerworks/cn/java/j-lo-spring25-ioc/

刚刚做了个项目需要spring2.5的注解注入机制,顺便把spring2.5注解的配置文件记录下来。

一 准备工作
    导入Jar包:


二 配置文件:
1 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    
<!--  Spring ApplicationContext -->
    
<listener>
        
<listener-class>
            org.springframework.web.context.ContextLoaderListener
        
</listener-class>
    
</listener>
    
<!--session scope no Spring bean  -->
    
<listener>
        
<listener-class>
            org.springframework.web.context.request.RequestContextListener
        
</listener-class>
    
</listener>
    
    
<context-param>
        
<param-name>contextConfigLocation</param-name>
        
<param-value>
            classpath:
/applicationContext.xml
        
</param-value>
    
</context-param>
    
    
    
<!-- struts2配置文件 -->
    
<filter>
        
<filter-name>struts2</filter-name>
        
<filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        
</filter-class>
    
</filter>
    
<filter-mapping>
        
<filter-name>struts2</filter-name>
        
<url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <welcome-file-list>
        <welcome-file>main.jsp</welcome-file>
    </welcome-file-list>
</web-app>

2 struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>


    
<package name="data_import_manager" extends="struts-default" >
    
        
<action name="importData">
            
<result>/import_taxdata.jsp</result>
        
</action>

        
<action name="taxEnterpriseImport" class="taxImportAction"
            method
="taxEnterprise" >
            
<result name="success">/import_taxdata.jsp</result>
        
</action>
        
<action name="taxInfoImport" class="taxImportAction"
            method
="taxInfo" >
            
<result name="error">/import_taxdata.jsp</result>
            
<result name="success">/success.jsp</result>
        
</action>
        
    
</package>


</struts>

3 application.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:context
="http://www.springframework.org/schema/context" 
    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">

    
<context:component-scan base-package="com.btwob.squall.bmis" />


    
<!-- 数据源 -->
    
<bean id="default_dataSource"
        
class="org.apache.commons.dbcp.BasicDataSource"
        destroy
-method="close">
        
<property name="driverClassName"
            value
="net.sourceforge.jtds.jdbc.Driver" />
        
<property name="url"
            value
="jdbc:jtds:sqlserver://192.168.1.111:1433/BuildingMIS" />
        
<property name="username" value="sa" />
        
<property name="password" value="root" />
        
<property name="maxActive" value="100" />
        
<property name="maxIdle" value="30"></property>
        
<property name="maxWait" value="500" />
    
</bean>


    
<bean id="sessionFactory"
        
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        
<!-- 载入数据源 -->
        
<property name="dataSource">
            
<ref bean="default_dataSource" />
        
</property>
        
<property name="hibernateProperties">
            
<props>
                
<prop key="hibernate.dialect">
                    org.hibernate.dialect.SQLServerDialect
                
</prop>
                
<prop key="show_sql">true</prop>
            
</props>
        
</property>
        
<property name="mappingResources">
            
<value>com/btwob/squall/bmis/model/Tax.hbm.xml</value>
        
</property>
    
</bean>



    
<!-- hibernateTemplate -->
    
<bean id="hibernateTemplate"
        
class="org.springframework.orm.hibernate3.HibernateTemplate">
        
<property name="sessionFactory">
            
<ref bean="sessionFactory" />
        
</property>
    
</bean>
    
    
    
</beans>

三 注解注入文件:
1 dao实现类:
@Scope("singleton")
@Repository(
"taxDao")
public class TaxDao implements ITaxDao {

    @Autowired
    
private HibernateTemplate hibernateTemplate;

public void saveTax(Tax tax) throws Exception {
        hibernateTemplate.save(tax);
        hibernateTemplate.flush();
    }

}
2 serivce实现类:
@Service("taxService")
public class TaxService implements ITaxService {

    @Autowired
    @Qualifier(
"taxDao")
    
private ITaxDao taxDao;
        
public void saveTaxService(Tax tax) {
        
try {
            taxDao.saveTax(tax);
        } 
catch (Exception e) {
            e.printStackTrace();
        }
    }
}
3 action类:

@SuppressWarnings("serial")
@Scope(
"prototype")
@Controller(
"taxImportAction")
public class TaxImportAction extends BaseAction {

    @Autowired
    @Qualifier(
"taxService")
    
private ITaxService taxService;

    
public TaxImportAction() {
        
super();
    }

    @Override
    
public String execute() {
        
return SUCCESS;
    }
    
public String taxEnterprise() throws Exception {return success;
}
    @SuppressWarnings(
"unchecked")
    
public String taxInfo() throws Exception {return success;
}
}



posted on 2010-01-24 22:16 龙樱 阅读(3003) 评论(2)  编辑  收藏 所属分类: 框架层

评论

# re: spring 2.5--注解 2010-01-25 09:42 咖啡妆

你的项目的jar包也太疯狂了吧 一大堆,重复的也很多,spring只需要 spring.jar 在spring3.X以后各个模块裁分开编译jar包。
建议用spring2.X 的最后版本spring2.6 。有些功能spring2.5没有而且有必要。spring3.x的想后兼容性会有问题。  回复  更多评论   

# re: spring 2.5--注解 2010-02-05 17:32 傀儡守望者

@咖啡妆
JAR包,都是项目中需要的,已经把重复的删除了。
  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2010年1月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(3)

随笔分类(13)

随笔档案(13)

文章分类(1)

文章档案(1)

搜索

最新评论

阅读排行榜

评论排行榜