博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
"注解"的用法
阅读量:4617 次
发布时间:2019-06-09

本文共 3466 字,大约阅读时间需要 11 分钟。

一、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
getAll() { //List
list=sessionFactory.openSession().createQuery("from Dept").list() ; /* * getHibernateTemplate()是用来得到HibernateTemplate的方法,HiernateTemplate中提供了一系列的对数据库操作方法 * find 查询 参数是hql语句 * save 增加 * update 更新 * delete 删除 */ List
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配置文件中添加关键代码扫描注解配置类:

以上代码中,首先添加对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
getAll() { System.out.println("service层的方法getAll()"); List
list=deptDao.getAll(); return list; } public DeptServiceImpl(DeptDao deptDao) { super(); this.deptDao = deptDao; } public DeptServiceImpl() { super(); }}

三、spring管理Junit测试

package com.jbit.fsd.test;import java.util.List;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.jbit.fsd.entity.User;import com.jbit.fsd.service.UserService;/** * 告诉Junit在启动方法之前,首先启动spring容器 */@RunWith(SpringJUnit4ClassRunner.class)/** *告诉配置文件的路径 * @author Administrator */@ContextConfiguration("classpath:applicationContext.xml")public class SpringJunitTest {	/**	 * 告诉spring容器给我一个引用,通过factory就可以访问spring容器了	 */	@Autowired	private BeanFactory factory;	@Test	public  void test() {		UserService service=null;		service = factory.getBean("userServiceImpl",UserService.class);		List
list=service.getAll(); System.out.println(list.size()+"********************"); //ac.getBean("b5C"); }}

项目spring项目中需要导入spring-test.xxx.jar文件

也可以自动装配:ApplicationContext对象,BeanFactory在启动时候不会实例化bean,在调用时候才实例化,ApplicationContext在初始化时候会实例化所有bean对象(推荐)可以在启动时候发现配置问题,也可以定义ApplicationContext设置为延迟加载在配置类型,在配置文件中定义bean的属性lazy-init="true";

 

转载于:https://www.cnblogs.com/ablejava/p/5657863.html

你可能感兴趣的文章
LeetCode 876. Middle of the Linked List
查看>>
作业一
查看>>
joj1023
查看>>
动画原理——旋转
查看>>
Finding LCM LightOJ - 1215 (水题)
查看>>
python生成器
查看>>
PowerDesigner Constraint name uniqueness 错误
查看>>
系统子系统_GPRS子系统流程图
查看>>
为什么 NSLog 不支持 Swift 对象(转)
查看>>
Ubuntu 下搭建SVN服务器
查看>>
css3转换
查看>>
将字符串中不同字符的个数打印出来
查看>>
java第三次上机
查看>>
android Javah生成JNI头文件
查看>>
npm创建react项目
查看>>
关于u32中查找和定位最后到bit Number of 1 Bits
查看>>
sql数据库查询
查看>>
云计算技能图谱
查看>>
委托、Lambda表达式和事件
查看>>
typecho模板制作代码收集
查看>>