要想知道怎样使用Annotation来配置bean就的知道命名空间:
一, 要先指定命名空间的名称
二, 指定命名空间的Schema文档样式文件的location
下面就是一个简单的基于Annotation配置的程序(在此程序中有多个userDao所以要指定是那个userDao)
注意:一般是使用@Resource不用@Autowired
package com.lbx.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.lbx.dao.UserDao;
import com.lbx.po.User;
import com.lbx.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void init(){
System.out.println("UserServiceImpl.init()");
}
@Override
public void add(User user) {
userDao.add(user);
}
public UserDao getUserDao() {
return userDao;
}
@Autowired//(required=false)
public void setUserDao(@Qualifier(value="userDao") UserDao userDao) { //也可以@Qualifier("userDao")
//@Qualifier一般都写在set方法上 @Qualifier(required=false)意思就是不必须的
this.userDao = userDao;
}
public void destroy(){
System.out.println("UserServiceImpl.destroy()");
}
}
bean.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"
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">
<context:annotation-config />
<bean id="userDao" class="com.lbx.dao.impl.UserDaoImpl"></bean>
<bean id="userDao1" class="com.lbx.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.lbx.service.impl.UserServiceImpl">
</bean>
</beans>
分享到:
相关推荐
ssh2框架整合模板。 spring2.5.6,hibernate3.2 基于annotation配置,配置文件内容很少 struts2基于模式匹配,配置文件也很简单。
基于Annotation的s2sh整合配置实现分页功能基于Annotation的s2sh整合配置实现分页功能基于Annotation的s2sh整合配置实现分页功能基于Annotation的s2sh整合配置实现分页功能基于Annotation的s2sh整合配置实现分页功能
基于annotation s2sh实现零配置的CRM
Spring基于注释(Annotation)的配置.pdfSpring基于注释(Annotation)的配置.pdfSpring基于注释(Annotation)的配置.pdfSpring基于注释(Annotation)的配置.pdfSpring基于注释(Annotation)的配置.pdfSpring基于...
基于annotation s2sh实现零配置的教务管理系统
s2sh基于annotation实现零配置的毕业设计(网上花店系统)
基于annotation s2sh实现零配置的教务管理系统改进版
毕业设计-s2sh基于annotation实现零配置文件的教务管理系统包含所有的jar包
NULL 博文链接:https://baobeituping.iteye.com/blog/1201798
NULL 博文链接:https://kzwang.iteye.com/blog/764791
个人认为由Sun官方支持的EJB规范会越来越流行,此时如果使用基于Annotation的SSH框架很容易转移到Struts+EJB+Spring的项目中,而且使用Annotation,很容易实现0配置,像在这个实例中就一个配置,这样避免了配置文件...
maven & spring & myBatis 的整合使用
spring基于注解的配置介绍,比起xml繁杂的配置文件,开发效率更高
自己学Struts的注解,写的一个零配置小demo。 很简单很简单,只供新手学习。
使用@Transactional注解时,可以通过参数配置事务详情: 5.2.2 基于Annotation方式的声明式事务 * * * * * * * * 声明式事务管理 如何实现Spring的声明式事务管理? 5.2 声明式事务管理 Spring的声明式事务管理可以...
基于注解Annotation的最新版SSH(Struts2.3.7+Hibernate4.1.9+Spring3.2.0)整合开发,真正实现零配置。 最新版本Struts、Spring、Hibernate框架整合: struts-2.3.7 spring-framework-3.2.0.RELEASE hibernate-...
存贮过程进行操作,目前还有待完善,可以不用hibernate-generic-dao - Generic DAO, 用Spring3.2.0和Hibernate4替换,就是一个非常好的Struts2+Spring3+Hibernate4基于annotation的零配置的开发框架 借鉴网友的经验...
2.5.2 基于Annotation的装配 基于Annotation装配的使用方式如下: JDBC驱动器:由数据库厂商创建,也称为JDBC驱动程序。负责与特定的数据库连接,以及处理通信细节。 创建接口UserDao,并定义方法; 创建接口实现类...
1.2. 系统配置 2. 实体Bean 2.1. 简介 2.2. 用EJB3注解进行映射 2.2.1. 声明实体bean 2.2.1.1. 定义表(Table) 2.2.1.2. 乐观锁定版本控制 2.2.2. 映射简单属性 2.2.2.1. 声明基本的属性映射 2.2.2.2. 声明列属性 2.2...
4.3 基于注解的事务配置 1 为业务实现类加上@Transactional注解 为AccountServiceImpl加上@Transactional注解。如下: 也可以加在方法定义前面,可以试试。 2 修改applicationContext-tx.xml 在上添加@Transactional...