"注解"的用法

一、Spring中使用注解实现Bean的定义

在dao的实现类中添加数据访问层的注解Bean,代码例下:

package com.jbit.ssh.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.jbit.ssh.dao.DeptDao;
import com.jbit.ssh.entity.Dept;
@Component("deptDao")  //用于标注dao类
public class DeptDaoImpl  implements DeptDao {

	public List<Dept> getAll() {
		//List<Dept> list=sessionFactory.openSession().createQuery("from Dept").list() ;
		/*
		 * getHibernateTemplate()是用来得到HibernateTemplate的方法,HiernateTemplate中提供了一系列的对数据库操作方法
		 * find 查询   参数是hql语句
		 * save 增加
		 * update 更新
		 * delete 删除
		 */

		List<Dept> list=getHibernateTemplate().find("from Dept");
		System.out.println("*****数据库操作得到所有的部门信息*******");
		return list;
	}
}

以上标红的注解代码和在spring配置文件中定义<bean id="deptDao" class="com.jbit.ssh.dao.imp.DeptDaoImpl"/>效果是一样的,除了@Component,Spring还提供了3个特殊的注解:

@Repository:用于标注Dao层的类。

@Service:用于标注业务层类。

@Controller:用于标准控制器的类。

使用注解配置信息需要在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:tx="http://www.springframework.org/schema/tx"
		xmlns:p="http://www.springframework.org/schema/p"
		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
			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.jbit.ssh.dao,com.jbit.ssh.service">        </context:component-scan>
</beans>

以上代码中,首先添加对context命名空间的声明,然后使用context命名空间下的component-scan扫描注解标注的类,

base-paceage属性指定了要扫描的基本包,spring会扫描这个包里面的所有的类,获取Bean的定义信息。

二、在spring中使用注解实现自动装配

在业务处理类的属性上添加@Autowire 或者在方法的参数进行标注。

package com.jbit.ssh.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.jbit.ssh.dao.DeptDao;
import com.jbit.ssh.entity.Dept;
import com.jbit.ssh.service.DeptService;
//@Service("deptService")  //用于标准service类
public class DeptServiceImpl implements DeptService {
	@Autowired  //自动按类型装配
	@Scop(session)  //指定作用域
	private DeptDao deptDao;
	//这没有自动装配

	public void setDeptDao(DeptDao deptDao) {
		this.deptDao = deptDao;
	}

	public List<Dept> getAll() {
		System.out.println("service层的方法getAll()");

		List<Dept> list=deptDao.getAll();
		return list;
	}
	public DeptServiceImpl(DeptDao deptDao) {
		super();
		this.deptDao = deptDao;
	}

	public DeptServiceImpl() {
		super();
	}
}

  

时间: 2024-08-30 09:19:47

"注解"的用法的相关文章

Spring的注解@Qualifier用法

Spring的注解@Qualifier用法在Controller中需要注入service那么我的这个server有两个实现类如何区分开这两个impl呢?根据注入资源的注解不同实现的方式有一点小小的区别 下面上铺垫图 请忽略我的红线 ##在Controller中使用 @Autowired注入时 Qualifier的意思是合格者,通过这个标示,表明了哪个实现类才是我们所需要的,添加@Qualifier注解,需要注意的是@Qualifier的参数名称为我们之前定义@Service注解的名称之一. ##

SpringMVC实现依赖注入的几个常用注解的用法解释和示例说明

在以前,我们使用Spring对一些属性进行依赖注入(DI)时,通常都是通过在配置文件中配置一个个的<bean>标签来实现,比如说这样: <bean id="userBiz" class="cn.zifangsky.biz.UserBizImpl"> <property name="userDAO" ref="userDAO"/> </bean> 但是一旦项目大了之后,如果要把所有

spring的@Transactional注解详细用法

概述 事务管理对于企业应用来说是至关重要的,即使出现异常情况,它也可以保证数据的一致性.Spring Framework对事务管理提供了一致的抽象,其特点如下: 为不同的事务API提供一致的编程模型,比如JTA(Java Transaction API), JDBC, Hibernate, JPA(Java Persistence API和JDO(Java Data Objects) 支持声明式事务管理,特别是基于注解的声明式事务管理,简单易用 提供比其他事务API如JTA更简单的编程式事务管理

Hibernate 注解的用法以及说明(二)

注解映射必须满足两大条件:Hibernate3.2以上版本和JSEE 5. @Entity 类注释,所有要持久化的类都要有@Entity   public class Org  implements java.io.Serializable {    }   @Id 主键       @Id        @GeneratedValue        private String orgId;         private String orgName;   @Column(name="...

详解Java的Spring框架中的注解的用法

转载:http://www.jb51.net/article/75460.htm 1. 使用Spring注解来注入属性 1.1. 使用注解以前我们是怎样注入属性的 类的实现: class UserManagerImpl implements UserManager { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } ... } 配置文件: <bean id=&qu

@Transactional注解详细用法

概述 事务管理对于企业应用来说是至关重要的,即使出现异常情况,它也可以保证数据的一致性.Spring Framework对事务管理提供了一致的抽象,其特点如下: 为不同的事务API提供一致的编程模型,比如JTA(Java Transaction API), JDBC, Hibernate, JPA(Java Persistence API和JDO(Java Data Objects) 支持声明式事务管理,特别是基于注解的声明式事务管理,简单易用 提供比其他事务API如JTA更简单的编程式事务管理

2、@RequestMapping注解的用法

@RequestMapping有如下属性值: 1.@RequestMapping来映射URL    注解 @RequestMapping 可以用在类定义处和方法定义处.    类定义处:规定初步的请求映射,相对于web应用的根目录:    方法定义处:进一步细分请求映射,相对于类定义处的URL.如果类定义处没有使用该注解,则方法标记的URL相对于根目录而言: package com.springmvc.helloworld_1; import org.springframework.stereo

spring in action学习笔记七:@Conditional注解的用法

@Profile注解是@Conditional注解的一个例子.即@Profile也是用@Conditional注解来实现的. 必须让条件实现Condition这个接口. 下面的案例讲如果环境中有magic这个属性,则能通过条件,spring会创建bean.反之如果环境中没有magic这个属性,则不能通过条件,spring就不会创建bean,会报错. 案例的代码如下: MagicBean的代码如下: 1 package com.advancedWiring.conditionalBeans; 2

@Query注解的用法(Spring Data JPA)

1. 一个使用@Query注解的简单例子 @Query(value = "select name,author,price from Book b where b.price>?1 and b.price<?2") List<Book> findByPriceRange(long price1, long price2); 2.  Like表达式 @Query(value = "select name,author,price from Book b

SpringMVC常用注解的用法

1. @PathVariable 当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上.示例代码: @Controller @RequestMapping("/owners/{ownerId}") public class RelativePathUriTemplateController { @RequestMapping(&qu