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;
}
}