Spring实战之@Autowire注解用法详解
作者:cakincqm 发布时间:2021-11-17 20:37:19
标签:Spring,@Autowire注解
本文实例讲述了Spring实战之@Autowire注解用法。分享给大家供大家参考,具体如下:
一 配置
<?xml version="1.0" encoding="GBK"?>
<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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan
base-package="org.crazyit.app.service,org.crazyit.app.dao"/>
</beans>
二 dao接口
BaseDao
package org.crazyit.app.dao;
public interface BaseDao<T>
{
void save(T e);
}
ItemDao
package org.crazyit.app.dao;
import org.crazyit.app.domain.*;
public interface ItemDao extends BaseDao<Item>
{
}
UserDao
package org.crazyit.app.dao;
import org.crazyit.app.domain.*;
public interface UserDao extends BaseDao<User>
{
}
三 dao实现类
BaseDaoImpl
package org.crazyit.app.dao.impl;
import org.crazyit.app.dao.*;
public class BaseDaoImpl<T> implements BaseDao<T>
{
public void save(T e)
{
System.out.println("程序保存对象:" + e);
}
}
ItemDaoImpl
package org.crazyit.app.dao.impl;
import org.springframework.stereotype.*;
import org.crazyit.app.dao.*;
import org.crazyit.app.domain.*;
@Component("itemDao")
public class ItemDaoImpl extends BaseDaoImpl<Item>
implements ItemDao
{
}
UserDaoImpl
package org.crazyit.app.dao.impl;
import org.springframework.stereotype.*;
import org.crazyit.app.dao.*;
import org.crazyit.app.domain.*;
@Component("userDao")
public class UserDaoImpl extends BaseDaoImpl<User>
implements UserDao
{
}
四 Bean
Item
package org.crazyit.app.domain;
public class Item
{
}
User
package org.crazyit.app.domain;
public class User
{
}
五 service接口
BaseService
package org.crazyit.app.service;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.service.*;
public interface BaseService<T>
{
void addEntity(T entity);
}
ItemService
package org.crazyit.app.service;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.service.*;
import org.crazyit.app.domain.*;
@Component
public interface ItemService extends BaseService<Item>
{
}
UserService
package org.crazyit.app.service;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.service.*;
import org.crazyit.app.domain.*;
@Component
public interface UserService extends BaseService<User>
{
}
六 Service实现类
BaseServiceImpl
package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.dao.*;
import org.crazyit.app.service.*;
public class BaseServiceImpl<T> implements BaseService<T>
{
@Autowired
private BaseDao<T> dao;
public void addEntity(T entity)
{
System.out.println("调用" + dao
+ "保存实体:" + entity);
}
}
ItemServiceImpl
package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.service.*;
import org.crazyit.app.domain.*;
@Component("itemService")
public class ItemServiceImpl extends BaseServiceImpl<Item>
implements ItemService
{
}
UserServiceImpl
package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.service.*;
import org.crazyit.app.domain.*;
@Component("userService")
public class UserServiceImpl extends BaseServiceImpl<User>
implements UserService
{
}
七 测试类
package lee;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.crazyit.app.service.*;
import org.crazyit.app.domain.*;
public class BeanTest
{
public static void main(String[] args)throws Exception
{
// 创建Spring容器
ApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
UserService us = ctx.getBean("userService", UserService.class);
us.addEntity(new User());
ItemService is = ctx.getBean("itemService", ItemService.class);
is.addEntity(new Item());
}
}
八 测试
调用org.crazyit.app.dao.impl.UserDaoImpl@b7dd107保存实体:org.crazyit.app.domain.User@42eca56e调用org.crazyit.app.dao.impl.ItemDaoImpl@52f759d7保存实体:org.crazyit.app.domain.Item@7cbd213e
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/chengqiuming/article/details/101172166


猜你喜欢
- 上一篇文章谈到音频剪切、混音、拼接与转码,也详细介绍cMake配置与涉及FFmpeg文件的导入: android端采用FFmpeg进行音频混
- maven3 安装:安装 Maven 之前要求先确定你的 JDK 已经安装配置完成。Maven是 Apache 下的一个项目,目前最新版本是
- 1.使用usb口输入的扫描枪,这里实现使用了winform首先创建一个CS文件using System;using System.Colle
- 现象说明maven的java项目,测试用例和main所在的源码文件均符合缺省写法和格式,但是在使用mvn clean sonar:sonar
- Java中throws和throw的区别讲解当然,你需要明白异常在Java中式以一个对象来看待。并且所有系统定义的编译和运行异常都可以由系统
- 背景:最近小组进行一个环境比较恶劣的项目,由于没有真实的测试环境,决定上云,最终选择国外的heroku,折腾半天,其中有一些坑在这里记录下来
- Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,大家可以查看RFC2045~RFC2049,上面有MIME的详细规范。
- Java中的BigDecimal类的使用:使用Java中的BigDecimal可以进行精确的计算,但是在使用BigDecimal时我们需要注
- 前言爱美之心人皆有之,在 unix 和 linux 命令行环境下工作的闷骚程序员们可能也觉得命令行太单调了,而是他们就发明了在命令行下采用
- 一般我们在java中运行其它类中的方法时,无论是静态调用,还是动态调用,都是在当前的进程中执行的,也就是说,只有一个java虚拟机实例在运行
- 1、properties文件显示乱码问题原因是因为properties默认使用ASCII码,就算在文件中填写了中文,再打开后依然会转换成AS
- JPA主键@Id,@IdClass,@Embeddable,@EmbeddedId1、自动主键默认情况下,主键是一个连续的64位数字(lon
- SpringBoot 动态修改Scheduled场景:可配置的 Scheduled 执行时间,正常的 Scheduled 是在项目启动的时候
- 前言前面几篇我们简单的复习了一下自定义 View 的测量与绘制,并且回顾了常见的一些事件的处理方式。那么如果我们想自定义 ViewGroup
- 说到关注功能,可能很多小伙伴要说了。谁不会写但是没有合理的架构,大家写出来的代码很可能是一大堆的复制粘贴。比如十几个页面,都有这个关注按钮。
- 我们知道,在java中,将一个非原型类型类型的对象引用,赋值给另一个对象的引用之后,这两个引用就指向了同一个对象,如:public clas
- 对于config文件,一般情况下都是使用ConfigurationManager加载,然后通过读取相应节点的值来获取想要的数据,但是,有时候
- 打印Java程序的线程栈信息jstack可以得知当前线程的运行情况安装jstack等命令集,jstack是开发版本jdk的一部分,不是开发版
- 为了解放程序员的双手,减少重复性代码的编写,推荐使用插件:mybatis-plus-generator 进行代码自动生成。下面我将详细介绍通
- 本文为大家分享了CentOS 7下安装JDK8的详细步骤,供大家参考,具体内容如下一、下载JDK 至oracle官网下载,如图所示二、安装J