spring2.5 注解技术
http://sunqitang.iteye.com/blog/357837
spring2.5 注解技术- <?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:component-scan base-package="com.babasport"/><?
?
注意分析:?
? 1
- <
- <context:component-scan base-package="com.baobaotao"/>
??? 后面的目录为要自动读取注释文件的目录范围(spring2.5 开始拥有的功能);这个里面包含了<context:annotation-config/>的信息。
? 3
- <?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
?? 这段代码加入了对context的支持
? 4
- <bean id="sessionFactory"src="/img/2012/09/27/1420573603.gif">
- public class ProjectService implements IProjectService {@Resource private ProjectTypeDAO projectTypeDAO;public void save(ProjectType projectType){this.projectTypeDAO.save();}}
例子中的projectTypeDAO就进行了注入,在配置文件中只需要写DAO,就可以不用写Service了
?
2? spring查询管理的相关注释(推荐)? (要支持<context:component-scan base-package=".."/>,由了这个就默认上面也包含了,不用在写上面的那个支持了)
?
? @Service 标注业务层对象的组件
? @Controller? 标注控制层对象的组件
? @Repository 标注数据库访问层,即DAO层
? @Component 标注“泛指"组件
?
把整个项目改造成注解方式的
?
第一步: 编写spring配置文件
?
- <?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:component-scan base-package="com.babasport"/><
?第二步:编写DAO文件,
???? BaseDAO文件继承hibernateDAOSupport。
由于SessionFacory是在hibernateDAOSupport里面的。不能进行更改,所以更set。。的名字。然后由类型匹配来进行更改
?
- package com.babasport.DAO.hibernate;import javax.annotation.Resource;import org.hibernate.SessionFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import org.springframework.stereotype.Repository;@Repositorypublic class BaseDAO extends HibernateDaoSupport {@Autowiredpublic void setSessionFactory0(SessionFactory sessionFactory){super.setSessionFactory(sessionFactory);}}?
普遍的DAO文件来继承这个BaseDAO文件:
- package com.babasport.Service.project;import javax.annotation.Resource;import org.springframework.stereotype.Service;import com.babasport.DAO.IProjectTypeDAO;import com.babasport.DAO.hibernate.ProjectTypeDAO;import com.babasport.VO.project.ProjectType;@Servicepublic class ProjectService implements IProjectService {@Resource(name="projectTypeDAO") private IProjectTypeDAO projectTypeDAO;public void save(ProjectType projectType){this.projectTypeDAO.save(projectType);}}?
第三步: 编写Service文件
?
- package com.babasport.Service.project;import javax.annotation.Resource;import org.springframework.stereotype.Service;import com.babasport.DAO.IProjectTypeDAO;import com.babasport.DAO.hibernate.ProjectTypeDAO;import com.babasport.VO.project.ProjectType;@Servicepublic class ProjectService implements IProjectService {@Resource(name="projectTypeDAO") private IProjectTypeDAO projectTypeDAO;public void save(ProjectType projectType){this.projectTypeDAO.save(projectType);}}
?分析:
?? @Service和@Response为自动往配置文件中加入bean
?? @Resouce和@Autowried为把ref信息导入