@Service用于标注业务层组件、 @Controller用于标注控制层组件(如struts中的action) @Repository用于标注数据访问组件,即DAO组件。 @Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Autowired 默认按类型装配,如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下: @Autowired @Qualifier("personDaoBean") 存在多个实例配合使用 @Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。web.xml 配置
在web中配置struts的filter 和spring 的监听器及配置文件。
<context-param> <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> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>spring整合struts有两种方案,根据action的创建模式来划分的。
连接有struts-spring-plugin.jar来完成,插件中提供了新的工厂类,覆盖了Struts2的原工厂类。新工厂类的工作原理:
1、首先根据Action的完整类名,到Spring配置文件中查询bean标签的id是否存在一致的名称。如果有一致的说明,Action对象是由Spring负责创建,并有Spring进行装配组合对象之间的关系。(方案二)
2、如果查找不到,会进行特殊的处理操作,再由Strtus2框架进行反射创建Action对象,并采用Strust2框架的自动装配功能来完成Action和Service对象的关联。默认是根据name进行查找的,当然我们也可以进行修改,例如改成以type类型进行查找的方式:<constantname="struts.objectFactory.spring.autoWire"value="type"></constant>(方案一)
这种集成合并的方式Struts2和Spring都是各自干各自的,集成的不是很好。因为Sping是容器么,对对象的管理更为专业,Struts2对流程的控制更加专业。
struts.xml配置
<struts> <constant name="struts.objectFactory" value="spring"/> <package name="default" namespace="/" extends="struts-default"> <action name="helloAction" class="com.wxg.action.userAction"> <result name="success">admin/welcome.jsp</result> <result name="error">admin/error.jsp</result> </action> </package> </struts>userAction类中不使用spring标签,类中的service要和spring中的beanId一致。
public class HelloAction extends ActionSupport{ private UserService userService; public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } }service的配置(spring整合struts的两种方案中,配置是一样的)
@Service("userService") @Transactional public class UserServiceImpl implements UserService{ private UserDao userDao; public UserDao getUserDao() { return userDao; } @Resource(name="userDao") public void setUserDao(UserDao userDao) { this.userDao = userDao; } }步骤和上边的基本上一样,都是导入响应的jar包,拷入响应的配置文件,web.xml文件的写法也一样,struts.xmlp配置有变化,action类的注释有变化,其他不变。看一下两个框架核心配置文件的和第一种方案的写法区别:
struts.xml
<struts> <package name="default" namespace="/" extends="struts-default"> <action name="helloAction" class="helloAction"> <result name="success">admin/welcome.jsp</result> <result name="error">admin/error.jsp</result> </action> </package> </struts>action中的配置
package com.wxg.security.action; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; import com.wxg.security.model.User; import com.wxg.security.service.UserService; @Controller("helloAction") @Scope("prototype") public class HelloAction extends ActionSupport{ private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } private UserService userService; public UserService getUserService() { return userService; } @Resource(name="userService") public void setUserService(UserService userService) { this.userService = userService; } @Override public String execute() { return NONE; } public String save(){ userService.save(); return SUCCESS; } public String login(){ String password = userService.getPassword(user.getUsername()); if(password.equals("error")) { return ERROR; }else if (user.getPassword().equals(password)){ return SUCCESS; } return ERROR; } }applicationContext.xml(spring的配置,整合hibernate的内容,包含连接池,springTemplate,spring事务的配置)
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:annotation-config/> <context:component-scan base-package="com.wxg"/>//开启spring注解扫描 <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"> <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/> <property name="url" value="jdbc:jtds:sqlserver://127.0.0.1:1433/mySSH"/> <property name="username" value="sa"/> <property name="password" value="1234"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="com.wxg.security.model"/> //开启Hibernate注解扫描 <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.SQLServerDialect hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider hibernate.show_sql=true hibernate.format_sql=true </value> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>hibernate.cfg.xml(不使用,在spring 中配置)
项目目录结构
model类(使用了hibernate 的配置模式)
package com.wxg.security.model; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="t_user") public class User { private Integer id; private String username; private String password; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Column public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public void print(){ System.out.println("spring test...."); System.out.println(this.username+":"+this.password); } }dao的实现类(这里采用了上级的接口,使用了spring的注释)
package com.wxg.security.dao.hibernate; import java.util.List; import java.sql.SQLException; import javax.annotation.Resource; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.criterion.Restrictions; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.stereotype.Repository; import com.wxg.security.dao.UserDao; import com.wxg.security.model.User; @Repository("userDao") public class UserHibernateDao implements UserDao { HibernateTemplate hibernateTemplate; public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; } @Resource(name="hibernateTemplate") public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } public void save(){ User user = new User(); user.setPassword("xxxx"); user.setUsername("oooo"); hibernateTemplate.save(user); } @SuppressWarnings("unchecked") @Override public String getPassword(String username) { System.out.println(username); List<User> list = (List<User>)hibernateTemplate.find("from User where username=?",username); if(list == null || list.size()==0){ return "error"; }else{ User user = (User) list.get(0); return user.getPassword(); } } }service类(采用了上级接口和spring的注释)
package com.wxg.security.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.wxg.security.dao.UserDao; import com.wxg.security.service.UserService; @Service("userService") @Transactional public class UserServiceImpl implements UserService{ private UserDao userDao; public UserDao getUserDao() { return userDao; } @Resource(name="userDao") public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void save(){ userDao.save(); } @Override public void list() { // TODO Auto-generated method stub } @Override public String getPassword(String username) { return userDao.getPassword(username); } }
