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
0
投稿
猜你喜欢
- 现如今打开一个 App,比如头条、微博,都会有长列表,随着我们不断地滑动,视窗内的内容也会不断地更新。今天就用 Flutter 实现一下这种
- 原因每次使用idea新建项目,就会在默认的c盘下的一个maven仓库中下载jar包,可是我自己指定maven仓库不是这个。如何让idea在新
- 本文讲述了WinForm中实现拖拽效果的功能,即在WinForm中有一个Button,可以实现拖拽这个Button到目标位置后生成一个该控件
- import java.util.Date;import java.text.DateFormat;/*** 格式化时间类* DateFor
- 1.多节点无缝切换问题分布式节点中的服务宕机或者重启不影响客户端使用分布式节点中的服务宕机重启不影响业务服务内部通信如果在某个分布式系统中想
- JWT简介Json Web Token(JWT):JSON网络令牌,是为了在网络应用环境间传递声明而制定的一种基于JSON的开放标准((RF
- 参数校验主要使用两个标签@Validated和@Valid;@Valid是Hibernate的注解校验,@Validated是spring的
- 概述从今天开始, 小白我将带大家开启 Java 数据结构 & 算法的新篇章.KMP 算法KMP (Knuth-Morris-Prat
- 本文实例讲述了Java网络编程实现的简单端口扫描器。分享给大家供大家参考,具体如下:在计算机网络的学习中,不由得觉得这门课的零碎知识点异常之
- 详解 Corba开发之Java实现Service与Client1 概述
- 基本语法C#,又名Csharp,天朝喜欢叫C井。C#是一种面向对象的编程语言。在面向对象的程序设计方法中,程序有各种相互交互的对象组成。相同
- 前言我们通常使用Spring boot做项目搭建的基础框架,必然少不了它的内置日志框架Logback,在spring-boot-starte
- 近日工程中,逐渐感觉到原来复制粘贴代码的笨重,突然想起以前有人和我说起过Git和SVN之类的版本管理工具。由于平时主要是写Java代码,所以
- 1.springboot 2.0 默认连接池就是Hikari了,所以引用parents后不用专门加依赖2.贴我自己的配置(时间单位都是毫秒)
- jedis是redis的java客户端,spring将redis连接池作为一个bean配置。redis连接池分为两种,一种是“redis.c
- 本文实例汇总了C#中@的用法,对C#程序设计来说有不错的借鉴价值。具体如下:一 字符串中的用法1.学过C#的人都知道C# 中字符串常量可以以
- 一、什么是外观模式定义:为子系统中的一组接口提供一个一致的界面,用来访问子系统中的一群接口。外观模式组成:Facade:负责子系统的的封装调
- 在作应用系统开发时,管理配置是必不可少的。例如数据库服务器的配置、安装和更新配置等等。由于Xml的兴起,现在的配置文件大都是以xml文档来存
- 本文实例讲述了Java循环队列原理与用法。分享给大家供大家参考,具体如下:在正式进行循环队列学习之前,我们先来看看在顺序队列中删除队首元素出
- jackson反序列化忽略字段JSON字符串中含有我们并不需要的字段,那么当对应的实体类中不含有该字段时,会抛出一个异常,告诉你有些字段没有